3434from ._connection import Connection
3535from ._constants import RECEIVER_MAX_SIZE
3636from ._exception import ApiClientError , ClientNotConnected
37+ from ._id import ComponentId , MicrogridId
3738from ._metadata import Location , Metadata
3839
3940DEFAULT_GRPC_CALL_TIMEOUT = 60.0
@@ -91,7 +92,9 @@ def __init__(
9192 connect = connect ,
9293 channel_defaults = channel_defaults ,
9394 )
94- self ._broadcasters : dict [int , streaming .GrpcStreamBroadcaster [Any , Any ]] = {}
95+ self ._broadcasters : dict [
96+ ComponentId , streaming .GrpcStreamBroadcaster [Any , Any ]
97+ ] = {}
9598 self ._retry_strategy = retry_strategy
9699
97100 @property
@@ -134,7 +137,7 @@ async def components( # noqa: DOC502 (raises ApiClientError indirectly)
134137 )
135138 result : Iterable [Component ] = map (
136139 lambda c : Component (
137- c .id ,
140+ ComponentId ( c .id ) ,
138141 component_category_from_protobuf (c .category ),
139142 component_type_from_protobuf (c .category , c .inverter ),
140143 component_metadata_from_protobuf (c .category , c .grid ),
@@ -176,12 +179,14 @@ async def metadata(self) -> Metadata:
176179 longitude = microgrid_metadata .location .longitude ,
177180 )
178181
179- return Metadata (microgrid_id = microgrid_metadata .microgrid_id , location = location )
182+ return Metadata (
183+ microgrid_id = MicrogridId (microgrid_metadata .microgrid_id ), location = location
184+ )
180185
181186 async def connections ( # noqa: DOC502 (raises ApiClientError indirectly)
182187 self ,
183- starts : Set [int ] = frozenset (),
184- ends : Set [int ] = frozenset (),
188+ starts : Set [ComponentId ] = frozenset (),
189+ ends : Set [ComponentId ] = frozenset (),
185190 ) -> Iterable [Connection ]:
186191 """Fetch the connections between components in the microgrid.
187192
@@ -199,7 +204,13 @@ async def connections( # noqa: DOC502 (raises ApiClientError indirectly)
199204 most likely a subclass of
200205 [GrpcError][frequenz.client.microgrid.GrpcError].
201206 """
202- connection_filter = microgrid_pb2 .ConnectionFilter (starts = starts , ends = ends )
207+ # Convert ComponentId to raw int for the API call
208+ start_ids = {int (start ) for start in starts }
209+ end_ids = {int (end ) for end in ends }
210+
211+ connection_filter = microgrid_pb2 .ConnectionFilter (
212+ starts = start_ids , ends = end_ids
213+ )
203214 valid_components , all_connections = await asyncio .gather (
204215 self .components (),
205216 client .call_stub_method (
@@ -214,7 +225,7 @@ async def connections( # noqa: DOC502 (raises ApiClientError indirectly)
214225
215226 # Filter out the components filtered in `components` method.
216227 # id=0 is an exception indicating grid component.
217- valid_ids = {c .component_id for c in valid_components }
228+ valid_ids = {int ( c .component_id ) for c in valid_components }
218229 valid_ids .add (0 )
219230
220231 connections = filter (
@@ -223,15 +234,15 @@ async def connections( # noqa: DOC502 (raises ApiClientError indirectly)
223234 )
224235
225236 result : Iterable [Connection ] = map (
226- lambda c : Connection (c .start , c .end ), connections
237+ lambda c : Connection (ComponentId ( c .start ), ComponentId ( c .end ) ), connections
227238 )
228239
229240 return result
230241
231242 async def _new_component_data_receiver (
232243 self ,
233244 * ,
234- component_id : int ,
245+ component_id : ComponentId ,
235246 expected_category : ComponentCategory ,
236247 transform : Callable [[microgrid_pb2 .ComponentData ], _ComponentDataT ],
237248 maxsize : int ,
@@ -262,7 +273,7 @@ async def _new_component_data_receiver(
262273 f"raw-component-data-{ component_id } " ,
263274 lambda : aiter (
264275 self .stub .StreamComponentData (
265- microgrid_pb2 .ComponentIdParam (id = component_id )
276+ microgrid_pb2 .ComponentIdParam (id = int ( component_id ) )
266277 )
267278 ),
268279 transform ,
@@ -276,7 +287,7 @@ async def _new_component_data_receiver(
276287
277288 async def _expect_category (
278289 self ,
279- component_id : int ,
290+ component_id : ComponentId ,
280291 expected_category : ComponentCategory ,
281292 ) -> None :
282293 """Check if the given component_id is of the expected type.
@@ -296,19 +307,17 @@ async def _expect_category(
296307 if comp .component_id == component_id
297308 )
298309 except StopIteration as exc :
299- raise ValueError (
300- f"Unable to find component with id { component_id } "
301- ) from exc
310+ raise ValueError (f"Unable to find { component_id } " ) from exc
302311
303312 if comp .category != expected_category :
304313 raise ValueError (
305- f"Component id { component_id } is a { comp .category .name .lower ()} "
314+ f"{ component_id } is a { comp .category .name .lower ()} "
306315 f", not a { expected_category .name .lower ()} ."
307316 )
308317
309318 async def meter_data ( # noqa: DOC502 (ValueError is raised indirectly by _expect_category)
310319 self ,
311- component_id : int ,
320+ component_id : ComponentId ,
312321 maxsize : int = RECEIVER_MAX_SIZE ,
313322 ) -> Receiver [MeterData ]:
314323 """Return a channel receiver that provides a `MeterData` stream.
@@ -332,7 +341,7 @@ async def meter_data( # noqa: DOC502 (ValueError is raised indirectly by _expec
332341
333342 async def battery_data ( # noqa: DOC502 (ValueError is raised indirectly by _expect_category)
334343 self ,
335- component_id : int ,
344+ component_id : ComponentId ,
336345 maxsize : int = RECEIVER_MAX_SIZE ,
337346 ) -> Receiver [BatteryData ]:
338347 """Return a channel receiver that provides a `BatteryData` stream.
@@ -356,7 +365,7 @@ async def battery_data( # noqa: DOC502 (ValueError is raised indirectly by _exp
356365
357366 async def inverter_data ( # noqa: DOC502 (ValueError is raised indirectly by _expect_category)
358367 self ,
359- component_id : int ,
368+ component_id : ComponentId ,
360369 maxsize : int = RECEIVER_MAX_SIZE ,
361370 ) -> Receiver [InverterData ]:
362371 """Return a channel receiver that provides an `InverterData` stream.
@@ -380,7 +389,7 @@ async def inverter_data( # noqa: DOC502 (ValueError is raised indirectly by _ex
380389
381390 async def ev_charger_data ( # noqa: DOC502 (ValueError is raised indirectly by _expect_category)
382391 self ,
383- component_id : int ,
392+ component_id : ComponentId ,
384393 maxsize : int = RECEIVER_MAX_SIZE ,
385394 ) -> Receiver [EVChargerData ]:
386395 """Return a channel receiver that provides an `EvChargeData` stream.
@@ -403,7 +412,7 @@ async def ev_charger_data( # noqa: DOC502 (ValueError is raised indirectly by _
403412 )
404413
405414 async def set_power ( # noqa: DOC502 (raises ApiClientError indirectly)
406- self , component_id : int , power_w : float
415+ self , component_id : ComponentId , power_w : float
407416 ) -> None :
408417 """Send request to the Microgrid to set power for component.
409418
@@ -425,15 +434,15 @@ async def set_power( # noqa: DOC502 (raises ApiClientError indirectly)
425434 self ,
426435 lambda : self .stub .SetPowerActive (
427436 microgrid_pb2 .SetPowerActiveParam (
428- component_id = component_id , power = power_w
437+ component_id = int ( component_id ) , power = power_w
429438 ),
430439 timeout = int (DEFAULT_GRPC_CALL_TIMEOUT ),
431440 ),
432441 method_name = "SetPowerActive" ,
433442 )
434443
435444 async def set_reactive_power ( # noqa: DOC502 (raises ApiClientError indirectly)
436- self , component_id : int , reactive_power_var : float
445+ self , component_id : ComponentId , reactive_power_var : float
437446 ) -> None :
438447 """Send request to the Microgrid to set reactive power for component.
439448
@@ -453,7 +462,7 @@ async def set_reactive_power( # noqa: DOC502 (raises ApiClientError indirectly)
453462 self ,
454463 lambda : self .stub .SetPowerReactive (
455464 microgrid_pb2 .SetPowerReactiveParam (
456- component_id = component_id , power = reactive_power_var
465+ component_id = int ( component_id ) , power = reactive_power_var
457466 ),
458467 timeout = int (DEFAULT_GRPC_CALL_TIMEOUT ),
459468 ),
@@ -462,7 +471,7 @@ async def set_reactive_power( # noqa: DOC502 (raises ApiClientError indirectly)
462471
463472 async def set_bounds ( # noqa: DOC503 (raises ApiClientError indirectly)
464473 self ,
465- component_id : int ,
474+ component_id : ComponentId ,
466475 lower : float ,
467476 upper : float ,
468477 ) -> None :
@@ -492,7 +501,7 @@ async def set_bounds( # noqa: DOC503 (raises ApiClientError indirectly)
492501 self ,
493502 lambda : self .stub .AddInclusionBounds (
494503 microgrid_pb2 .SetBoundsParam (
495- component_id = component_id ,
504+ component_id = int ( component_id ) ,
496505 target_metric = target_metric ,
497506 bounds = metrics_pb2 .Bounds (lower = lower , upper = upper ),
498507 ),
0 commit comments