9
9
from typing import Any , TypeVar , cast
10
10
11
11
import 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
32
14
from frequenz .channels import Receiver
33
15
from 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
36
18
37
19
from ._component import (
38
20
Component ,
@@ -87,7 +69,7 @@ def __init__(
87
69
self ._server_url = server_url
88
70
"""The location of the microgrid API server as a URL."""
89
71
90
- self .api = MicrogridStub (channel .parse_grpc_uri (server_url ))
72
+ self .api = microgrid_pb2_grpc . MicrogridStub (channel .parse_grpc_uri (server_url ))
91
73
"""The gRPC stub for the microgrid API."""
92
74
93
75
self ._broadcasters : dict [int , streaming .GrpcStreamBroadcaster [Any , Any ]] = {}
@@ -113,9 +95,9 @@ async def components(self) -> Iterable[Component]:
113
95
# grpc.aio is missing types and mypy thinks this is not awaitable,
114
96
# but it is
115
97
component_list = await cast (
116
- Awaitable [PbComponentList ],
98
+ Awaitable [microgrid_pb2 . ComponentList ],
117
99
self .api .ListComponents (
118
- PbComponentFilter (),
100
+ microgrid_pb2 . ComponentFilter (),
119
101
timeout = int (DEFAULT_GRPC_CALL_TIMEOUT ),
120
102
),
121
103
)
@@ -127,7 +109,8 @@ async def components(self) -> Iterable[Component]:
127
109
) from grpc_error
128
110
129
111
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 ,
131
114
component_list .components ,
132
115
)
133
116
result : Iterable [Component ] = map (
@@ -151,10 +134,10 @@ async def metadata(self) -> Metadata:
151
134
Returns:
152
135
the microgrid metadata.
153
136
"""
154
- microgrid_metadata : PbMicrogridMetadata | None = None
137
+ microgrid_metadata : microgrid_pb2 . MicrogridMetadata | None = None
155
138
try :
156
139
microgrid_metadata = await cast (
157
- Awaitable [PbMicrogridMetadata ],
140
+ Awaitable [microgrid_pb2 . MicrogridMetadata ],
158
141
self .api .GetMicrogridMetadata (
159
142
Empty (),
160
143
timeout = int (DEFAULT_GRPC_CALL_TIMEOUT ),
@@ -196,14 +179,14 @@ async def connections(
196
179
most likely a subclass of
197
180
[GrpcError][frequenz.client.microgrid.GrpcError].
198
181
"""
199
- connection_filter = PbConnectionFilter (starts = starts , ends = ends )
182
+ connection_filter = microgrid_pb2 . ConnectionFilter (starts = starts , ends = ends )
200
183
try :
201
184
valid_components , all_connections = await asyncio .gather (
202
185
self .components (),
203
186
# grpc.aio is missing types and mypy thinks this is not
204
187
# awaitable, but it is
205
188
cast (
206
- Awaitable [PbConnectionList ],
189
+ Awaitable [microgrid_pb2 . ConnectionList ],
207
190
self .api .ListConnections (
208
191
connection_filter ,
209
192
timeout = int (DEFAULT_GRPC_CALL_TIMEOUT ),
@@ -237,7 +220,7 @@ async def _new_component_data_receiver(
237
220
* ,
238
221
component_id : int ,
239
222
expected_category : ComponentCategory ,
240
- transform : Callable [[PbComponentData ], _ComponentDataT ],
223
+ transform : Callable [[microgrid_pb2 . ComponentData ], _ComponentDataT ],
241
224
maxsize : int ,
242
225
) -> Receiver [_ComponentDataT ]:
243
226
"""Return a new broadcaster receiver for a given `component_id`.
@@ -265,12 +248,14 @@ async def _new_component_data_receiver(
265
248
broadcaster = streaming .GrpcStreamBroadcaster (
266
249
f"raw-component-data-{ component_id } " ,
267
250
# 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.
271
254
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
+ ),
274
259
),
275
260
transform ,
276
261
retry_strategy = self ._retry_strategy ,
@@ -427,7 +412,9 @@ async def set_power(self, component_id: int, power_w: float) -> None:
427
412
await cast (
428
413
Awaitable [Empty ],
429
414
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
+ ),
431
418
timeout = int (DEFAULT_GRPC_CALL_TIMEOUT ),
432
419
),
433
420
)
@@ -444,7 +431,7 @@ async def set_bounds(
444
431
lower : float ,
445
432
upper : float ,
446
433
) -> 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.
448
435
449
436
Args:
450
437
component_id: ID of the component to set bounds for.
@@ -463,15 +450,17 @@ async def set_bounds(
463
450
if lower > 0 :
464
451
raise ValueError (f"Lower bound { lower } must be less than or equal to 0." )
465
452
466
- target_metric = PbSetBoundsParam .TargetMetric .TARGET_METRIC_POWER_ACTIVE
453
+ target_metric = (
454
+ microgrid_pb2 .SetBoundsParam .TargetMetric .TARGET_METRIC_POWER_ACTIVE
455
+ )
467
456
try :
468
457
await cast (
469
458
Awaitable [Timestamp ],
470
459
self .api .AddInclusionBounds (
471
- PbSetBoundsParam (
460
+ microgrid_pb2 . SetBoundsParam (
472
461
component_id = component_id ,
473
462
target_metric = target_metric ,
474
- bounds = PbBounds (lower = lower , upper = upper ),
463
+ bounds = metrics_pb2 . Bounds (lower = lower , upper = upper ),
475
464
),
476
465
timeout = int (DEFAULT_GRPC_CALL_TIMEOUT ),
477
466
),
0 commit comments