2
2
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
3
3
4
4
"""Dispatch API client for Python."""
5
- from dataclasses import dataclass
6
5
from datetime import datetime
7
6
from typing import Awaitable
8
7
11
10
12
11
# pylint: disable=no-name-in-module
13
12
from frequenz .api .dispatch .v1 .dispatch_pb2 import (
14
- ComponentSelector ,
15
13
DispatchFilter ,
16
14
DispatchList ,
17
15
DispatchListRequest ,
18
16
)
19
17
from frequenz .api .dispatch .v1 .dispatch_pb2 import (
20
18
TimeIntervalFilter as PBTimeIntervalFilter ,
21
19
)
22
- from frequenz .client .common .microgrid .components .components import (
23
- ComponentCategory ,
24
- _component_category_to_protobuf ,
25
- )
26
20
from google .protobuf .timestamp_pb2 import Timestamp
27
21
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
+ )
43
28
44
- end_to : datetime
45
- """Filter by end_time < end_to."""
29
+ # pylint: enable=no-name-in-module
46
30
47
31
48
32
class Client :
@@ -62,11 +46,11 @@ def __init__(self, grpc_channel: grpc.aio.Channel, svc_addr: str) -> None:
62
46
async def list (
63
47
self ,
64
48
microgrid_id : int ,
65
- component_selectors : list [list [ int ] | ComponentCategory ] | None = None ,
49
+ component_selectors : list [ComponentSelector ] | None = None ,
66
50
interval_filter : TimeIntervalFilter | None = None ,
67
51
is_active : bool | None = None ,
68
52
is_dry_run : bool | None = None ,
69
- ) -> DispatchList :
53
+ ) -> list [ Dispatch ] :
70
54
"""List dispatches.
71
55
72
56
Args:
@@ -96,16 +80,8 @@ def to_timestamp(dt: datetime) -> Timestamp:
96
80
97
81
selectors = []
98
82
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 ))
109
85
110
86
filters = DispatchFilter (
111
87
selectors = selectors ,
@@ -117,4 +93,4 @@ def to_timestamp(dt: datetime) -> Timestamp:
117
93
118
94
response : Awaitable [DispatchList ]
119
95
response = self ._stub .ListMicrogridDispatches (request ) # type: ignore
120
- return await response
96
+ return list ( map ( Dispatch . from_protobuf , ( await response ). dispatches ))
0 commit comments