99from typing import Any , TypeVar , cast
1010
1111import grpc .aio
12-
13- # pylint: disable=no-name-in-module
14- from frequenz .api .common .components_pb2 import ComponentCategory as PbComponentCategory
15- from frequenz .api .common .metrics_pb2 import Bounds as PbBounds
16- from frequenz .api .microgrid .microgrid_pb2 import ComponentData as PbComponentData
17- from frequenz .api .microgrid .microgrid_pb2 import ComponentFilter as PbComponentFilter
18- from frequenz .api .microgrid .microgrid_pb2 import ComponentIdParam as PbComponentIdParam
19- from frequenz .api .microgrid .microgrid_pb2 import ComponentList as PbComponentList
20- from frequenz .api .microgrid .microgrid_pb2 import ConnectionFilter as PbConnectionFilter
21- from frequenz .api .microgrid .microgrid_pb2 import ConnectionList as PbConnectionList
22- from frequenz .api .microgrid .microgrid_pb2 import (
23- MicrogridMetadata as PbMicrogridMetadata ,
24- )
25- from frequenz .api .microgrid .microgrid_pb2 import SetBoundsParam as PbSetBoundsParam
26- from frequenz .api .microgrid .microgrid_pb2 import (
27- SetPowerActiveParam as PbSetPowerActiveParam ,
28- )
29- from frequenz .api .microgrid .microgrid_pb2_grpc import MicrogridStub
30-
31- # pylint: enable=no-name-in-module
12+ from frequenz .api .common import components_pb2 , metrics_pb2
13+ from frequenz .api .microgrid import microgrid_pb2 , microgrid_pb2_grpc
3214from frequenz .channels import Receiver
3315from frequenz .client .base import channel , retry , streaming
34- from google .protobuf .empty_pb2 import Empty # pylint: disable=no-name-in-module
35- from google .protobuf .timestamp_pb2 import Timestamp # pylint: disable=no-name-in-module
16+ from google .protobuf .empty_pb2 import Empty
17+ from google .protobuf .timestamp_pb2 import Timestamp
3618
3719from ._component import (
3820 Component ,
@@ -87,7 +69,7 @@ def __init__(
8769 self ._server_url = server_url
8870 """The location of the microgrid API server as a URL."""
8971
90- self .api = MicrogridStub (channel .parse_grpc_uri (server_url ))
72+ self .api = microgrid_pb2_grpc . MicrogridStub (channel .parse_grpc_uri (server_url ))
9173 """The gRPC stub for the microgrid API."""
9274
9375 self ._broadcasters : dict [int , streaming .GrpcStreamBroadcaster [Any , Any ]] = {}
@@ -113,9 +95,9 @@ async def components(self) -> Iterable[Component]:
11395 # grpc.aio is missing types and mypy thinks this is not awaitable,
11496 # but it is
11597 component_list = await cast (
116- Awaitable [PbComponentList ],
98+ Awaitable [microgrid_pb2 . ComponentList ],
11799 self .api .ListComponents (
118- PbComponentFilter (),
100+ microgrid_pb2 . ComponentFilter (),
119101 timeout = int (DEFAULT_GRPC_CALL_TIMEOUT ),
120102 ),
121103 )
@@ -127,7 +109,8 @@ async def components(self) -> Iterable[Component]:
127109 ) from grpc_error
128110
129111 components_only = filter (
130- lambda c : c .category is not PbComponentCategory .COMPONENT_CATEGORY_SENSOR ,
112+ lambda c : c .category
113+ is not components_pb2 .ComponentCategory .COMPONENT_CATEGORY_SENSOR ,
131114 component_list .components ,
132115 )
133116 result : Iterable [Component ] = map (
@@ -151,10 +134,10 @@ async def metadata(self) -> Metadata:
151134 Returns:
152135 the microgrid metadata.
153136 """
154- microgrid_metadata : PbMicrogridMetadata | None = None
137+ microgrid_metadata : microgrid_pb2 . MicrogridMetadata | None = None
155138 try :
156139 microgrid_metadata = await cast (
157- Awaitable [PbMicrogridMetadata ],
140+ Awaitable [microgrid_pb2 . MicrogridMetadata ],
158141 self .api .GetMicrogridMetadata (
159142 Empty (),
160143 timeout = int (DEFAULT_GRPC_CALL_TIMEOUT ),
@@ -196,14 +179,14 @@ async def connections(
196179 most likely a subclass of
197180 [GrpcError][frequenz.client.microgrid.GrpcError].
198181 """
199- connection_filter = PbConnectionFilter (starts = starts , ends = ends )
182+ connection_filter = microgrid_pb2 . ConnectionFilter (starts = starts , ends = ends )
200183 try :
201184 valid_components , all_connections = await asyncio .gather (
202185 self .components (),
203186 # grpc.aio is missing types and mypy thinks this is not
204187 # awaitable, but it is
205188 cast (
206- Awaitable [PbConnectionList ],
189+ Awaitable [microgrid_pb2 . ConnectionList ],
207190 self .api .ListConnections (
208191 connection_filter ,
209192 timeout = int (DEFAULT_GRPC_CALL_TIMEOUT ),
@@ -237,7 +220,7 @@ async def _new_component_data_receiver(
237220 * ,
238221 component_id : int ,
239222 expected_category : ComponentCategory ,
240- transform : Callable [[PbComponentData ], _ComponentDataT ],
223+ transform : Callable [[microgrid_pb2 . ComponentData ], _ComponentDataT ],
241224 maxsize : int ,
242225 ) -> Receiver [_ComponentDataT ]:
243226 """Return a new broadcaster receiver for a given `component_id`.
@@ -265,12 +248,14 @@ async def _new_component_data_receiver(
265248 broadcaster = streaming .GrpcStreamBroadcaster (
266249 f"raw-component-data-{ component_id } " ,
267250 # We need to cast here because grpc says StreamComponentData is
268- # a grpc.CallIterator[PbComponentData ] which is not an AsyncIterator,
269- # but it is a grpc.aio.UnaryStreamCall[..., PbComponentData], which it
270- # 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.
271254 lambda : cast (
272- AsyncIterator [PbComponentData ],
273- self .api .StreamComponentData (PbComponentIdParam (id = component_id )),
255+ AsyncIterator [microgrid_pb2 .ComponentData ],
256+ self .api .StreamComponentData (
257+ microgrid_pb2 .ComponentIdParam (id = component_id )
258+ ),
274259 ),
275260 transform ,
276261 retry_strategy = self ._retry_strategy ,
@@ -427,7 +412,9 @@ async def set_power(self, component_id: int, power_w: float) -> None:
427412 await cast (
428413 Awaitable [Empty ],
429414 self .api .SetPowerActive (
430- PbSetPowerActiveParam (component_id = component_id , power = power_w ),
415+ microgrid_pb2 .SetPowerActiveParam (
416+ component_id = component_id , power = power_w
417+ ),
431418 timeout = int (DEFAULT_GRPC_CALL_TIMEOUT ),
432419 ),
433420 )
@@ -444,7 +431,7 @@ async def set_bounds(
444431 lower : float ,
445432 upper : float ,
446433 ) -> None :
447- """Send `PbSetBoundsParam `s received from a channel to the Microgrid service.
434+ """Send `SetBoundsParam `s received from a channel to the Microgrid service.
448435
449436 Args:
450437 component_id: ID of the component to set bounds for.
@@ -463,15 +450,17 @@ async def set_bounds(
463450 if lower > 0 :
464451 raise ValueError (f"Lower bound { lower } must be less than or equal to 0." )
465452
466- target_metric = PbSetBoundsParam .TargetMetric .TARGET_METRIC_POWER_ACTIVE
453+ target_metric = (
454+ microgrid_pb2 .SetBoundsParam .TargetMetric .TARGET_METRIC_POWER_ACTIVE
455+ )
467456 try :
468457 await cast (
469458 Awaitable [Timestamp ],
470459 self .api .AddInclusionBounds (
471- PbSetBoundsParam (
460+ microgrid_pb2 . SetBoundsParam (
472461 component_id = component_id ,
473462 target_metric = target_metric ,
474- bounds = PbBounds (lower = lower , upper = upper ),
463+ bounds = metrics_pb2 . Bounds (lower = lower , upper = upper ),
475464 ),
476465 timeout = int (DEFAULT_GRPC_CALL_TIMEOUT ),
477466 ),
0 commit comments