99from collections .abc import Iterable
1010from dataclasses import dataclass
1111from datetime import datetime , timedelta , timezone
12- from enum import Enum
1312
1413# pylint: disable=no-name-in-module
1514from frequenz .api .microgrid .battery_pb2 import ComponentState as BatteryComponentState
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
5237class 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 ,
0 commit comments