Skip to content

Commit 087c056

Browse files
authored
Improve docs and tests and fix minor issues (frequenz-floss#1178)
This PR accumulate a set of assorted cleanups and minor improvements.
2 parents 18c1b56 + 6689690 commit 087c056

File tree

14 files changed

+235
-372
lines changed

14 files changed

+235
-372
lines changed

src/frequenz/sdk/microgrid/_power_distributing/_component_managers/_battery_manager.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,18 @@ async def _get_distribution(self, request: Request) -> DistributionResult | Resu
224224
Returns:
225225
Distribution of the batteries.
226226
"""
227-
try:
228-
pairs_data: list[InvBatPair] = self._get_components_data(
229-
request.component_ids
230-
)
231-
except KeyError as err:
232-
return Error(request=request, msg=str(err))
227+
match self._get_components_data(request.component_ids):
228+
case str() as err:
229+
return Error(request=request, msg=err)
230+
case list() as pairs_data:
231+
pass
232+
case unexpected:
233+
typing.assert_never(unexpected)
233234

234235
if not pairs_data:
235236
error_msg = (
236-
"No data for at least one of the given "
237-
f"batteries {str(request.component_ids)}"
237+
"No data for at least one of the given batteries: "
238+
+ self._str_ids(request.component_ids)
238239
)
239240
return Error(request=request, msg=str(error_msg))
240241

@@ -510,18 +511,17 @@ def nan_metric_in_list(data: list[DataType], metrics: list[str]) -> bool:
510511

511512
def _get_components_data(
512513
self, batteries: collections.abc.Set[int]
513-
) -> list[InvBatPair]:
514+
) -> list[InvBatPair] | str:
514515
"""Get data for the given batteries and adjacent inverters.
515516
516517
Args:
517518
batteries: Batteries that needs data.
518519
519-
Raises:
520-
KeyError: If any battery in the given list doesn't exists in microgrid.
521-
522520
Returns:
523-
Pairs of battery and adjacent inverter data.
521+
Pairs of battery and adjacent inverter data or an error message if there was
522+
an error while getting the data.
524523
"""
524+
inverter_ids: collections.abc.Set[int]
525525
pairs_data: list[InvBatPair] = []
526526

527527
working_batteries = self._component_pool_status_tracker.get_working_components(
@@ -530,9 +530,9 @@ def _get_components_data(
530530

531531
for battery_id in working_batteries:
532532
if battery_id not in self._battery_caches:
533-
raise KeyError(
533+
return (
534534
f"No battery {battery_id}, "
535-
f"available batteries: {list(self._battery_caches.keys())}"
535+
f"available batteries: {self._str_ids(self._battery_caches.keys())}"
536536
)
537537

538538
connected_inverters = _get_all_from_map(self._bat_invs_map, batteries)
@@ -545,9 +545,10 @@ def _get_components_data(
545545

546546
if batteries_from_inverters != batteries:
547547
extra_batteries = batteries_from_inverters - batteries
548-
raise KeyError(
549-
f"Inverters {_get_all_from_map(self._bat_invs_map, extra_batteries)} "
550-
f"are connected to batteries that were not requested: {extra_batteries}"
548+
inverter_ids = _get_all_from_map(self._bat_invs_map, extra_batteries)
549+
return (
550+
f"Inverter(s) ({self._str_ids(inverter_ids)}) are connected to "
551+
f"battery(ies) ({self._str_ids(extra_batteries)}) that were not requested"
551552
)
552553

553554
# set of set of batteries one for each working_battery
@@ -556,7 +557,7 @@ def _get_components_data(
556557
)
557558

558559
for battery_ids in battery_sets:
559-
inverter_ids: frozenset[int] = self._bat_invs_map[next(iter(battery_ids))]
560+
inverter_ids = self._bat_invs_map[next(iter(battery_ids))]
560561

561562
data = self._get_battery_inverter_data(battery_ids, inverter_ids)
562563
if data is None:
@@ -570,6 +571,9 @@ def _get_components_data(
570571
pairs_data.append(data)
571572
return pairs_data
572573

574+
def _str_ids(self, ids: collections.abc.Set[int]) -> str:
575+
return ", ".join(str(cid) for cid in sorted(ids))
576+
573577
def _get_power_distribution(
574578
self, request: Request, inv_bat_pairs: list[InvBatPair]
575579
) -> DistributionResult:

0 commit comments

Comments
 (0)