Skip to content

Commit 3da1379

Browse files
committed
Remove parameters for frequency()
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent 471adc6 commit 3da1379

File tree

3 files changed

+33
-40
lines changed

3 files changed

+33
-40
lines changed

RELEASE_NOTES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646

4747
- The `ComponentGraph.components()` parameters `component_id` and `component_category` were renamed to `component_ids` and `component_categories`, respectively.
4848

49+
- The `GridFrequency.component` propery was renamed to `GridFrequency.source`
50+
51+
- The `microgrid.frequency()` method no longer supports passing the `component` parameter. Instead the best component is automatically selected.
52+
4953
## New Features
5054

5155
<!-- Here goes the main new features and examples or instructions on how to use them -->

src/frequenz/sdk/microgrid/_data_pipeline.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from frequenz.channels import Broadcast, Sender
2020

2121
from ..actor._actor import Actor
22-
from ..microgrid.component import Component
2322
from ..timeseries._base_types import PoolType
2423
from ..timeseries._grid_frequency import GridFrequency
2524
from ..timeseries.grid import Grid
@@ -119,29 +118,21 @@ def __init__(
119118
self._grid: Grid | None = None
120119
self._ev_charger_pools: dict[frozenset[int], EVChargerPool] = {}
121120
self._battery_pools: dict[frozenset[int], BatteryPoolReferenceStore] = {}
122-
self._frequency_pool: dict[int, GridFrequency] = {}
121+
self._frequency_instance: GridFrequency | None = None
123122

124-
def frequency(self, component: Component | None = None) -> GridFrequency:
123+
def frequency(self) -> GridFrequency:
125124
"""Fetch the grid frequency for the microgrid.
126125
127-
Args:
128-
component: The component to use when fetching the grid frequency. If None,
129-
the component will be fetched from the registry.
130-
131126
Returns:
132-
A GridFrequency instance.
127+
The GridFrequency instance.
133128
"""
134-
if component is None:
135-
component = GridFrequency.find_frequency_component()
129+
if self._frequency_instance is None:
130+
self._frequency_instance = GridFrequency(
131+
self._data_sourcing_request_sender(),
132+
self._channel_registry,
133+
)
136134

137-
if component.component_id in self._frequency_pool:
138-
return self._frequency_pool[component.component_id]
139-
140-
grid_frequency = GridFrequency(
141-
self._data_sourcing_request_sender(), self._channel_registry, component
142-
)
143-
self._frequency_pool[component.component_id] = grid_frequency
144-
return grid_frequency
135+
return self._frequency_instance
145136

146137
def logical_meter(self) -> LogicalMeter:
147138
"""Return the logical meter instance.
@@ -409,17 +400,13 @@ async def initialize(resampler_config: ResamplerConfig) -> None:
409400
_DATA_PIPELINE = _DataPipeline(resampler_config)
410401

411402

412-
def frequency(component: Component | None = None) -> GridFrequency:
403+
def frequency() -> GridFrequency:
413404
"""Return the grid frequency.
414405
415-
Args:
416-
component: Optional component to get the frequency for. If not specified,
417-
the frequency of the grid is returned.
418-
419406
Returns:
420407
The grid frequency.
421408
"""
422-
return _get().frequency(component)
409+
return _get().frequency()
423410

424411

425412
def logical_meter() -> LogicalMeter:

src/frequenz/sdk/timeseries/_grid_frequency.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,32 +49,32 @@ def __init__(
4949
self,
5050
data_sourcing_request_sender: Sender[ComponentMetricRequest],
5151
channel_registry: ChannelRegistry,
52-
component: Component,
52+
source: Component | None = None,
5353
):
5454
"""Initialize the grid frequency formula generator.
5555
5656
Args:
5757
data_sourcing_request_sender: The sender to use for requests.
5858
channel_registry: The channel registry to use for the grid frequency.
59-
component: The component to use for the grid frequency receiver. If not
60-
provided, the first component that is either a meter, inverter or EV
61-
charger will be used.
59+
source: The source component to use to receive the grid frequency.
6260
"""
6361
self._request_sender = data_sourcing_request_sender
6462
self._channel_registry = channel_registry
65-
self._component = component
66-
self._component_metric_request = create_request(component.component_id)
63+
self._source_component = source or GridFrequency.find_frequency_source()
64+
self._component_metric_request = create_request(
65+
self._source_component.component_id
66+
)
6767

6868
self._task: None | asyncio.Task[None] = None
6969

7070
@property
71-
def component(self) -> Component:
72-
"""The component that is used for grid frequency.
71+
def source(self) -> Component:
72+
"""The component that is used to fetch the grid frequency.
7373
7474
Returns:
7575
The component that is used for grid frequency.
7676
"""
77-
return self._component
77+
return self._source_component
7878

7979
def new_receiver(self) -> Receiver[Sample[Frequency]]:
8080
"""Create a receiver for grid frequency.
@@ -89,22 +89,24 @@ def new_receiver(self) -> Receiver[Sample[Frequency]]:
8989
if not self._task:
9090
self._task = asyncio.create_task(self._send_request())
9191
else:
92-
_logger.info("Grid frequency request already sent: %s", self._component)
92+
_logger.info(
93+
"Grid frequency request already sent: %s", self._source_component
94+
)
9395

9496
return receiver
9597

9698
async def _send_request(self) -> None:
9799
"""Send the request for grid frequency."""
98-
_logger.info("Sending request for grid frequency: %s", self._component)
100+
_logger.info("Sending request for grid frequency: %s", self._source_component)
99101
await self._request_sender.send(self._component_metric_request)
100-
_logger.info("Sent request for grid frequency: %s", self._component)
102+
_logger.info("Sent request for grid frequency: %s", self._source_component)
101103

102104
@staticmethod
103-
def find_frequency_component() -> Component:
104-
"""Find the component that will be used for grid frequency.
105+
def find_frequency_source() -> Component:
106+
"""Find the source component that will be used for grid frequency.
105107
106-
Uses the first meter it can find to gather the frequency. If no meter is
107-
available, it will use the first inverter, then EV charger.
108+
Will use the first meter it can find to gather the frequency.
109+
If no meter is available, the first inverter will be used and finally the first EV charger.
108110
109111
Returns:
110112
The component that will be used for grid frequency.

0 commit comments

Comments
 (0)