Skip to content

Commit 7f88d8c

Browse files
committed
Allow for unknown ComponentCategorys
The new API client can return also an `int` as a component category when the client doesn't know the category number. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 0707542 commit 7f88d8c

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

src/frequenz/sdk/microgrid/_data_sourcing/microgrid_api_source.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
from frequenz.channels import Receiver, Sender
1212
from frequenz.client.common.microgrid.components import ComponentId
13-
from frequenz.client.microgrid import (
14-
ComponentCategory,
15-
)
13+
from frequenz.client.microgrid.component import ComponentCategory
1614
from frequenz.client.microgrid.metrics import Metric
1715
from frequenz.quantities import Quantity
1816

@@ -140,7 +138,7 @@ def __init__(
140138
registry: A channel registry. To be replaced by a singleton
141139
instance.
142140
"""
143-
self._comp_categories_cache: dict[ComponentId, ComponentCategory] = {}
141+
self._comp_categories_cache: dict[ComponentId, ComponentCategory | int] = {}
144142

145143
self.comp_data_receivers: dict[ComponentId, Receiver[Any]] = {}
146144
"""The dictionary of component IDs to data receivers."""
@@ -155,7 +153,7 @@ def __init__(
155153

156154
async def _get_component_category(
157155
self, comp_id: ComponentId
158-
) -> ComponentCategory | None:
156+
) -> ComponentCategory | int | None:
159157
"""Get the component category of the given component.
160158
161159
Args:
@@ -280,7 +278,7 @@ async def _check_meter_request(
280278
async def _check_requested_component_and_metrics(
281279
self,
282280
comp_id: ComponentId,
283-
category: ComponentCategory,
281+
category: ComponentCategory | int,
284282
requests: dict[Metric | TransitionalMetric, list[ComponentMetricRequest]],
285283
) -> None:
286284
"""Check if the requested component and metrics are valid.
@@ -312,7 +310,7 @@ async def _check_requested_component_and_metrics(
312310
raise ValueError(err)
313311

314312
def _get_data_extraction_method(
315-
self, category: ComponentCategory, metric: Metric | TransitionalMetric
313+
self, category: ComponentCategory | int, metric: Metric | TransitionalMetric
316314
) -> Callable[[Any], float]:
317315
"""Get the data extraction method for the given metric.
318316
@@ -341,7 +339,7 @@ def _get_data_extraction_method(
341339

342340
def _get_metric_senders(
343341
self,
344-
category: ComponentCategory,
342+
category: ComponentCategory | int,
345343
requests: dict[Metric | TransitionalMetric, list[ComponentMetricRequest]],
346344
) -> list[tuple[Callable[[Any], float], list[Sender[Sample[Quantity]]]]]:
347345
"""Get channel senders from the channel registry for each requested metric.
@@ -371,7 +369,7 @@ def _get_metric_senders(
371369
async def _handle_data_stream(
372370
self,
373371
comp_id: ComponentId,
374-
category: ComponentCategory,
372+
category: ComponentCategory | int,
375373
) -> None:
376374
"""Stream component data and send the requested metrics out.
377375
@@ -436,14 +434,14 @@ async def clean_tasks(
436434
"Unexpected error while handling data stream for component %d (%s), "
437435
"component data is not being streamed anymore",
438436
comp_id,
439-
category.name,
437+
category,
440438
)
441439
raise
442440

443441
async def _update_streams(
444442
self,
445443
comp_id: ComponentId,
446-
category: ComponentCategory,
444+
category: ComponentCategory | int,
447445
) -> None:
448446
"""Update the requested metric streams for the given component.
449447
@@ -456,7 +454,7 @@ async def _update_streams(
456454

457455
self.comp_data_tasks[comp_id] = asyncio.create_task(
458456
run_forever(lambda: self._handle_data_stream(comp_id, category)),
459-
name=f"{type(self).__name__}._update_stream({comp_id=}, {category.name})",
457+
name=f"{type(self).__name__}._update_stream({comp_id=}, {category})",
460458
)
461459

462460
async def add_metric(self, request: ComponentMetricRequest) -> None:

0 commit comments

Comments
 (0)