Skip to content

Commit 84f2b70

Browse files
authored
Update to use the microgrid API v0.18 (#1283)
This PR migrates the SDK from the old `frequenz-client-microgrid` using the old microgrid API v0.15 to the new using the API v0.18. The main changes involve updating component models, metrics, and data fetching mechanisms to use the new client library structure.
2 parents cb396b1 + 683bcb2 commit 84f2b70

File tree

78 files changed

+3800
-2629
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+3800
-2629
lines changed

RELEASE_NOTES.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,25 @@
66

77
## Upgrading
88

9-
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
9+
- The SDK now depends on the `frequenz-client-microgrid` v0.18.x series.
10+
11+
* Check the release notes for the client [v0.18].
12+
* There were a lot of changes, so it might be also worth having a quick look at the microgrid API [v0.17][api-v0.17] and [v0.18][api-v0.17] releases.
13+
* Checking out the API common releases [v0.6][common-v0.6], [v0.7][common-v0.7], [v0.8][common-v0.8] might also be worthwhile, at least if you find any errors about renamed or missing types.
14+
* Although many of the changes in lower layer are hidden by the SDK, there are some changes that can't be hidden away. For example the `Metric` enum had some renames, and `Component.component_id` was renamed to `Component.id`. Also, there is a new component class hierarchy.
15+
16+
- `ComponentGraph` methods arguments were renamed to better reflect what they expect.
17+
18+
* The `components()` method now uses `matching_ids` and `matching_types` instead of `component_ids` and `component_categories` respectively. `matching_types` takes types inheriting from `Component` instead of categories, for example `Battery` or `BatteryInverter`.
19+
* The `connections()` methods now take `matching_sources` and `matching_destinations` instead of `start` and `end` respectively. This is to match the new names in `ComponentConnection`.
20+
* All arguments for both methods can now receiver arbitrary iterables instead of `set`s, and can also accept a single value.
21+
22+
[v0.18]: https://github.com/frequenz-floss/frequenz-client-microgrid-python/releases/tag/v0.18.0
23+
[api-v0.17]: https://github.com/frequenz-floss/frequenz-api-microgrid/releases/tag/v0.17.0
24+
[api-v0.18]: https://github.com/frequenz-floss/frequenz-api-microgrid/releases/tag/v0.18.0
25+
[common-v0.6]: https://github.com/frequenz-floss/frequenz-api-common/releases/tag/v0.6.0
26+
[common-v0.7]: https://github.com/frequenz-floss/frequenz-api-common/releases/tag/v0.7.0
27+
[common-v0.8]: https://github.com/frequenz-floss/frequenz-api-common/releases/tag/v0.8.0
1028

1129
## New Features
1230

benchmarks/power_distribution/power_distributor.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from frequenz.channels import Broadcast
1515
from frequenz.client.common.microgrid.components import ComponentId
16-
from frequenz.client.microgrid import Component, ComponentCategory
16+
from frequenz.client.microgrid.component import Battery
1717
from frequenz.quantities import Power
1818

1919
from frequenz.sdk import microgrid
@@ -116,8 +116,7 @@ async def run_test( # pylint: disable=too-many-locals
116116
battery_status_channel = Broadcast[ComponentPoolStatus](name="battery-status")
117117
power_result_channel = Broadcast[Result](name="power-result")
118118
async with PowerDistributingActor(
119-
component_category=ComponentCategory.BATTERY,
120-
component_type=None,
119+
component_type=Battery,
121120
requests_receiver=power_request_channel.new_receiver(),
122121
results_sender=power_result_channel.new_sender(),
123122
component_pool_status_sender=battery_status_channel.new_sender(),
@@ -143,10 +142,10 @@ async def run() -> None:
143142
ResamplerConfig2(resampling_period=timedelta(seconds=1.0)),
144143
)
145144

146-
all_batteries: set[Component] = connection_manager.get().component_graph.components(
147-
component_categories={ComponentCategory.BATTERY}
145+
all_batteries = connection_manager.get().component_graph.components(
146+
matching_types=Battery
148147
)
149-
batteries_ids = {c.component_id for c in all_batteries}
148+
batteries_ids = {c.id for c in all_batteries}
150149
# Take some time to get data from components
151150
await asyncio.sleep(4)
152151
with open("/dev/stdout", "w", encoding="utf-8") as csvfile:

benchmarks/timeseries/benchmark_datasourcing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from typing import Any
1818

1919
from frequenz.channels import Broadcast, Receiver, ReceiverStoppedError
20-
from frequenz.client.microgrid import ComponentMetricId
20+
from frequenz.client.microgrid.metrics import Metric
2121

2222
from frequenz.sdk import microgrid
2323
from frequenz.sdk._internal._channels import ChannelRegistry
@@ -37,9 +37,9 @@
3737
sys.exit(1)
3838

3939
COMPONENT_METRIC_IDS = [
40-
ComponentMetricId.CURRENT_PHASE_1,
41-
ComponentMetricId.CURRENT_PHASE_2,
42-
ComponentMetricId.CURRENT_PHASE_3,
40+
Metric.AC_CURRENT_PHASE_1,
41+
Metric.AC_CURRENT_PHASE_2,
42+
Metric.AC_CURRENT_PHASE_3,
4343
]
4444

4545

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ plugins:
119119
- https://docs.python.org/3/objects.inv
120120
- https://frequenz-floss.github.io/frequenz-channels-python/v1/objects.inv
121121
- https://frequenz-floss.github.io/frequenz-client-common-python/v0.3/objects.inv
122-
- https://frequenz-floss.github.io/frequenz-client-microgrid-python/v0.9/objects.inv
122+
- https://frequenz-floss.github.io/frequenz-client-microgrid-python/v0.18/objects.inv
123123
- https://frequenz-floss.github.io/frequenz-core-python/v1/objects.inv
124124
- https://frequenz-floss.github.io/frequenz-quantities-python/v1/objects.inv
125125
- https://lovasoa.github.io/marshmallow_dataclass/html/objects.inv

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ dependencies = [
2929
# Make sure to update the mkdocs.yml file when
3030
# changing the version
3131
# (plugins.mkdocstrings.handlers.python.import)
32-
"frequenz-client-microgrid >= 0.9.0, < 0.10.0",
33-
"frequenz-client-common >= 0.3.2, < 0.4.0",
32+
"frequenz-client-microgrid >= 0.18.0, < 0.19.0",
33+
"frequenz-client-common >= 0.3.6, < 0.4.0",
3434
"frequenz-channels >= 1.6.1, < 2.0.0",
3535
"frequenz-quantities[marshmallow] >= 1.0.0, < 2.0.0",
3636
"networkx >= 2.8, < 4",

src/frequenz/sdk/microgrid/_data_pipeline.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from frequenz.channels import Broadcast, Sender
2020
from frequenz.client.common.microgrid.components import ComponentId
21-
from frequenz.client.microgrid import ComponentCategory, InverterType
21+
from frequenz.client.microgrid.component import Battery, EvCharger, SolarInverter
2222

2323
from frequenz.sdk.microgrid._power_managing._base_classes import Algorithm, DefaultPower
2424

@@ -105,23 +105,25 @@ def __init__(
105105
self._channel_registry,
106106
api_power_request_timeout=api_power_request_timeout,
107107
power_manager_algorithm=Algorithm.SHIFTING_MATRYOSHKA,
108-
component_category=ComponentCategory.BATTERY,
109108
default_power=DefaultPower.ZERO,
109+
component_class=Battery,
110110
)
111111
self._ev_power_wrapper = PowerWrapper(
112112
self._channel_registry,
113113
api_power_request_timeout=api_power_request_timeout,
114114
power_manager_algorithm=Algorithm.MATRYOSHKA,
115-
component_category=ComponentCategory.EV_CHARGER,
116115
default_power=DefaultPower.MAX,
116+
component_class=EvCharger,
117117
)
118118
self._pv_power_wrapper = PowerWrapper(
119119
self._channel_registry,
120120
api_power_request_timeout=api_power_request_timeout,
121121
power_manager_algorithm=Algorithm.MATRYOSHKA,
122-
component_category=ComponentCategory.INVERTER,
123-
component_type=InverterType.SOLAR,
124122
default_power=DefaultPower.MIN,
123+
# Using SolarInverter might be too specific, maybe we need to also pass
124+
# HybridInverter, see
125+
# https://github.com/frequenz-floss/frequenz-sdk-python/issues/1285
126+
component_class=SolarInverter,
125127
)
126128

127129
self._logical_meter: LogicalMeter | None = None

src/frequenz/sdk/microgrid/_data_sourcing/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33

44
"""The DataSourcingActor."""
55

6-
from ._component_metric_request import ComponentMetricId, ComponentMetricRequest
6+
from frequenz.client.microgrid.metrics import Metric
7+
8+
from ._component_metric_request import ComponentMetricRequest
79
from .data_sourcing import DataSourcingActor
810

911
__all__ = [
10-
"ComponentMetricId",
12+
"Metric",
1113
"ComponentMetricRequest",
1214
"DataSourcingActor",
1315
]

src/frequenz/sdk/microgrid/_data_sourcing/_component_metric_request.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
from datetime import datetime
88

99
from frequenz.client.common.microgrid.components import ComponentId
10-
from frequenz.client.microgrid import ComponentMetricId
10+
from frequenz.client.microgrid.metrics import Metric
1111

12-
__all__ = ["ComponentMetricRequest", "ComponentMetricId"]
12+
from frequenz.sdk.microgrid._old_component_data import TransitionalMetric
13+
14+
__all__ = ["ComponentMetricRequest", "Metric"]
1315

1416

1517
@dataclass
@@ -25,7 +27,7 @@ class ComponentMetricRequest:
2527
metric. For example, requesters can use different `namespace` values to subscribe to
2628
raw or resampled data streams separately. This ensures that each requester receives
2729
the appropriate type of data without interference. Requests with the same
28-
`namespace`, `component_id`, and `metric_id` will use the same channel, preventing
30+
`namespace`, `component_id`, and `metric` will use the same channel, preventing
2931
unnecessary duplication of data streams.
3032
3133
The requester and provider must use the same channel name so that they can
@@ -40,7 +42,7 @@ class ComponentMetricRequest:
4042
component_id: ComponentId
4143
"""The ID of the requested component."""
4244

43-
metric_id: ComponentMetricId
45+
metric: Metric | TransitionalMetric
4446
"""The ID of the requested component's metric."""
4547

4648
start_time: datetime | None
@@ -60,7 +62,7 @@ def get_channel_name(self) -> str:
6062
"component_metric_request<"
6163
f"namespace={self.namespace},"
6264
f"component_id={self.component_id},"
63-
f"metric_id={self.metric_id.name}"
65+
f"metric={self.metric.name}"
6466
f"{start}"
6567
">"
6668
)

0 commit comments

Comments
 (0)