frequenz-client-microgrid 0.4.0__tar.gz → 0.5.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {frequenz-client-microgrid-0.4.0/src/frequenz_client_microgrid.egg-info → frequenz-client-microgrid-0.5.1}/PKG-INFO +1 -1
- frequenz-client-microgrid-0.5.1/RELEASE_NOTES.md +5 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1}/pyproject.toml +33 -22
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1}/src/frequenz/client/microgrid/__init__.py +4 -2
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1}/src/frequenz/client/microgrid/_client.py +94 -57
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1}/src/frequenz/client/microgrid/_component.py +24 -21
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1}/src/frequenz/client/microgrid/_component_data.py +13 -13
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1}/src/frequenz/client/microgrid/_component_error.py +36 -36
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1}/src/frequenz/client/microgrid/_component_states.py +54 -56
- frequenz-client-microgrid-0.5.1/src/frequenz/client/microgrid/_exception.py +50 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1/src/frequenz_client_microgrid.egg-info}/PKG-INFO +1 -1
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1}/src/frequenz_client_microgrid.egg-info/requires.txt +23 -21
- frequenz-client-microgrid-0.4.0/RELEASE_NOTES.md +0 -60
- frequenz-client-microgrid-0.4.0/src/frequenz/client/microgrid/_exception.py +0 -582
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1}/LICENSE +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1}/MANIFEST.in +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1}/README.md +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1}/setup.cfg +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1}/src/frequenz/client/microgrid/_connection.py +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1}/src/frequenz/client/microgrid/_constants.py +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1}/src/frequenz/client/microgrid/_metadata.py +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1}/src/frequenz/client/microgrid/conftest.py +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1}/src/frequenz/client/microgrid/py.typed +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1}/src/frequenz_client_microgrid.egg-info/SOURCES.txt +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1}/src/frequenz_client_microgrid.egg-info/dependency_links.txt +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.1}/src/frequenz_client_microgrid.egg-info/top_level.txt +0 -0
|
@@ -2,7 +2,11 @@
|
|
|
2
2
|
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
|
|
3
3
|
|
|
4
4
|
[build-system]
|
|
5
|
-
requires = [
|
|
5
|
+
requires = [
|
|
6
|
+
"setuptools == 68.1.0",
|
|
7
|
+
"setuptools_scm[toml] == 7.1.0",
|
|
8
|
+
"frequenz-repo-config[lib] == 0.9.1",
|
|
9
|
+
]
|
|
6
10
|
build-backend = "setuptools.build_meta"
|
|
7
11
|
|
|
8
12
|
[project]
|
|
@@ -32,10 +36,11 @@ classifiers = [
|
|
|
32
36
|
]
|
|
33
37
|
requires-python = ">= 3.11, < 4"
|
|
34
38
|
dependencies = [
|
|
35
|
-
"
|
|
39
|
+
"frequenz-api-microgrid >= 0.15.3, < 0.16.0",
|
|
36
40
|
"frequenz-channels >= 1.0.0-rc1, < 2.0.0",
|
|
37
|
-
"frequenz-client-base
|
|
38
|
-
"
|
|
41
|
+
"frequenz-client-base >= 0.6.0, < 0.7",
|
|
42
|
+
"grpcio >= 1.54.2, < 2",
|
|
43
|
+
"protobuf >= 4.21.6, < 6",
|
|
39
44
|
"timezonefinder >= 6.2.0, < 7",
|
|
40
45
|
"typing-extensions >= 4.5.0, < 5",
|
|
41
46
|
]
|
|
@@ -47,42 +52,43 @@ email = "floss@frequenz.com"
|
|
|
47
52
|
|
|
48
53
|
[project.optional-dependencies]
|
|
49
54
|
dev-flake8 = [
|
|
50
|
-
"flake8 == 7.
|
|
55
|
+
"flake8 == 7.1.1",
|
|
51
56
|
"flake8-docstrings == 1.7.0",
|
|
52
57
|
"flake8-pyproject == 1.2.3", # For reading the flake8 config from pyproject.toml
|
|
53
|
-
"pydoclint == 0.
|
|
58
|
+
"pydoclint == 0.5.6",
|
|
54
59
|
"pydocstyle == 6.3.0",
|
|
55
60
|
]
|
|
56
|
-
dev-formatting = ["black == 24.
|
|
61
|
+
dev-formatting = ["black == 24.8.0", "isort == 5.13.2"]
|
|
57
62
|
dev-mkdocs = [
|
|
58
|
-
"black == 24.
|
|
59
|
-
"Markdown==3.
|
|
60
|
-
"mike == 2.
|
|
63
|
+
"black == 24.8.0",
|
|
64
|
+
"Markdown==3.7",
|
|
65
|
+
"mike == 2.1.3",
|
|
61
66
|
"mkdocs-gen-files == 0.5.0",
|
|
62
67
|
"mkdocs-literate-nav == 0.6.1",
|
|
63
68
|
"mkdocs-macros-plugin == 1.0.5",
|
|
64
|
-
"mkdocs-material == 9.5.
|
|
65
|
-
"mkdocstrings[python] == 0.
|
|
66
|
-
"
|
|
69
|
+
"mkdocs-material == 9.5.34",
|
|
70
|
+
"mkdocstrings[python] == 0.26.0",
|
|
71
|
+
"mkdocstrings-python == 1.10.9",
|
|
72
|
+
"frequenz-repo-config[lib] == 0.10.0",
|
|
67
73
|
]
|
|
68
74
|
dev-mypy = [
|
|
69
|
-
"mypy == 1.
|
|
70
|
-
"types-Markdown == 3.
|
|
75
|
+
"mypy == 1.11.2",
|
|
76
|
+
"types-Markdown == 3.7.0.20240822",
|
|
71
77
|
# For checking the noxfile, docs/ script, and tests
|
|
72
78
|
"frequenz-client-microgrid[dev-mkdocs,dev-noxfile,dev-pytest]",
|
|
73
79
|
]
|
|
74
|
-
dev-noxfile = ["nox == 2024.4.15", "frequenz-repo-config[lib] == 0.
|
|
80
|
+
dev-noxfile = ["nox == 2024.4.15", "frequenz-repo-config[lib] == 0.10.0"]
|
|
75
81
|
dev-pylint = [
|
|
76
|
-
"pylint == 3.
|
|
82
|
+
"pylint == 3.2.7",
|
|
77
83
|
# For checking the noxfile, docs/ script, and tests
|
|
78
84
|
"frequenz-client-microgrid[dev-mkdocs,dev-noxfile,dev-pytest]",
|
|
79
85
|
]
|
|
80
86
|
dev-pytest = [
|
|
81
|
-
"pytest == 8.
|
|
82
|
-
"frequenz-repo-config[extra-lint-examples] == 0.
|
|
83
|
-
"pytest-mock == 3.
|
|
84
|
-
"pytest-asyncio == 0.
|
|
85
|
-
"async-solipsism == 0.
|
|
87
|
+
"pytest == 8.3.2",
|
|
88
|
+
"frequenz-repo-config[extra-lint-examples] == 0.10.0",
|
|
89
|
+
"pytest-mock == 3.14.0",
|
|
90
|
+
"pytest-asyncio == 0.24.0",
|
|
91
|
+
"async-solipsism == 0.7",
|
|
86
92
|
]
|
|
87
93
|
dev = [
|
|
88
94
|
"frequenz-client-microgrid[dev-mkdocs,dev-flake8,dev-formatting,dev-mkdocs,dev-mypy,dev-noxfile,dev-pylint,dev-pytest]",
|
|
@@ -138,8 +144,13 @@ disable = [
|
|
|
138
144
|
# pylint's unsubscriptable check is buggy and is not needed because
|
|
139
145
|
# it is a type-check, for which we already have mypy.
|
|
140
146
|
"unsubscriptable-object",
|
|
147
|
+
# Checked by mypy
|
|
148
|
+
"no-member",
|
|
149
|
+
"no-name-in-module",
|
|
141
150
|
# Checked by flake8
|
|
151
|
+
"f-string-without-interpolation",
|
|
142
152
|
"line-too-long",
|
|
153
|
+
"missing-function-docstring",
|
|
143
154
|
"redefined-outer-name",
|
|
144
155
|
"unnecessary-lambda-assignment",
|
|
145
156
|
"unused-import",
|
|
@@ -41,7 +41,8 @@ from ._component_states import (
|
|
|
41
41
|
)
|
|
42
42
|
from ._connection import Connection
|
|
43
43
|
from ._exception import (
|
|
44
|
-
|
|
44
|
+
ApiClientError,
|
|
45
|
+
ClientNotConnected,
|
|
45
46
|
DataLoss,
|
|
46
47
|
EntityAlreadyExists,
|
|
47
48
|
EntityNotFound,
|
|
@@ -65,12 +66,13 @@ from ._metadata import Location, Metadata
|
|
|
65
66
|
|
|
66
67
|
__all__ = [
|
|
67
68
|
"ApiClient",
|
|
69
|
+
"ApiClientError",
|
|
68
70
|
"BatteryComponentState",
|
|
69
71
|
"BatteryData",
|
|
70
72
|
"BatteryError",
|
|
71
73
|
"BatteryErrorCode",
|
|
72
74
|
"BatteryRelayState",
|
|
73
|
-
"
|
|
75
|
+
"ClientNotConnected",
|
|
74
76
|
"Component",
|
|
75
77
|
"ComponentCategory",
|
|
76
78
|
"ComponentData",
|
|
@@ -5,19 +5,17 @@
|
|
|
5
5
|
|
|
6
6
|
import asyncio
|
|
7
7
|
import logging
|
|
8
|
-
from collections.abc import Callable, Iterable, Set
|
|
9
|
-
from
|
|
8
|
+
from collections.abc import AsyncIterator, Awaitable, Callable, Iterable, Set
|
|
9
|
+
from dataclasses import replace
|
|
10
|
+
from typing import Any, TypeVar, cast
|
|
10
11
|
|
|
11
|
-
import
|
|
12
|
-
import
|
|
13
|
-
from
|
|
12
|
+
import grpc.aio
|
|
13
|
+
from frequenz.api.common import components_pb2, metrics_pb2
|
|
14
|
+
from frequenz.api.microgrid import microgrid_pb2, microgrid_pb2_grpc
|
|
14
15
|
from frequenz.channels import Receiver
|
|
15
16
|
from frequenz.client.base import channel, retry, streaming
|
|
16
|
-
from
|
|
17
|
-
from
|
|
18
|
-
components as pb_components,
|
|
19
|
-
)
|
|
20
|
-
from frequenz.microgrid.betterproto.frequenz.api.common import metrics as pb_metrics
|
|
17
|
+
from google.protobuf.empty_pb2 import Empty
|
|
18
|
+
from google.protobuf.timestamp_pb2 import Timestamp
|
|
21
19
|
|
|
22
20
|
from ._component import (
|
|
23
21
|
Component,
|
|
@@ -35,7 +33,7 @@ from ._component_data import (
|
|
|
35
33
|
)
|
|
36
34
|
from ._connection import Connection
|
|
37
35
|
from ._constants import RECEIVER_MAX_SIZE
|
|
38
|
-
from ._exception import
|
|
36
|
+
from ._exception import ApiClientError
|
|
39
37
|
from ._metadata import Location, Metadata
|
|
40
38
|
|
|
41
39
|
DEFAULT_GRPC_CALL_TIMEOUT = 60.0
|
|
@@ -47,6 +45,16 @@ _ComponentDataT = TypeVar("_ComponentDataT", bound=ComponentData)
|
|
|
47
45
|
_logger = logging.getLogger(__name__)
|
|
48
46
|
|
|
49
47
|
|
|
48
|
+
DEFAULT_CHANNEL_OPTIONS = replace(
|
|
49
|
+
channel.ChannelOptions(), ssl=channel.SslOptions(enabled=False)
|
|
50
|
+
)
|
|
51
|
+
"""The default channel options for the microgrid API client.
|
|
52
|
+
|
|
53
|
+
These are the same defaults as the common default options but with SSL disabled, as the
|
|
54
|
+
microgrid API does not use SSL by default.
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
|
|
50
58
|
class ApiClient:
|
|
51
59
|
"""A microgrid API client."""
|
|
52
60
|
|
|
@@ -54,6 +62,7 @@ class ApiClient:
|
|
|
54
62
|
self,
|
|
55
63
|
server_url: str,
|
|
56
64
|
*,
|
|
65
|
+
channel_options: channel.ChannelOptions = DEFAULT_CHANNEL_OPTIONS,
|
|
57
66
|
retry_strategy: retry.Strategy | None = None,
|
|
58
67
|
) -> None:
|
|
59
68
|
"""Initialize the class instance.
|
|
@@ -65,6 +74,8 @@ class ApiClient:
|
|
|
65
74
|
where the `port` should be an int between 0 and 65535 (defaulting to
|
|
66
75
|
9090) and `ssl` should be a boolean (defaulting to `false`).
|
|
67
76
|
For example: `grpc://localhost:1090?ssl=true`.
|
|
77
|
+
channel_options: The default options use to create the channel when not
|
|
78
|
+
specified in the URL.
|
|
68
79
|
retry_strategy: The retry strategy to use to reconnect when the connection
|
|
69
80
|
to the streaming method is lost. By default a linear backoff strategy
|
|
70
81
|
is used.
|
|
@@ -72,7 +83,9 @@ class ApiClient:
|
|
|
72
83
|
self._server_url = server_url
|
|
73
84
|
"""The location of the microgrid API server as a URL."""
|
|
74
85
|
|
|
75
|
-
self.api =
|
|
86
|
+
self.api = microgrid_pb2_grpc.MicrogridStub(
|
|
87
|
+
channel.parse_grpc_uri(server_url, defaults=channel_options)
|
|
88
|
+
)
|
|
76
89
|
"""The gRPC stub for the microgrid API."""
|
|
77
90
|
|
|
78
91
|
self._broadcasters: dict[int, streaming.GrpcStreamBroadcaster[Any, Any]] = {}
|
|
@@ -90,25 +103,30 @@ class ApiClient:
|
|
|
90
103
|
Iterator whose elements are all the components in the microgrid.
|
|
91
104
|
|
|
92
105
|
Raises:
|
|
93
|
-
|
|
106
|
+
ApiClientError: If the are any errors communicating with the Microgrid API,
|
|
94
107
|
most likely a subclass of
|
|
95
108
|
[GrpcError][frequenz.client.microgrid.GrpcError].
|
|
96
109
|
"""
|
|
97
110
|
try:
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
111
|
+
# grpc.aio is missing types and mypy thinks this is not awaitable,
|
|
112
|
+
# but it is
|
|
113
|
+
component_list = await cast(
|
|
114
|
+
Awaitable[microgrid_pb2.ComponentList],
|
|
115
|
+
self.api.ListComponents(
|
|
116
|
+
microgrid_pb2.ComponentFilter(),
|
|
117
|
+
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
|
|
118
|
+
),
|
|
101
119
|
)
|
|
102
|
-
except
|
|
103
|
-
raise
|
|
120
|
+
except grpc.aio.AioRpcError as grpc_error:
|
|
121
|
+
raise ApiClientError.from_grpc_error(
|
|
104
122
|
server_url=self._server_url,
|
|
105
|
-
operation="
|
|
123
|
+
operation="ListComponents",
|
|
106
124
|
grpc_error=grpc_error,
|
|
107
125
|
) from grpc_error
|
|
108
126
|
|
|
109
127
|
components_only = filter(
|
|
110
128
|
lambda c: c.category
|
|
111
|
-
is not
|
|
129
|
+
is not components_pb2.ComponentCategory.COMPONENT_CATEGORY_SENSOR,
|
|
112
130
|
component_list.components,
|
|
113
131
|
)
|
|
114
132
|
result: Iterable[Component] = map(
|
|
@@ -132,13 +150,16 @@ class ApiClient:
|
|
|
132
150
|
Returns:
|
|
133
151
|
the microgrid metadata.
|
|
134
152
|
"""
|
|
135
|
-
microgrid_metadata:
|
|
153
|
+
microgrid_metadata: microgrid_pb2.MicrogridMetadata | None = None
|
|
136
154
|
try:
|
|
137
|
-
microgrid_metadata = await
|
|
138
|
-
|
|
139
|
-
|
|
155
|
+
microgrid_metadata = await cast(
|
|
156
|
+
Awaitable[microgrid_pb2.MicrogridMetadata],
|
|
157
|
+
self.api.GetMicrogridMetadata(
|
|
158
|
+
Empty(),
|
|
159
|
+
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
|
|
160
|
+
),
|
|
140
161
|
)
|
|
141
|
-
except
|
|
162
|
+
except grpc.aio.AioRpcError:
|
|
142
163
|
_logger.exception("The microgrid metadata is not available.")
|
|
143
164
|
|
|
144
165
|
if not microgrid_metadata:
|
|
@@ -170,25 +191,28 @@ class ApiClient:
|
|
|
170
191
|
Microgrid connections matching the provided start and end filters.
|
|
171
192
|
|
|
172
193
|
Raises:
|
|
173
|
-
|
|
194
|
+
ApiClientError: If the are any errors communicating with the Microgrid API,
|
|
174
195
|
most likely a subclass of
|
|
175
196
|
[GrpcError][frequenz.client.microgrid.GrpcError].
|
|
176
197
|
"""
|
|
177
|
-
connection_filter =
|
|
178
|
-
starts=list(starts), ends=list(ends)
|
|
179
|
-
)
|
|
198
|
+
connection_filter = microgrid_pb2.ConnectionFilter(starts=starts, ends=ends)
|
|
180
199
|
try:
|
|
181
200
|
valid_components, all_connections = await asyncio.gather(
|
|
182
201
|
self.components(),
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
202
|
+
# grpc.aio is missing types and mypy thinks this is not
|
|
203
|
+
# awaitable, but it is
|
|
204
|
+
cast(
|
|
205
|
+
Awaitable[microgrid_pb2.ConnectionList],
|
|
206
|
+
self.api.ListConnections(
|
|
207
|
+
connection_filter,
|
|
208
|
+
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
|
|
209
|
+
),
|
|
186
210
|
),
|
|
187
211
|
)
|
|
188
|
-
except
|
|
189
|
-
raise
|
|
212
|
+
except grpc.aio.AioRpcError as grpc_error:
|
|
213
|
+
raise ApiClientError.from_grpc_error(
|
|
190
214
|
server_url=self._server_url,
|
|
191
|
-
operation="
|
|
215
|
+
operation="ListConnections",
|
|
192
216
|
grpc_error=grpc_error,
|
|
193
217
|
) from grpc_error
|
|
194
218
|
# Filter out the components filtered in `components` method.
|
|
@@ -212,7 +236,7 @@ class ApiClient:
|
|
|
212
236
|
*,
|
|
213
237
|
component_id: int,
|
|
214
238
|
expected_category: ComponentCategory,
|
|
215
|
-
transform: Callable[[
|
|
239
|
+
transform: Callable[[microgrid_pb2.ComponentData], _ComponentDataT],
|
|
216
240
|
maxsize: int,
|
|
217
241
|
) -> Receiver[_ComponentDataT]:
|
|
218
242
|
"""Return a new broadcaster receiver for a given `component_id`.
|
|
@@ -239,8 +263,15 @@ class ApiClient:
|
|
|
239
263
|
if broadcaster is None:
|
|
240
264
|
broadcaster = streaming.GrpcStreamBroadcaster(
|
|
241
265
|
f"raw-component-data-{component_id}",
|
|
242
|
-
|
|
243
|
-
|
|
266
|
+
# We need to cast here because grpc says StreamComponentData is
|
|
267
|
+
# a grpc.CallIterator[microgrid_pb2.ComponentData] which is not an
|
|
268
|
+
# AsyncIterator, but it is a grpc.aio.UnaryStreamCall[...,
|
|
269
|
+
# microgrid_pb2.ComponentData], which it is.
|
|
270
|
+
lambda: cast(
|
|
271
|
+
AsyncIterator[microgrid_pb2.ComponentData],
|
|
272
|
+
self.api.StreamComponentData(
|
|
273
|
+
microgrid_pb2.ComponentIdParam(id=component_id)
|
|
274
|
+
),
|
|
244
275
|
),
|
|
245
276
|
transform,
|
|
246
277
|
retry_strategy=self._retry_strategy,
|
|
@@ -389,21 +420,24 @@ class ApiClient:
|
|
|
389
420
|
power_w: power to set for the component.
|
|
390
421
|
|
|
391
422
|
Raises:
|
|
392
|
-
|
|
423
|
+
ApiClientError: If the are any errors communicating with the Microgrid API,
|
|
393
424
|
most likely a subclass of
|
|
394
425
|
[GrpcError][frequenz.client.microgrid.GrpcError].
|
|
395
426
|
"""
|
|
396
427
|
try:
|
|
397
|
-
await
|
|
398
|
-
|
|
399
|
-
|
|
428
|
+
await cast(
|
|
429
|
+
Awaitable[Empty],
|
|
430
|
+
self.api.SetPowerActive(
|
|
431
|
+
microgrid_pb2.SetPowerActiveParam(
|
|
432
|
+
component_id=component_id, power=power_w
|
|
433
|
+
),
|
|
434
|
+
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
|
|
400
435
|
),
|
|
401
|
-
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
|
|
402
436
|
)
|
|
403
|
-
except
|
|
404
|
-
raise
|
|
437
|
+
except grpc.aio.AioRpcError as grpc_error:
|
|
438
|
+
raise ApiClientError.from_grpc_error(
|
|
405
439
|
server_url=self._server_url,
|
|
406
|
-
operation="
|
|
440
|
+
operation="SetPowerActive",
|
|
407
441
|
grpc_error=grpc_error,
|
|
408
442
|
) from grpc_error
|
|
409
443
|
|
|
@@ -423,7 +457,7 @@ class ApiClient:
|
|
|
423
457
|
Raises:
|
|
424
458
|
ValueError: when upper bound is less than 0, or when lower bound is
|
|
425
459
|
greater than 0.
|
|
426
|
-
|
|
460
|
+
ApiClientError: If the are any errors communicating with the Microgrid API,
|
|
427
461
|
most likely a subclass of
|
|
428
462
|
[GrpcError][frequenz.client.microgrid.GrpcError].
|
|
429
463
|
"""
|
|
@@ -433,20 +467,23 @@ class ApiClient:
|
|
|
433
467
|
raise ValueError(f"Lower bound {lower} must be less than or equal to 0.")
|
|
434
468
|
|
|
435
469
|
target_metric = (
|
|
436
|
-
|
|
470
|
+
microgrid_pb2.SetBoundsParam.TargetMetric.TARGET_METRIC_POWER_ACTIVE
|
|
437
471
|
)
|
|
438
472
|
try:
|
|
439
|
-
await
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
473
|
+
await cast(
|
|
474
|
+
Awaitable[Timestamp],
|
|
475
|
+
self.api.AddInclusionBounds(
|
|
476
|
+
microgrid_pb2.SetBoundsParam(
|
|
477
|
+
component_id=component_id,
|
|
478
|
+
target_metric=target_metric,
|
|
479
|
+
bounds=metrics_pb2.Bounds(lower=lower, upper=upper),
|
|
480
|
+
),
|
|
481
|
+
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
|
|
444
482
|
),
|
|
445
|
-
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
|
|
446
483
|
)
|
|
447
|
-
except
|
|
448
|
-
raise
|
|
484
|
+
except grpc.aio.AioRpcError as grpc_error:
|
|
485
|
+
raise ApiClientError.from_grpc_error(
|
|
449
486
|
server_url=self._server_url,
|
|
450
|
-
operation="
|
|
487
|
+
operation="AddInclusionBounds",
|
|
451
488
|
grpc_error=grpc_error,
|
|
452
489
|
) from grpc_error
|
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
from dataclasses import dataclass
|
|
7
7
|
from enum import Enum
|
|
8
8
|
|
|
9
|
-
from frequenz.
|
|
10
|
-
from frequenz.
|
|
9
|
+
from frequenz.api.common import components_pb2
|
|
10
|
+
from frequenz.api.microgrid import grid_pb2, inverter_pb2
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
class ComponentType(Enum):
|
|
@@ -17,22 +17,22 @@ class ComponentType(Enum):
|
|
|
17
17
|
class InverterType(ComponentType):
|
|
18
18
|
"""Enum representing inverter types."""
|
|
19
19
|
|
|
20
|
-
NONE =
|
|
20
|
+
NONE = inverter_pb2.Type.TYPE_UNSPECIFIED
|
|
21
21
|
"""Unspecified inverter type."""
|
|
22
22
|
|
|
23
|
-
BATTERY =
|
|
23
|
+
BATTERY = inverter_pb2.Type.TYPE_BATTERY
|
|
24
24
|
"""Battery inverter."""
|
|
25
25
|
|
|
26
|
-
SOLAR =
|
|
26
|
+
SOLAR = inverter_pb2.Type.TYPE_SOLAR
|
|
27
27
|
"""Solar inverter."""
|
|
28
28
|
|
|
29
|
-
HYBRID =
|
|
29
|
+
HYBRID = inverter_pb2.Type.TYPE_HYBRID
|
|
30
30
|
"""Hybrid inverter."""
|
|
31
31
|
|
|
32
32
|
|
|
33
33
|
def component_type_from_protobuf(
|
|
34
|
-
component_category:
|
|
35
|
-
component_metadata:
|
|
34
|
+
component_category: components_pb2.ComponentCategory.ValueType,
|
|
35
|
+
component_metadata: inverter_pb2.Metadata,
|
|
36
36
|
) -> ComponentType | None:
|
|
37
37
|
"""Convert a protobuf InverterType message to Component enum.
|
|
38
38
|
|
|
@@ -48,7 +48,10 @@ def component_type_from_protobuf(
|
|
|
48
48
|
# ComponentType values in the protobuf definition are not unique across categories
|
|
49
49
|
# as of v0.11.0, so we need to check the component category first, before doing any
|
|
50
50
|
# component type checks.
|
|
51
|
-
if
|
|
51
|
+
if (
|
|
52
|
+
component_category
|
|
53
|
+
== components_pb2.ComponentCategory.COMPONENT_CATEGORY_INVERTER
|
|
54
|
+
):
|
|
52
55
|
if not any(int(t.value) == int(component_metadata.type) for t in InverterType):
|
|
53
56
|
return None
|
|
54
57
|
|
|
@@ -60,30 +63,30 @@ def component_type_from_protobuf(
|
|
|
60
63
|
class ComponentCategory(Enum):
|
|
61
64
|
"""Possible types of microgrid component."""
|
|
62
65
|
|
|
63
|
-
NONE =
|
|
66
|
+
NONE = components_pb2.ComponentCategory.COMPONENT_CATEGORY_UNSPECIFIED
|
|
64
67
|
"""Unspecified component category."""
|
|
65
68
|
|
|
66
|
-
GRID =
|
|
69
|
+
GRID = components_pb2.ComponentCategory.COMPONENT_CATEGORY_GRID
|
|
67
70
|
"""Grid component."""
|
|
68
71
|
|
|
69
|
-
METER =
|
|
72
|
+
METER = components_pb2.ComponentCategory.COMPONENT_CATEGORY_METER
|
|
70
73
|
"""Meter component."""
|
|
71
74
|
|
|
72
|
-
INVERTER =
|
|
75
|
+
INVERTER = components_pb2.ComponentCategory.COMPONENT_CATEGORY_INVERTER
|
|
73
76
|
"""Inverter component."""
|
|
74
77
|
|
|
75
|
-
BATTERY =
|
|
78
|
+
BATTERY = components_pb2.ComponentCategory.COMPONENT_CATEGORY_BATTERY
|
|
76
79
|
"""Battery component."""
|
|
77
80
|
|
|
78
|
-
EV_CHARGER =
|
|
81
|
+
EV_CHARGER = components_pb2.ComponentCategory.COMPONENT_CATEGORY_EV_CHARGER
|
|
79
82
|
"""EV charger component."""
|
|
80
83
|
|
|
81
|
-
CHP =
|
|
84
|
+
CHP = components_pb2.ComponentCategory.COMPONENT_CATEGORY_CHP
|
|
82
85
|
"""CHP component."""
|
|
83
86
|
|
|
84
87
|
|
|
85
88
|
def component_category_from_protobuf(
|
|
86
|
-
component_category:
|
|
89
|
+
component_category: components_pb2.ComponentCategory.ValueType,
|
|
87
90
|
) -> ComponentCategory:
|
|
88
91
|
"""Convert a protobuf ComponentCategory message to ComponentCategory enum.
|
|
89
92
|
|
|
@@ -100,7 +103,7 @@ def component_category_from_protobuf(
|
|
|
100
103
|
a valid component category as it does not form part of the
|
|
101
104
|
microgrid itself)
|
|
102
105
|
"""
|
|
103
|
-
if component_category ==
|
|
106
|
+
if component_category == components_pb2.ComponentCategory.COMPONENT_CATEGORY_SENSOR:
|
|
104
107
|
raise ValueError("Cannot create a component from a sensor!")
|
|
105
108
|
|
|
106
109
|
if not any(t.value == component_category for t in ComponentCategory):
|
|
@@ -131,8 +134,8 @@ class GridMetadata(ComponentMetadata):
|
|
|
131
134
|
|
|
132
135
|
|
|
133
136
|
def component_metadata_from_protobuf(
|
|
134
|
-
component_category:
|
|
135
|
-
component_metadata:
|
|
137
|
+
component_category: components_pb2.ComponentCategory.ValueType,
|
|
138
|
+
component_metadata: grid_pb2.Metadata,
|
|
136
139
|
) -> GridMetadata | None:
|
|
137
140
|
"""Convert a protobuf GridMetadata message to GridMetadata class.
|
|
138
141
|
|
|
@@ -145,7 +148,7 @@ def component_metadata_from_protobuf(
|
|
|
145
148
|
Returns:
|
|
146
149
|
GridMetadata instance corresponding to the protobuf message.
|
|
147
150
|
"""
|
|
148
|
-
if component_category ==
|
|
151
|
+
if component_category == components_pb2.ComponentCategory.COMPONENT_CATEGORY_GRID:
|
|
149
152
|
max_current = component_metadata.rated_fuse_current
|
|
150
153
|
fuse = Fuse(max_current)
|
|
151
154
|
return GridMetadata(fuse)
|
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
|
|
6
6
|
from abc import ABC, abstractmethod
|
|
7
7
|
from dataclasses import dataclass, field
|
|
8
|
-
from datetime import datetime
|
|
8
|
+
from datetime import datetime, timezone
|
|
9
9
|
from typing import Self
|
|
10
10
|
|
|
11
|
-
from frequenz.microgrid
|
|
11
|
+
from frequenz.api.microgrid import microgrid_pb2
|
|
12
12
|
|
|
13
13
|
from ._component_error import BatteryError, InverterError
|
|
14
14
|
from ._component_states import (
|
|
@@ -35,10 +35,10 @@ class ComponentData(ABC):
|
|
|
35
35
|
# data from a protobuf message. The whole protobuf message is stored as the `raw`
|
|
36
36
|
# attribute. When `ComponentData` is not instantiated from a protobuf message,
|
|
37
37
|
# i.e. using the constructor, `raw` will be set to `None`.
|
|
38
|
-
raw:
|
|
38
|
+
raw: microgrid_pb2.ComponentData | None = field(default=None, init=False)
|
|
39
39
|
"""Raw component data as decoded from the wire."""
|
|
40
40
|
|
|
41
|
-
def _set_raw(self, raw:
|
|
41
|
+
def _set_raw(self, raw: microgrid_pb2.ComponentData) -> None:
|
|
42
42
|
"""Store raw protobuf message.
|
|
43
43
|
|
|
44
44
|
It is preferred to keep the dataclasses immutable (frozen) and make the `raw`
|
|
@@ -52,7 +52,7 @@ class ComponentData(ABC):
|
|
|
52
52
|
|
|
53
53
|
@classmethod
|
|
54
54
|
@abstractmethod
|
|
55
|
-
def from_proto(cls, raw:
|
|
55
|
+
def from_proto(cls, raw: microgrid_pb2.ComponentData) -> Self:
|
|
56
56
|
"""Create ComponentData from a protobuf message.
|
|
57
57
|
|
|
58
58
|
Args:
|
|
@@ -119,7 +119,7 @@ class MeterData(ComponentData):
|
|
|
119
119
|
"""The AC power frequency in Hertz (Hz)."""
|
|
120
120
|
|
|
121
121
|
@classmethod
|
|
122
|
-
def from_proto(cls, raw:
|
|
122
|
+
def from_proto(cls, raw: microgrid_pb2.ComponentData) -> Self:
|
|
123
123
|
"""Create MeterData from a protobuf message.
|
|
124
124
|
|
|
125
125
|
Args:
|
|
@@ -130,7 +130,7 @@ class MeterData(ComponentData):
|
|
|
130
130
|
"""
|
|
131
131
|
meter_data = cls(
|
|
132
132
|
component_id=raw.id,
|
|
133
|
-
timestamp=raw.ts,
|
|
133
|
+
timestamp=raw.ts.ToDatetime(tzinfo=timezone.utc),
|
|
134
134
|
active_power=raw.meter.data.ac.power_active.value,
|
|
135
135
|
active_power_per_phase=(
|
|
136
136
|
raw.meter.data.ac.phase_1.power_active.value,
|
|
@@ -236,7 +236,7 @@ class BatteryData(ComponentData): # pylint: disable=too-many-instance-attribute
|
|
|
236
236
|
"""List of errors in protobuf struct."""
|
|
237
237
|
|
|
238
238
|
@classmethod
|
|
239
|
-
def from_proto(cls, raw:
|
|
239
|
+
def from_proto(cls, raw: microgrid_pb2.ComponentData) -> Self:
|
|
240
240
|
"""Create BatteryData from a protobuf message.
|
|
241
241
|
|
|
242
242
|
Args:
|
|
@@ -248,7 +248,7 @@ class BatteryData(ComponentData): # pylint: disable=too-many-instance-attribute
|
|
|
248
248
|
raw_power = raw.battery.data.dc.power
|
|
249
249
|
battery_data = cls(
|
|
250
250
|
component_id=raw.id,
|
|
251
|
-
timestamp=raw.ts,
|
|
251
|
+
timestamp=raw.ts.ToDatetime(tzinfo=timezone.utc),
|
|
252
252
|
soc=raw.battery.data.soc.avg,
|
|
253
253
|
soc_lower_bound=raw.battery.data.soc.system_inclusion_bounds.lower,
|
|
254
254
|
soc_upper_bound=raw.battery.data.soc.system_inclusion_bounds.upper,
|
|
@@ -374,7 +374,7 @@ class InverterData(ComponentData): # pylint: disable=too-many-instance-attribut
|
|
|
374
374
|
"""List of errors from the component."""
|
|
375
375
|
|
|
376
376
|
@classmethod
|
|
377
|
-
def from_proto(cls, raw:
|
|
377
|
+
def from_proto(cls, raw: microgrid_pb2.ComponentData) -> Self:
|
|
378
378
|
"""Create InverterData from a protobuf message.
|
|
379
379
|
|
|
380
380
|
Args:
|
|
@@ -386,7 +386,7 @@ class InverterData(ComponentData): # pylint: disable=too-many-instance-attribut
|
|
|
386
386
|
raw_power = raw.inverter.data.ac.power_active
|
|
387
387
|
inverter_data = cls(
|
|
388
388
|
component_id=raw.id,
|
|
389
|
-
timestamp=raw.ts,
|
|
389
|
+
timestamp=raw.ts.ToDatetime(tzinfo=timezone.utc),
|
|
390
390
|
active_power=raw.inverter.data.ac.power_active.value,
|
|
391
391
|
active_power_per_phase=(
|
|
392
392
|
raw.inverter.data.ac.phase_1.power_active.value,
|
|
@@ -530,7 +530,7 @@ class EVChargerData(ComponentData): # pylint: disable=too-many-instance-attribu
|
|
|
530
530
|
"""The state of the ev charger."""
|
|
531
531
|
|
|
532
532
|
@classmethod
|
|
533
|
-
def from_proto(cls, raw:
|
|
533
|
+
def from_proto(cls, raw: microgrid_pb2.ComponentData) -> Self:
|
|
534
534
|
"""Create EVChargerData from a protobuf message.
|
|
535
535
|
|
|
536
536
|
Args:
|
|
@@ -542,7 +542,7 @@ class EVChargerData(ComponentData): # pylint: disable=too-many-instance-attribu
|
|
|
542
542
|
raw_power = raw.ev_charger.data.ac.power_active
|
|
543
543
|
ev_charger_data = cls(
|
|
544
544
|
component_id=raw.id,
|
|
545
|
-
timestamp=raw.ts,
|
|
545
|
+
timestamp=raw.ts.ToDatetime(tzinfo=timezone.utc),
|
|
546
546
|
active_power=raw_power.value,
|
|
547
547
|
active_power_per_phase=(
|
|
548
548
|
raw.ev_charger.data.ac.phase_1.power_active.value,
|