Skip to content

Commit f3e3d41

Browse files
committed
Update MicrogridGrpcClient and tests to use latest grpc methods
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 1638430 commit f3e3d41

File tree

2 files changed

+11
-34
lines changed

2 files changed

+11
-34
lines changed

src/frequenz/sdk/microgrid/client/_client.py

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import asyncio
77
import logging
8-
import math
98
from abc import ABC, abstractmethod
109
from typing import (
1110
Any,
@@ -20,12 +19,11 @@
2019
)
2120

2221
import grpc
23-
from frequenz.api.microgrid import common_pb2 as common_pb
2422
from frequenz.api.common import components_pb2 as components_pb
23+
from frequenz.api.common import metrics_pb2 as metrics_pb
2524
from frequenz.api.microgrid import microgrid_pb2 as microgrid_pb
2625
from frequenz.api.microgrid.microgrid_pb2_grpc import MicrogridStub
2726
from frequenz.channels import Broadcast, Receiver, Sender
28-
from google.protobuf.empty_pb2 import Empty # pylint: disable=no-name-in-module
2927

3028
from ..._internal._constants import RECEIVER_MAX_SIZE
3129
from ..component import (
@@ -341,7 +339,7 @@ async def _component_data_task(
341339
"Making call to `GetComponentData`, for component_id=%d", component_id
342340
)
343341
try:
344-
call = self.api.GetComponentData(
342+
call = self.api.StreamComponentData(
345343
microgrid_pb.ComponentIdParam(id=component_id),
346344
)
347345
# grpc.aio is missing types and mypy thinks this is not
@@ -579,25 +577,12 @@ async def set_power(self, component_id: int, power_w: float) -> None:
579577
when the api call exceeded timeout
580578
"""
581579
try:
582-
if power_w >= 0:
583-
# grpc.aio is missing types and mypy thinks this is not
584-
# async iterable, but it is
585-
await self.api.Charge(
586-
microgrid_pb.PowerLevelParam(
587-
component_id=component_id, power_w=math.floor(power_w)
588-
),
589-
timeout=DEFAULT_GRPC_CALL_TIMEOUT, # type: ignore[arg-type]
590-
) # type: ignore[misc]
591-
else:
592-
# grpc.aio is missing types and mypy thinks this is not
593-
# async iterable, but it is
594-
power_w *= -1
595-
await self.api.Discharge(
596-
microgrid_pb.PowerLevelParam(
597-
component_id=component_id, power_w=math.floor(power_w)
598-
),
599-
timeout=DEFAULT_GRPC_CALL_TIMEOUT, # type: ignore[arg-type]
600-
) # type: ignore[misc]
580+
await self.api.SetPowerActive(
581+
microgrid_pb.SetPowerActiveParam(
582+
component_id=component_id, power=power_w
583+
),
584+
timeout=DEFAULT_GRPC_CALL_TIMEOUT, # type: ignore[arg-type]
585+
) # type: ignore[misc]
601586
except grpc.aio.AioRpcError as err:
602587
msg = f"Failed to set power. Microgrid API: {self.target}. Err: {err.details()}"
603588
raise grpc.aio.AioRpcError(
@@ -633,20 +618,13 @@ async def set_bounds(
633618
if lower > 0:
634619
raise ValueError(f"Lower bound {upper} must be less than or equal to 0.")
635620

636-
# grpc.aio is missing types and mypy thinks request_iterator is
637-
# a required argument, but it is not
638-
set_bounds_call = self.api.SetBounds(
639-
timeout=DEFAULT_GRPC_CALL_TIMEOUT,
640-
) # type: ignore[call-arg]
641621
try:
642-
# grpc.aio is missing types and mypy thinks set_bounds_call can be Empty
643-
assert not isinstance(set_bounds_call, Empty)
644-
await set_bounds_call.write(
622+
self.api.AddInclusionBounds(
645623
microgrid_pb.SetBoundsParam(
646624
component_id=component_id,
647625
# pylint: disable=no-member,line-too-long
648626
target_metric=microgrid_pb.SetBoundsParam.TargetMetric.TARGET_METRIC_POWER_ACTIVE,
649-
bounds=common_pb.Bounds(lower=lower, upper=upper),
627+
bounds=metrics_pb.Bounds(lower=lower, upper=upper),
650628
),
651629
)
652630
except grpc.aio.AioRpcError as err:

tests/microgrid/test_timeout.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ def mock_set_power(
9898
time.sleep(GRPC_SERVER_DELAY)
9999
return Empty()
100100

101-
mocker.patch.object(servicer, "Charge", mock_set_power)
102-
mocker.patch.object(servicer, "Discharge", mock_set_power)
101+
mocker.patch.object(servicer, "SetPowerActive", mock_set_power)
103102
server = MockGrpcServer(servicer, port=57809)
104103
await server.start()
105104

0 commit comments

Comments
 (0)