Skip to content

Commit 8b0cf85

Browse files
committed
Add a few dispatch types and conversion tests for them
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent 2fc74c4 commit 8b0cf85

File tree

3 files changed

+515
-37
lines changed

3 files changed

+515
-37
lines changed

src/frequenz/client/dispatch/_client.py

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
33

44
"""Dispatch API client for Python."""
5-
from dataclasses import dataclass
65
from datetime import datetime
76
from typing import Awaitable
87

@@ -11,38 +10,23 @@
1110

1211
# pylint: disable=no-name-in-module
1312
from frequenz.api.dispatch.v1.dispatch_pb2 import (
14-
ComponentSelector,
1513
DispatchFilter,
1614
DispatchList,
1715
DispatchListRequest,
1816
)
1917
from frequenz.api.dispatch.v1.dispatch_pb2 import (
2018
TimeIntervalFilter as PBTimeIntervalFilter,
2119
)
22-
from frequenz.client.common.microgrid.components.components import (
23-
ComponentCategory,
24-
_component_category_to_protobuf,
25-
)
2620
from google.protobuf.timestamp_pb2 import Timestamp
2721

28-
# pylint: enable=no-name-in-module
29-
30-
31-
@dataclass(frozen=True, kw_only=True)
32-
class TimeIntervalFilter:
33-
"""Filter for a time interval."""
34-
35-
start_from: datetime
36-
"""Filter by start_time >= start_from."""
37-
38-
start_to: datetime
39-
"""Filter by start_time < start_to."""
40-
41-
end_from: datetime
42-
"""Filter by end_time >= end_from."""
22+
from ._types import (
23+
ComponentSelector,
24+
Dispatch,
25+
TimeIntervalFilter,
26+
_component_selector_to_protobuf,
27+
)
4328

44-
end_to: datetime
45-
"""Filter by end_time < end_to."""
29+
# pylint: enable=no-name-in-module
4630

4731

4832
class Client:
@@ -62,11 +46,11 @@ def __init__(self, grpc_channel: grpc.aio.Channel, svc_addr: str) -> None:
6246
async def list(
6347
self,
6448
microgrid_id: int,
65-
component_selectors: list[list[int] | ComponentCategory] | None = None,
49+
component_selectors: list[ComponentSelector] | None = None,
6650
interval_filter: TimeIntervalFilter | None = None,
6751
is_active: bool | None = None,
6852
is_dry_run: bool | None = None,
69-
) -> DispatchList:
53+
) -> list[Dispatch]:
7054
"""List dispatches.
7155
7256
Args:
@@ -96,16 +80,8 @@ def to_timestamp(dt: datetime) -> Timestamp:
9680

9781
selectors = []
9882

99-
if component_selectors is not None:
100-
for selector in component_selectors:
101-
proto_selector = ComponentSelector()
102-
if isinstance(selector, list):
103-
proto_selector.component_ids.component_ids.extend(selector)
104-
elif isinstance(selector, ComponentCategory):
105-
proto_selector.component_category = _component_category_to_protobuf(
106-
selector
107-
)
108-
selectors.append(proto_selector)
83+
for selector in component_selectors if component_selectors else []:
84+
selectors.append(_component_selector_to_protobuf(selector))
10985

11086
filters = DispatchFilter(
11187
selectors=selectors,
@@ -117,4 +93,4 @@ def to_timestamp(dt: datetime) -> Timestamp:
11793

11894
response: Awaitable[DispatchList]
11995
response = self._stub.ListMicrogridDispatches(request) # type: ignore
120-
return await response
96+
return list(map(Dispatch.from_protobuf, (await response).dispatches))

0 commit comments

Comments
 (0)