Skip to content

Commit f141e9c

Browse files
committed
Rename StatusComponentStatusEnum
And move it to the `_component_status` module. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 61672f5 commit f141e9c

File tree

4 files changed

+123
-112
lines changed

4 files changed

+123
-112
lines changed

src/frequenz/sdk/actor/power_distributing/_battery_status.py

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from collections.abc import Iterable
1010
from dataclasses import dataclass
1111
from datetime import datetime, timedelta, timezone
12-
from enum import Enum
1312

1413
# pylint: disable=no-name-in-module
1514
from frequenz.api.microgrid.battery_pb2 import ComponentState as BatteryComponentState
@@ -29,25 +28,11 @@
2928
ComponentData,
3029
InverterData,
3130
)
31+
from ._component_status import ComponentStatusEnum
3232

3333
_logger = logging.getLogger(__name__)
3434

3535

36-
class Status(Enum):
37-
"""Tells if battery is can be used."""
38-
39-
NOT_WORKING = 0
40-
"""Component is not working and should not be used"""
41-
42-
UNCERTAIN = 1
43-
"""Component should work, but it failed in last request. It is blocked for few
44-
seconds and it is not recommended to use it unless it is necessary.
45-
"""
46-
47-
WORKING = 2
48-
"""Component is working"""
49-
50-
5136
@dataclass
5237
class SetPowerResult:
5338
"""Information what batteries succeed or failed the last request."""
@@ -188,7 +173,7 @@ def __init__( # pylint: disable=too-many-arguments
188173
battery_id: int,
189174
max_data_age_sec: float,
190175
max_blocking_duration_sec: float,
191-
status_sender: Sender[Status],
176+
status_sender: Sender[ComponentStatusEnum],
192177
set_power_result_receiver: Receiver[SetPowerResult],
193178
) -> None:
194179
"""Create class instance.
@@ -211,7 +196,7 @@ def __init__( # pylint: disable=too-many-arguments
211196
self._max_data_age = max_data_age_sec
212197
# First battery is considered as not working.
213198
# Change status after first messages are received.
214-
self._last_status: Status = Status.NOT_WORKING
199+
self._last_status: ComponentStatusEnum = ComponentStatusEnum.NOT_WORKING
215200
self._blocking_status: _BlockingStatus = _BlockingStatus(
216201
1.0, max_blocking_duration_sec
217202
)
@@ -272,7 +257,8 @@ def _handle_status_set_power_result(self, result: SetPowerResult) -> None:
272257
self._blocking_status.unblock()
273258

274259
elif (
275-
self.battery_id in result.failed and self._last_status != Status.NOT_WORKING
260+
self.battery_id in result.failed
261+
and self._last_status != ComponentStatusEnum.NOT_WORKING
276262
):
277263
duration = self._blocking_status.block()
278264

@@ -301,7 +287,7 @@ def _handle_status_inverter_timer(self) -> None:
301287
self._inverter.last_msg_timestamp,
302288
)
303289

304-
def _get_new_status_if_changed(self) -> Status | None:
290+
def _get_new_status_if_changed(self) -> ComponentStatusEnum | None:
305291
current_status = self._get_current_status()
306292
if self._last_status != current_status:
307293
self._last_status = current_status
@@ -315,7 +301,7 @@ def _get_new_status_if_changed(self) -> Status | None:
315301

316302
async def _run(
317303
self,
318-
status_sender: Sender[Status],
304+
status_sender: Sender[ComponentStatusEnum],
319305
set_power_result_receiver: Receiver[SetPowerResult],
320306
) -> None:
321307
"""Process data from the components and set_power_result_receiver.
@@ -391,7 +377,7 @@ async def _run(
391377
except Exception as err: # pylint: disable=broad-except
392378
_logger.exception("BatteryStatusTracker crashed with error: %s", err)
393379

394-
def _get_current_status(self) -> Status:
380+
def _get_current_status(self) -> ComponentStatusEnum:
395381
"""Get current battery status.
396382
397383
Returns:
@@ -402,15 +388,15 @@ def _get_current_status(self) -> Status:
402388
)
403389

404390
if not is_msg_correct:
405-
return Status.NOT_WORKING
406-
if self._last_status == Status.NOT_WORKING:
391+
return ComponentStatusEnum.NOT_WORKING
392+
if self._last_status == ComponentStatusEnum.NOT_WORKING:
407393
# If message just become correct, then try to use it
408394
self._blocking_status.unblock()
409-
return Status.WORKING
395+
return ComponentStatusEnum.WORKING
410396
if self._blocking_status.is_blocked():
411-
return Status.UNCERTAIN
397+
return ComponentStatusEnum.UNCERTAIN
412398

413-
return Status.WORKING
399+
return ComponentStatusEnum.WORKING
414400

415401
def _is_capacity_present(self, msg: BatteryData) -> bool:
416402
"""Check whether the battery capacity is NaN or not.
@@ -424,7 +410,7 @@ def _is_capacity_present(self, msg: BatteryData) -> bool:
424410
True if battery capacity is present, false otherwise.
425411
"""
426412
if math.isnan(msg.capacity):
427-
if self._last_status == Status.WORKING:
413+
if self._last_status == ComponentStatusEnum.WORKING:
428414
_logger.warning(
429415
"Battery %d capacity is NaN",
430416
msg.component_id,
@@ -445,7 +431,7 @@ def _no_critical_error(self, msg: BatteryData | InverterData) -> bool:
445431
# pylint: disable=protected-access
446432
critical_err = next((err for err in msg._errors if err.level == critical), None)
447433
if critical_err is not None:
448-
if self._last_status == Status.WORKING:
434+
if self._last_status == ComponentStatusEnum.WORKING:
449435
_logger.warning(
450436
"Component %d has critical error: %s",
451437
msg.component_id,
@@ -467,7 +453,7 @@ def _is_inverter_state_correct(self, msg: InverterData) -> bool:
467453
# pylint: disable=protected-access
468454
state = msg._component_state
469455
if state not in BatteryStatusTracker._inverter_valid_state:
470-
if self._last_status == Status.WORKING:
456+
if self._last_status == ComponentStatusEnum.WORKING:
471457
_logger.warning(
472458
"Inverter %d has invalid state: %s",
473459
msg.component_id,
@@ -489,7 +475,7 @@ def _is_battery_state_correct(self, msg: BatteryData) -> bool:
489475
# pylint: disable=protected-access
490476
state = msg._component_state
491477
if state not in BatteryStatusTracker._battery_valid_state:
492-
if self._last_status == Status.WORKING:
478+
if self._last_status == ComponentStatusEnum.WORKING:
493479
_logger.warning(
494480
"Battery %d has invalid state: %s",
495481
self.battery_id,
@@ -501,7 +487,7 @@ def _is_battery_state_correct(self, msg: BatteryData) -> bool:
501487
# pylint: disable=protected-access
502488
relay_state = msg._relay_state
503489
if relay_state not in BatteryStatusTracker._battery_valid_relay:
504-
if self._last_status == Status.WORKING:
490+
if self._last_status == ComponentStatusEnum.WORKING:
505491
_logger.warning(
506492
"Battery %d has invalid relay state: %s",
507493
self.battery_id,
@@ -534,7 +520,7 @@ def _is_message_reliable(self, message: ComponentData) -> bool:
534520
"""
535521
is_outdated = self._is_timestamp_outdated(message.timestamp)
536522

537-
if is_outdated and self._last_status == Status.WORKING:
523+
if is_outdated and self._last_status == ComponentStatusEnum.WORKING:
538524
_logger.warning(
539525
"Component %d stopped sending data. Last timestamp: %s.",
540526
message.component_id,

src/frequenz/sdk/actor/power_distributing/_component_pool_status_tracker.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
from frequenz.channels.util import MergeNamed
1414

1515
from ..._internal._asyncio import cancel_and_await
16-
from ._battery_status import BatteryStatusTracker, SetPowerResult, Status
17-
from ._component_status import ComponentPoolStatus
16+
from ._battery_status import BatteryStatusTracker, SetPowerResult
17+
from ._component_status import ComponentPoolStatus, ComponentStatusEnum
1818

1919
_logger = logging.getLogger(__name__)
2020

@@ -32,7 +32,7 @@ class _ComponentStatusChannelHelper:
3232

3333
def __post_init__(self) -> None:
3434
self.name: str = f"component-{self.component_id}-status"
35-
channel = Broadcast[Status](self.name)
35+
channel = Broadcast[ComponentStatusEnum](self.name)
3636

3737
receiver_name = f"{self.name}-receiver"
3838
self.receiver = channel.new_receiver(name=receiver_name, maxsize=1)
@@ -82,7 +82,7 @@ def __init__( # noqa: DOC502 (RuntimeError raised from BatteryStatusTracker)
8282

8383
# Receivers for individual components statuses are needed to create a
8484
# `MergeNamed` object.
85-
receivers: dict[str, Receiver[Status]] = {}
85+
receivers: dict[str, Receiver[ComponentStatusEnum]] = {}
8686

8787
for battery_id in component_ids:
8888
channel = _ComponentStatusChannelHelper(battery_id)
@@ -98,7 +98,7 @@ def __init__( # noqa: DOC502 (RuntimeError raised from BatteryStatusTracker)
9898
),
9999
)
100100

101-
self._component_status_channel = MergeNamed[Status](
101+
self._component_status_channel = MergeNamed[ComponentStatusEnum](
102102
**receivers,
103103
)
104104

@@ -148,13 +148,13 @@ async def _update_status(
148148
"""
149149
async for channel_name, status in self._component_status_channel:
150150
component_id = self._batteries[channel_name].battery_id
151-
if status == Status.WORKING:
151+
if status == ComponentStatusEnum.WORKING:
152152
self._current_status.working.add(component_id)
153153
self._current_status.uncertain.discard(component_id)
154-
elif status == Status.UNCERTAIN:
154+
elif status == ComponentStatusEnum.UNCERTAIN:
155155
self._current_status.working.discard(component_id)
156156
self._current_status.uncertain.add(component_id)
157-
elif status == Status.NOT_WORKING:
157+
elif status == ComponentStatusEnum.NOT_WORKING:
158158
self._current_status.working.discard(component_id)
159159
self._current_status.uncertain.discard(component_id)
160160

src/frequenz/sdk/actor/power_distributing/_component_status.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""Classes to track the status of components in the microgrid."""
55

66

7+
import enum
78
from collections import abc
89
from dataclasses import dataclass
910

@@ -31,3 +32,20 @@ def get_working_components(self, components: abc.Set[int]) -> set[int]:
3132
if len(working) > 0:
3233
return working
3334
return self.uncertain.intersection(components)
35+
36+
37+
class ComponentStatusEnum(enum.Enum):
38+
"""Enum for component status."""
39+
40+
NOT_WORKING = 0
41+
"""Component is not working and should not be used."""
42+
43+
UNCERTAIN = 1
44+
"""Component should work, although the last request to it failed.
45+
46+
It is blocked for few seconds and it is not recommended to use it unless it is
47+
necessary.
48+
"""
49+
50+
WORKING = 2
51+
"""Component is working"""

0 commit comments

Comments
 (0)