99from typing import Any , TypeVar , cast
1010
1111import grpc .aio
12- from frequenz .api .common .components_pb2 import ComponentCategory as PbComponentCategory
13- from frequenz .api .common .metrics_pb2 import Bounds as PbBounds
14- from frequenz .api .microgrid .microgrid_pb2 import ComponentData as PbComponentData
15- from frequenz .api .microgrid .microgrid_pb2 import ComponentFilter as PbComponentFilter
16- from frequenz .api .microgrid .microgrid_pb2 import ComponentIdParam as PbComponentIdParam
17- from frequenz .api .microgrid .microgrid_pb2 import ComponentList as PbComponentList
18- from frequenz .api .microgrid .microgrid_pb2 import ConnectionFilter as PbConnectionFilter
19- from frequenz .api .microgrid .microgrid_pb2 import ConnectionList as PbConnectionList
20- from frequenz .api .microgrid .microgrid_pb2 import (
21- MicrogridMetadata as PbMicrogridMetadata ,
22- )
23- from frequenz .api .microgrid .microgrid_pb2 import SetBoundsParam as PbSetBoundsParam
24- from frequenz .api .microgrid .microgrid_pb2 import (
25- SetPowerActiveParam as PbSetPowerActiveParam ,
26- )
27- from frequenz .api .microgrid .microgrid_pb2_grpc import MicrogridStub
12+ from frequenz .api .common import components_pb2 , metrics_pb2
13+ from frequenz .api .microgrid import microgrid_pb2 , microgrid_pb2_grpc
2814from frequenz .channels import Receiver
2915from frequenz .client .base import channel , retry , streaming
3016from google .protobuf .empty_pb2 import Empty
@@ -83,7 +69,7 @@ def __init__(
8369 self ._server_url = server_url
8470 """The location of the microgrid API server as a URL."""
8571
86- self .api = MicrogridStub (channel .parse_grpc_uri (server_url ))
72+ self .api = microgrid_pb2_grpc . MicrogridStub (channel .parse_grpc_uri (server_url ))
8773 """The gRPC stub for the microgrid API."""
8874
8975 self ._broadcasters : dict [int , streaming .GrpcStreamBroadcaster [Any , Any ]] = {}
@@ -109,9 +95,9 @@ async def components(self) -> Iterable[Component]:
10995 # grpc.aio is missing types and mypy thinks this is not awaitable,
11096 # but it is
11197 component_list = await cast (
112- Awaitable [PbComponentList ],
98+ Awaitable [microgrid_pb2 . ComponentList ],
11399 self .api .ListComponents (
114- PbComponentFilter (),
100+ microgrid_pb2 . ComponentFilter (),
115101 timeout = int (DEFAULT_GRPC_CALL_TIMEOUT ),
116102 ),
117103 )
@@ -123,7 +109,8 @@ async def components(self) -> Iterable[Component]:
123109 ) from grpc_error
124110
125111 components_only = filter (
126- lambda c : c .category is not PbComponentCategory .COMPONENT_CATEGORY_SENSOR ,
112+ lambda c : c .category
113+ is not components_pb2 .ComponentCategory .COMPONENT_CATEGORY_SENSOR ,
127114 component_list .components ,
128115 )
129116 result : Iterable [Component ] = map (
@@ -147,10 +134,10 @@ async def metadata(self) -> Metadata:
147134 Returns:
148135 the microgrid metadata.
149136 """
150- microgrid_metadata : PbMicrogridMetadata | None = None
137+ microgrid_metadata : microgrid_pb2 . MicrogridMetadata | None = None
151138 try :
152139 microgrid_metadata = await cast (
153- Awaitable [PbMicrogridMetadata ],
140+ Awaitable [microgrid_pb2 . MicrogridMetadata ],
154141 self .api .GetMicrogridMetadata (
155142 Empty (),
156143 timeout = int (DEFAULT_GRPC_CALL_TIMEOUT ),
@@ -192,14 +179,14 @@ async def connections(
192179 most likely a subclass of
193180 [GrpcError][frequenz.client.microgrid.GrpcError].
194181 """
195- connection_filter = PbConnectionFilter (starts = starts , ends = ends )
182+ connection_filter = microgrid_pb2 . ConnectionFilter (starts = starts , ends = ends )
196183 try :
197184 valid_components , all_connections = await asyncio .gather (
198185 self .components (),
199186 # grpc.aio is missing types and mypy thinks this is not
200187 # awaitable, but it is
201188 cast (
202- Awaitable [PbConnectionList ],
189+ Awaitable [microgrid_pb2 . ConnectionList ],
203190 self .api .ListConnections (
204191 connection_filter ,
205192 timeout = int (DEFAULT_GRPC_CALL_TIMEOUT ),
@@ -233,7 +220,7 @@ async def _new_component_data_receiver(
233220 * ,
234221 component_id : int ,
235222 expected_category : ComponentCategory ,
236- transform : Callable [[PbComponentData ], _ComponentDataT ],
223+ transform : Callable [[microgrid_pb2 . ComponentData ], _ComponentDataT ],
237224 maxsize : int ,
238225 ) -> Receiver [_ComponentDataT ]:
239226 """Return a new broadcaster receiver for a given `component_id`.
@@ -261,12 +248,14 @@ async def _new_component_data_receiver(
261248 broadcaster = streaming .GrpcStreamBroadcaster (
262249 f"raw-component-data-{ component_id } " ,
263250 # We need to cast here because grpc says StreamComponentData is
264- # a grpc.CallIterator[PbComponentData ] which is not an AsyncIterator,
265- # but it is a grpc.aio.UnaryStreamCall[..., PbComponentData], which it
266- # is.
251+ # a grpc.CallIterator[microgrid_pb2.ComponentData ] which is not an
252+ # AsyncIterator, but it is a grpc.aio.UnaryStreamCall[...,
253+ # microgrid_pb2.ComponentData], which it is.
267254 lambda : cast (
268- AsyncIterator [PbComponentData ],
269- self .api .StreamComponentData (PbComponentIdParam (id = component_id )),
255+ AsyncIterator [microgrid_pb2 .ComponentData ],
256+ self .api .StreamComponentData (
257+ microgrid_pb2 .ComponentIdParam (id = component_id )
258+ ),
270259 ),
271260 transform ,
272261 retry_strategy = self ._retry_strategy ,
@@ -423,7 +412,9 @@ async def set_power(self, component_id: int, power_w: float) -> None:
423412 await cast (
424413 Awaitable [Empty ],
425414 self .api .SetPowerActive (
426- PbSetPowerActiveParam (component_id = component_id , power = power_w ),
415+ microgrid_pb2 .SetPowerActiveParam (
416+ component_id = component_id , power = power_w
417+ ),
427418 timeout = int (DEFAULT_GRPC_CALL_TIMEOUT ),
428419 ),
429420 )
@@ -440,7 +431,7 @@ async def set_bounds(
440431 lower : float ,
441432 upper : float ,
442433 ) -> None :
443- """Send `PbSetBoundsParam `s received from a channel to the Microgrid service.
434+ """Send `SetBoundsParam `s received from a channel to the Microgrid service.
444435
445436 Args:
446437 component_id: ID of the component to set bounds for.
@@ -459,15 +450,17 @@ async def set_bounds(
459450 if lower > 0 :
460451 raise ValueError (f"Lower bound { lower } must be less than or equal to 0." )
461452
462- target_metric = PbSetBoundsParam .TargetMetric .TARGET_METRIC_POWER_ACTIVE
453+ target_metric = (
454+ microgrid_pb2 .SetBoundsParam .TargetMetric .TARGET_METRIC_POWER_ACTIVE
455+ )
463456 try :
464457 await cast (
465458 Awaitable [Timestamp ],
466459 self .api .AddInclusionBounds (
467- PbSetBoundsParam (
460+ microgrid_pb2 . SetBoundsParam (
468461 component_id = component_id ,
469462 target_metric = target_metric ,
470- bounds = PbBounds (lower = lower , upper = upper ),
463+ bounds = metrics_pb2 . Bounds (lower = lower , upper = upper ),
471464 ),
472465 timeout = int (DEFAULT_GRPC_CALL_TIMEOUT ),
473466 ),
0 commit comments