frequenz-client-microgrid 0.4.0__tar.gz → 0.5.0__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.0}/PKG-INFO +1 -1
- frequenz-client-microgrid-0.5.0/RELEASE_NOTES.md +10 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.0}/pyproject.toml +33 -22
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.0}/src/frequenz/client/microgrid/__init__.py +4 -2
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.0}/src/frequenz/client/microgrid/_client.py +78 -57
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.0}/src/frequenz/client/microgrid/_component.py +24 -21
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.0}/src/frequenz/client/microgrid/_component_data.py +13 -13
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.0}/src/frequenz/client/microgrid/_component_error.py +36 -36
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.0}/src/frequenz/client/microgrid/_component_states.py +54 -56
- frequenz-client-microgrid-0.5.0/src/frequenz/client/microgrid/_exception.py +50 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.0/src/frequenz_client_microgrid.egg-info}/PKG-INFO +1 -1
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.0}/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.0}/LICENSE +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.0}/MANIFEST.in +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.0}/README.md +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.0}/setup.cfg +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.0}/src/frequenz/client/microgrid/_connection.py +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.0}/src/frequenz/client/microgrid/_constants.py +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.0}/src/frequenz/client/microgrid/_metadata.py +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.0}/src/frequenz/client/microgrid/conftest.py +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.0}/src/frequenz/client/microgrid/py.typed +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.0}/src/frequenz_client_microgrid.egg-info/SOURCES.txt +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.0}/src/frequenz_client_microgrid.egg-info/dependency_links.txt +0 -0
- {frequenz-client-microgrid-0.4.0 → frequenz-client-microgrid-0.5.0}/src/frequenz_client_microgrid.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Frequenz Microgrid API Client Release Notes
|
|
2
|
+
|
|
3
|
+
## Upgrading
|
|
4
|
+
|
|
5
|
+
- This release stops using `betterproto` and `grpclib` as backend for gRPC and goes back to using Google's `grpcio` and `protobuf`.
|
|
6
|
+
|
|
7
|
+
If your code was using `betterproto` and `grpclib`, it now needs to be ported to use `grpcio` and `protobuf` too. Remember to also remove any `betterproto` and `grpclib` dependencies from your project.
|
|
8
|
+
|
|
9
|
+
- We are now using base-client v0.6.0.
|
|
10
|
+
|
|
@@ -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.33",
|
|
70
|
+
"mkdocstrings[python] == 0.25.2",
|
|
71
|
+
"mkdocstrings-python == 1.10.8",
|
|
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.6",
|
|
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,16 @@
|
|
|
5
5
|
|
|
6
6
|
import asyncio
|
|
7
7
|
import logging
|
|
8
|
-
from collections.abc import Callable, Iterable, Set
|
|
9
|
-
from typing import Any, TypeVar
|
|
8
|
+
from collections.abc import AsyncIterator, Awaitable, Callable, Iterable, Set
|
|
9
|
+
from typing import Any, TypeVar, cast
|
|
10
10
|
|
|
11
|
-
import
|
|
12
|
-
import
|
|
13
|
-
from
|
|
11
|
+
import grpc.aio
|
|
12
|
+
from frequenz.api.common import components_pb2, metrics_pb2
|
|
13
|
+
from frequenz.api.microgrid import microgrid_pb2, microgrid_pb2_grpc
|
|
14
14
|
from frequenz.channels import Receiver
|
|
15
15
|
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
|
|
16
|
+
from google.protobuf.empty_pb2 import Empty
|
|
17
|
+
from google.protobuf.timestamp_pb2 import Timestamp
|
|
21
18
|
|
|
22
19
|
from ._component import (
|
|
23
20
|
Component,
|
|
@@ -35,7 +32,7 @@ from ._component_data import (
|
|
|
35
32
|
)
|
|
36
33
|
from ._connection import Connection
|
|
37
34
|
from ._constants import RECEIVER_MAX_SIZE
|
|
38
|
-
from ._exception import
|
|
35
|
+
from ._exception import ApiClientError
|
|
39
36
|
from ._metadata import Location, Metadata
|
|
40
37
|
|
|
41
38
|
DEFAULT_GRPC_CALL_TIMEOUT = 60.0
|
|
@@ -72,7 +69,7 @@ class ApiClient:
|
|
|
72
69
|
self._server_url = server_url
|
|
73
70
|
"""The location of the microgrid API server as a URL."""
|
|
74
71
|
|
|
75
|
-
self.api =
|
|
72
|
+
self.api = microgrid_pb2_grpc.MicrogridStub(channel.parse_grpc_uri(server_url))
|
|
76
73
|
"""The gRPC stub for the microgrid API."""
|
|
77
74
|
|
|
78
75
|
self._broadcasters: dict[int, streaming.GrpcStreamBroadcaster[Any, Any]] = {}
|
|
@@ -90,25 +87,30 @@ class ApiClient:
|
|
|
90
87
|
Iterator whose elements are all the components in the microgrid.
|
|
91
88
|
|
|
92
89
|
Raises:
|
|
93
|
-
|
|
90
|
+
ApiClientError: If the are any errors communicating with the Microgrid API,
|
|
94
91
|
most likely a subclass of
|
|
95
92
|
[GrpcError][frequenz.client.microgrid.GrpcError].
|
|
96
93
|
"""
|
|
97
94
|
try:
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
95
|
+
# grpc.aio is missing types and mypy thinks this is not awaitable,
|
|
96
|
+
# but it is
|
|
97
|
+
component_list = await cast(
|
|
98
|
+
Awaitable[microgrid_pb2.ComponentList],
|
|
99
|
+
self.api.ListComponents(
|
|
100
|
+
microgrid_pb2.ComponentFilter(),
|
|
101
|
+
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
|
|
102
|
+
),
|
|
101
103
|
)
|
|
102
|
-
except
|
|
103
|
-
raise
|
|
104
|
+
except grpc.aio.AioRpcError as grpc_error:
|
|
105
|
+
raise ApiClientError.from_grpc_error(
|
|
104
106
|
server_url=self._server_url,
|
|
105
|
-
operation="
|
|
107
|
+
operation="ListComponents",
|
|
106
108
|
grpc_error=grpc_error,
|
|
107
109
|
) from grpc_error
|
|
108
110
|
|
|
109
111
|
components_only = filter(
|
|
110
112
|
lambda c: c.category
|
|
111
|
-
is not
|
|
113
|
+
is not components_pb2.ComponentCategory.COMPONENT_CATEGORY_SENSOR,
|
|
112
114
|
component_list.components,
|
|
113
115
|
)
|
|
114
116
|
result: Iterable[Component] = map(
|
|
@@ -132,13 +134,16 @@ class ApiClient:
|
|
|
132
134
|
Returns:
|
|
133
135
|
the microgrid metadata.
|
|
134
136
|
"""
|
|
135
|
-
microgrid_metadata:
|
|
137
|
+
microgrid_metadata: microgrid_pb2.MicrogridMetadata | None = None
|
|
136
138
|
try:
|
|
137
|
-
microgrid_metadata = await
|
|
138
|
-
|
|
139
|
-
|
|
139
|
+
microgrid_metadata = await cast(
|
|
140
|
+
Awaitable[microgrid_pb2.MicrogridMetadata],
|
|
141
|
+
self.api.GetMicrogridMetadata(
|
|
142
|
+
Empty(),
|
|
143
|
+
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
|
|
144
|
+
),
|
|
140
145
|
)
|
|
141
|
-
except
|
|
146
|
+
except grpc.aio.AioRpcError:
|
|
142
147
|
_logger.exception("The microgrid metadata is not available.")
|
|
143
148
|
|
|
144
149
|
if not microgrid_metadata:
|
|
@@ -170,25 +175,28 @@ class ApiClient:
|
|
|
170
175
|
Microgrid connections matching the provided start and end filters.
|
|
171
176
|
|
|
172
177
|
Raises:
|
|
173
|
-
|
|
178
|
+
ApiClientError: If the are any errors communicating with the Microgrid API,
|
|
174
179
|
most likely a subclass of
|
|
175
180
|
[GrpcError][frequenz.client.microgrid.GrpcError].
|
|
176
181
|
"""
|
|
177
|
-
connection_filter =
|
|
178
|
-
starts=list(starts), ends=list(ends)
|
|
179
|
-
)
|
|
182
|
+
connection_filter = microgrid_pb2.ConnectionFilter(starts=starts, ends=ends)
|
|
180
183
|
try:
|
|
181
184
|
valid_components, all_connections = await asyncio.gather(
|
|
182
185
|
self.components(),
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
+
# grpc.aio is missing types and mypy thinks this is not
|
|
187
|
+
# awaitable, but it is
|
|
188
|
+
cast(
|
|
189
|
+
Awaitable[microgrid_pb2.ConnectionList],
|
|
190
|
+
self.api.ListConnections(
|
|
191
|
+
connection_filter,
|
|
192
|
+
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
|
|
193
|
+
),
|
|
186
194
|
),
|
|
187
195
|
)
|
|
188
|
-
except
|
|
189
|
-
raise
|
|
196
|
+
except grpc.aio.AioRpcError as grpc_error:
|
|
197
|
+
raise ApiClientError.from_grpc_error(
|
|
190
198
|
server_url=self._server_url,
|
|
191
|
-
operation="
|
|
199
|
+
operation="ListConnections",
|
|
192
200
|
grpc_error=grpc_error,
|
|
193
201
|
) from grpc_error
|
|
194
202
|
# Filter out the components filtered in `components` method.
|
|
@@ -212,7 +220,7 @@ class ApiClient:
|
|
|
212
220
|
*,
|
|
213
221
|
component_id: int,
|
|
214
222
|
expected_category: ComponentCategory,
|
|
215
|
-
transform: Callable[[
|
|
223
|
+
transform: Callable[[microgrid_pb2.ComponentData], _ComponentDataT],
|
|
216
224
|
maxsize: int,
|
|
217
225
|
) -> Receiver[_ComponentDataT]:
|
|
218
226
|
"""Return a new broadcaster receiver for a given `component_id`.
|
|
@@ -239,8 +247,15 @@ class ApiClient:
|
|
|
239
247
|
if broadcaster is None:
|
|
240
248
|
broadcaster = streaming.GrpcStreamBroadcaster(
|
|
241
249
|
f"raw-component-data-{component_id}",
|
|
242
|
-
|
|
243
|
-
|
|
250
|
+
# We need to cast here because grpc says StreamComponentData 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.
|
|
254
|
+
lambda: cast(
|
|
255
|
+
AsyncIterator[microgrid_pb2.ComponentData],
|
|
256
|
+
self.api.StreamComponentData(
|
|
257
|
+
microgrid_pb2.ComponentIdParam(id=component_id)
|
|
258
|
+
),
|
|
244
259
|
),
|
|
245
260
|
transform,
|
|
246
261
|
retry_strategy=self._retry_strategy,
|
|
@@ -389,21 +404,24 @@ class ApiClient:
|
|
|
389
404
|
power_w: power to set for the component.
|
|
390
405
|
|
|
391
406
|
Raises:
|
|
392
|
-
|
|
407
|
+
ApiClientError: If the are any errors communicating with the Microgrid API,
|
|
393
408
|
most likely a subclass of
|
|
394
409
|
[GrpcError][frequenz.client.microgrid.GrpcError].
|
|
395
410
|
"""
|
|
396
411
|
try:
|
|
397
|
-
await
|
|
398
|
-
|
|
399
|
-
|
|
412
|
+
await cast(
|
|
413
|
+
Awaitable[Empty],
|
|
414
|
+
self.api.SetPowerActive(
|
|
415
|
+
microgrid_pb2.SetPowerActiveParam(
|
|
416
|
+
component_id=component_id, power=power_w
|
|
417
|
+
),
|
|
418
|
+
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
|
|
400
419
|
),
|
|
401
|
-
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
|
|
402
420
|
)
|
|
403
|
-
except
|
|
404
|
-
raise
|
|
421
|
+
except grpc.aio.AioRpcError as grpc_error:
|
|
422
|
+
raise ApiClientError.from_grpc_error(
|
|
405
423
|
server_url=self._server_url,
|
|
406
|
-
operation="
|
|
424
|
+
operation="SetPowerActive",
|
|
407
425
|
grpc_error=grpc_error,
|
|
408
426
|
) from grpc_error
|
|
409
427
|
|
|
@@ -423,7 +441,7 @@ class ApiClient:
|
|
|
423
441
|
Raises:
|
|
424
442
|
ValueError: when upper bound is less than 0, or when lower bound is
|
|
425
443
|
greater than 0.
|
|
426
|
-
|
|
444
|
+
ApiClientError: If the are any errors communicating with the Microgrid API,
|
|
427
445
|
most likely a subclass of
|
|
428
446
|
[GrpcError][frequenz.client.microgrid.GrpcError].
|
|
429
447
|
"""
|
|
@@ -433,20 +451,23 @@ class ApiClient:
|
|
|
433
451
|
raise ValueError(f"Lower bound {lower} must be less than or equal to 0.")
|
|
434
452
|
|
|
435
453
|
target_metric = (
|
|
436
|
-
|
|
454
|
+
microgrid_pb2.SetBoundsParam.TargetMetric.TARGET_METRIC_POWER_ACTIVE
|
|
437
455
|
)
|
|
438
456
|
try:
|
|
439
|
-
await
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
457
|
+
await cast(
|
|
458
|
+
Awaitable[Timestamp],
|
|
459
|
+
self.api.AddInclusionBounds(
|
|
460
|
+
microgrid_pb2.SetBoundsParam(
|
|
461
|
+
component_id=component_id,
|
|
462
|
+
target_metric=target_metric,
|
|
463
|
+
bounds=metrics_pb2.Bounds(lower=lower, upper=upper),
|
|
464
|
+
),
|
|
465
|
+
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
|
|
444
466
|
),
|
|
445
|
-
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
|
|
446
467
|
)
|
|
447
|
-
except
|
|
448
|
-
raise
|
|
468
|
+
except grpc.aio.AioRpcError as grpc_error:
|
|
469
|
+
raise ApiClientError.from_grpc_error(
|
|
449
470
|
server_url=self._server_url,
|
|
450
|
-
operation="
|
|
471
|
+
operation="AddInclusionBounds",
|
|
451
472
|
grpc_error=grpc_error,
|
|
452
473
|
) 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,
|