Skip to content

Commit

Permalink
Stop PV inverter when it is not known to be working
Browse files Browse the repository at this point in the history
Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
  • Loading branch information
shsms authored and ela-kotulska-frequenz committed Aug 20, 2024
1 parent 23d0612 commit 264dec5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
from .....microgrid import connection_manager
from .....timeseries import Power
from ..._component_pool_status_tracker import ComponentPoolStatusTracker
from ..._component_status import ComponentPoolStatus, PVInverterStatusTracker
from ..._component_status import (
ComponentPoolStatus,
ComponentStatusEnum,
PVInverterStatusTracker,
)
from ...request import Request
from ...result import PartialFailure, Result, Success
from .._component_manager import ComponentManager
Expand Down Expand Up @@ -56,6 +60,12 @@ def __init__(
if self._pv_inverter_ids
else None
)
self._component_status_rx = (
self._component_pool_status_tracker.get_component_status_receiver()
if self._component_pool_status_tracker
else None
)
self._set_to_zero_task = asyncio.create_task(self._set_to_zero_if_not_working())
self._component_data_caches: dict[int, LatestValueCache[InverterData]] = {}
self._target_power = Power.zero()
self._target_power_channel = Broadcast[Request](name="target_power")
Expand Down Expand Up @@ -172,6 +182,15 @@ async def distribute_power(self, request: Request) -> None:
)
await self._set_api_power(request, allocations, remaining_power)

async def _set_to_zero_if_not_working(self) -> None:
if not self._component_status_rx:
return
async for status in self._component_status_rx:
if status.component_id in self._pv_inverter_ids:
if status.value == ComponentStatusEnum.NOT_WORKING:
api_client = connection_manager.get().api_client
await api_client.set_power(status.component_id, 0.0)

async def _set_api_power( # pylint: disable=too-many-locals
self, request: Request, allocations: dict[int, Power], remaining_power: Power
) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__( # pylint: disable=too-many-arguments
self._max_blocking_duration = max_blocking_duration
self._component_status_sender = component_status_sender
self._component_status_tracker_type = component_status_tracker_type

self._component_status_channels: list[Broadcast[ComponentStatus]] = []
# At first no component is working, we will get notification when they start
# working.
self._current_status = ComponentPoolStatus(working=set(), uncertain=set())
Expand Down Expand Up @@ -94,6 +94,7 @@ def _make_merged_status_receiver(
channel: Broadcast[ComponentStatus] = Broadcast(
name=f"component_{component_id}_status"
)
self._component_status_channels.append(channel)
tracker = self._component_status_tracker_type(
component_id=component_id,
max_data_age=self._max_data_age,
Expand All @@ -105,6 +106,14 @@ def _make_merged_status_receiver(
status_receivers.append(channel.new_receiver())
return merge(*status_receivers)

def get_component_status_receiver(self) -> Merger[ComponentStatus]:
"""Get the receiver for the status of the tracked components.
Returns:
Receiver for the status of the tracked components.
"""
return merge(*(chan.new_receiver() for chan in self._component_status_channels))

async def _run(self) -> None:
"""Start tracking component status."""
async with contextlib.AsyncExitStack() as stack:
Expand Down

0 comments on commit 264dec5

Please sign in to comment.