Skip to content

Commit 291f3b2

Browse files
Clean BatteryStatusTracker code and improve documentation
Signed-off-by: ela-kotulska-frequenz <[email protected]>
1 parent 34c7f02 commit 291f3b2

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

src/frequenz/sdk/microgrid/_battery/_status.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,20 @@
3333
class BatteryStatus(Enum):
3434
"""Tells if battery is can be used."""
3535

36-
# Component it is not working.
3736
NOT_WORKING = 0
38-
# Component should work, but it failed in last request. It is blocked for few
39-
# seconds and it is not recommended to use it until it is necessary.
37+
"""Component is not working and should not be used"""
38+
4039
UNCERTAIN = 1
41-
# Component is working
40+
"""Component should work, but it failed in last request. It is blocked for few
41+
seconds and it is not recommended to use it unless it is necessary.
42+
"""
43+
4244
WORKING = 2
45+
"""Component is working"""
4346

4447

4548
@dataclass
46-
class _ComponentReceiver(Generic[T]):
49+
class _ComponentData(Generic[T]):
4750
component_id: int
4851
receiver: Peekable[T]
4952

@@ -80,8 +83,8 @@ class BatteryStatusTracker(AsyncConstructible):
8083
_max_blocking_duration_sec: float
8184
_blocked_until: Optional[datetime]
8285
_last_blocking_duration_sec: float
83-
_battery_receiver: _ComponentReceiver[BatteryData]
84-
_inverter_receiver: _ComponentReceiver[InverterData]
86+
_battery: _ComponentData[BatteryData]
87+
_inverter: _ComponentData[InverterData]
8588

8689
@classmethod
8790
async def async_new(
@@ -105,7 +108,6 @@ async def async_new(
105108
RuntimeError: If battery has no adjacent inverter.
106109
"""
107110
self: BatteryStatusTracker = BatteryStatusTracker.__new__(cls)
108-
self._battery_id = battery_id
109111
self._max_data_age = max_data_age_sec
110112

111113
self._last_status = BatteryStatus.WORKING
@@ -115,23 +117,17 @@ async def async_new(
115117
self._blocked_until = None
116118
self._last_blocking_duration_sec = self._min_blocking_duration_sec
117119

118-
inverter_id = self._find_adjacent_inverter_id()
120+
inverter_id = self._find_adjacent_inverter_id(battery_id)
119121
if inverter_id is None:
120-
raise RuntimeError(
121-
f"Can't find inverter adjacent to battery: {self._battery_id}"
122-
)
122+
raise RuntimeError(f"Can't find inverter adjacent to battery: {battery_id}")
123123

124124
api_client = get_microgrid().api_client
125125

126-
bat_recv = await api_client.battery_data(self._battery_id)
127-
self._battery_receiver = _ComponentReceiver(
128-
self._battery_id, bat_recv.into_peekable()
129-
)
126+
bat_recv = await api_client.battery_data(battery_id)
127+
self._battery = _ComponentData(battery_id, bat_recv.into_peekable())
130128

131129
inv_recv = await api_client.inverter_data(inverter_id)
132-
self._inverter_receiver = _ComponentReceiver(
133-
inverter_id, inv_recv.into_peekable()
134-
)
130+
self._inverter = _ComponentData(inverter_id, inv_recv.into_peekable())
135131

136132
await asyncio.sleep(_START_DELAY_SEC)
137133
return self
@@ -143,7 +139,7 @@ def battery_id(self) -> int:
143139
Returns:
144140
Battery id
145141
"""
146-
return self._battery_id
142+
return self._battery.component_id
147143

148144
def get_status(self) -> BatteryStatus:
149145
"""Return status of the battery.
@@ -157,14 +153,14 @@ def get_status(self) -> BatteryStatus:
157153
Returns:
158154
Battery status
159155
"""
160-
bat_msg = self._battery_receiver.receiver.peek()
161-
inv_msg = self._inverter_receiver.receiver.peek()
156+
bat_msg = self._battery.receiver.peek()
157+
inv_msg = self._inverter.receiver.peek()
162158
if bat_msg is None or inv_msg is None:
163159
if self._last_status == BatteryStatus.WORKING:
164160
if not bat_msg:
165-
component_id = self._battery_id
161+
component_id = self._battery.component_id
166162
else:
167-
component_id = self._inverter_receiver.component_id
163+
component_id = self._inverter.component_id
168164

169165
_logger.warning(
170166
"None returned from component %d receiver.", component_id
@@ -320,7 +316,7 @@ def _is_battery_state_correct(self, msg: BatteryData) -> bool:
320316
if self._last_status == BatteryStatus.WORKING:
321317
_logger.warning(
322318
"Battery %d has invalid state: %s",
323-
self._battery_id,
319+
self._battery.component_id,
324320
str(state),
325321
)
326322
return False
@@ -332,7 +328,7 @@ def _is_battery_state_correct(self, msg: BatteryData) -> bool:
332328
if self._last_status == BatteryStatus.WORKING:
333329
_logger.warning(
334330
"Battery %d has invalid relay state: %s",
335-
self._battery_id,
331+
self._battery.component_id,
336332
str(relay_state),
337333
)
338334
return False
@@ -360,17 +356,20 @@ def _is_message_reliable(self, message: ComponentData) -> bool:
360356

361357
return not is_outdated
362358

363-
def _find_adjacent_inverter_id(self) -> Optional[int]:
359+
def _find_adjacent_inverter_id(self, battery_id: int) -> Optional[int]:
364360
"""Find inverter adjacent to this battery.
365361
362+
Args:
363+
battery_id: battery id adjacent to the wanted inverter
364+
366365
Returns:
367366
Id of the inverter. If battery hasn't adjacent inverter, then return None.
368367
"""
369368
graph = get_microgrid().component_graph
370369
return next(
371370
(
372371
comp.component_id
373-
for comp in graph.predecessors(self._battery_id)
372+
for comp in graph.predecessors(battery_id)
374373
if comp.category == ComponentCategory.INVERTER
375374
),
376375
None,

tests/actor/test_battery_pool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def set_battery_message(
235235

236236
# Use protected access to set expected output from the `peek`.
237237
# pylint: disable=protected-access
238-
recv = battery_pool._batteries[bat_id]._battery_receiver.receiver
238+
recv = battery_pool._batteries[bat_id]._battery.receiver
239239
recv.peek.return_value = create_msg(bat_id) # type: ignore[attr-defined]
240240

241241
def set_inverter_message(
@@ -262,7 +262,7 @@ def set_inverter_message(
262262

263263
# Use protected access to set expected output from the `peek`.
264264
# pylint: disable=protected-access
265-
inv_receiver = battery_pool._batteries[bat_id]._inverter_receiver.receiver
265+
inv_receiver = battery_pool._batteries[bat_id]._inverter.receiver
266266
inv_receiver.peek.return_value = create_msg( # type: ignore[attr-defined]
267267
inv_id
268268
)

0 commit comments

Comments
 (0)