Skip to content

Commit b9e3d79

Browse files
committed
Update remaining code to work with FormulaEngine interface changes
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent bcc2acb commit b9e3d79

File tree

4 files changed

+79
-78
lines changed

4 files changed

+79
-78
lines changed

src/frequenz/sdk/timeseries/_formula_engine/__init__.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,13 @@
22
# Copyright © 2023 Frequenz Energy-as-a-Service GmbH
33

44
"""A formula engine for applying formulas."""
5-
from ._formula_engine import (
6-
FormulaEngine,
7-
FormulaEngine3Phase,
8-
FormulaReceiver,
9-
FormulaReceiver3Phase,
10-
_GenericEngine,
11-
_GenericFormulaReceiver,
12-
)
5+
from ._formula_engine import FormulaEngine, FormulaEngine3Phase
136
from ._formula_engine_pool import FormulaEnginePool
147
from ._resampled_formula_builder import ResampledFormulaBuilder
158

169
__all__ = [
1710
"FormulaEngine",
1811
"FormulaEngine3Phase",
19-
"FormulaReceiver",
20-
"FormulaReceiver3Phase",
2112
"FormulaEnginePool",
22-
"_GenericEngine",
23-
"_GenericFormulaReceiver",
2413
"ResampledFormulaBuilder",
2514
]

src/frequenz/sdk/timeseries/_formula_engine/_formula_engine_pool.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@
1919

2020
if TYPE_CHECKING:
2121
# Break circular import by enclosing these type hints in a `TYPE_CHECKING` block.
22-
from .._formula_engine import (
23-
FormulaReceiver,
24-
FormulaReceiver3Phase,
25-
_GenericEngine,
26-
_GenericFormulaReceiver,
27-
)
22+
from .._formula_engine import FormulaEngine, FormulaEngine3Phase
2823

2924

3025
class FormulaEnginePool:
@@ -51,14 +46,14 @@ def __init__(
5146
self._namespace = namespace
5247
self._channel_registry = channel_registry
5348
self._resampler_subscription_sender = resampler_subscription_sender
54-
self._engines: dict[str, "FormulaReceiver|FormulaReceiver3Phase"] = {}
49+
self._engines: dict[str, "FormulaEngine|FormulaEngine3Phase"] = {}
5550

56-
async def from_string(
51+
def from_string(
5752
self,
5853
formula: str,
5954
component_metric_id: ComponentMetricId,
6055
nones_are_zeros: bool = False,
61-
) -> "FormulaReceiver":
56+
) -> FormulaEngine:
6257
"""Get a receiver for a manual formula.
6358
6459
Args:
@@ -73,7 +68,7 @@ async def from_string(
7368
"""
7469
channel_key = formula + component_metric_id.value
7570
if channel_key in self._engines:
76-
return self._engines[channel_key].new_receiver()
71+
return self._engines[channel_key] # type: ignore
7772

7873
builder = ResampledFormulaBuilder(
7974
self._namespace,
@@ -82,17 +77,17 @@ async def from_string(
8277
self._resampler_subscription_sender,
8378
component_metric_id,
8479
)
85-
formula_engine = await builder.from_string(formula, nones_are_zeros)
80+
formula_engine = builder.from_string(formula, nones_are_zeros)
8681
self._engines[channel_key] = formula_engine
8782

88-
return formula_engine.new_receiver()
83+
return formula_engine
8984

90-
async def from_generator(
85+
def from_generator(
9186
self,
9287
channel_key: str,
93-
generator: "Type[FormulaGenerator[_GenericEngine]]",
88+
generator: "Type[FormulaGenerator]",
9489
config: FormulaGeneratorConfig = FormulaGeneratorConfig(),
95-
) -> "_GenericFormulaReceiver":
90+
) -> FormulaEngine | FormulaEngine3Phase:
9691
"""Get a receiver for a formula from a generator.
9792
9893
Args:
@@ -105,13 +100,13 @@ async def from_generator(
105100
FormulaGenerator returns.
106101
"""
107102
if channel_key in self._engines:
108-
return self._engines[channel_key].new_receiver()
103+
return self._engines[channel_key]
109104

110-
engine = await generator(
105+
engine = generator(
111106
self._namespace,
112107
self._channel_registry,
113108
self._resampler_subscription_sender,
114109
config,
115110
).generate()
116111
self._engines[channel_key] = engine
117-
return engine.new_receiver()
112+
return engine

src/frequenz/sdk/timeseries/ev_charger_pool/_ev_charger_pool.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from ...microgrid import connection_manager
2020
from ...microgrid.component import ComponentCategory, ComponentMetricId
2121
from .. import Sample, Sample3Phase
22-
from .._formula_engine import FormulaEnginePool, FormulaReceiver, FormulaReceiver3Phase
22+
from .._formula_engine import FormulaEngine, FormulaEngine3Phase, FormulaEnginePool
2323
from .._formula_engine._formula_generators import (
2424
EVChargerCurrentFormula,
2525
EVChargerPowerFormula,
@@ -104,40 +104,45 @@ def component_ids(self) -> abc.Set[int]:
104104
"""
105105
return self._component_ids
106106

107-
async def current(self) -> FormulaReceiver3Phase:
107+
def current(self) -> FormulaEngine3Phase:
108108
"""Fetch the total current for the EV Chargers in the pool.
109109
110110
If a formula engine to calculate EV Charger current is not already running, it
111-
will be started. Else, it will return a new receiver to the already existing
112-
data stream.
111+
will be started.
112+
113+
A receiver from the formula engine can be created using the `new_receiver`
114+
method.
113115
114116
Returns:
115-
A *new* receiver that will stream EV Charger current values.
117+
A FormulaEngine that will calculate and stream the total current of all EV
118+
Chargers.
116119
"""
117-
return await self._formula_pool.from_generator(
120+
return self._formula_pool.from_generator(
118121
"ev_charger_total_current",
119122
EVChargerCurrentFormula,
120123
FormulaGeneratorConfig(component_ids=self._component_ids),
121-
)
124+
) # type: ignore[return-value]
122125

123-
async def power(self) -> FormulaReceiver:
126+
def power(self) -> FormulaEngine:
124127
"""Fetch the total power for the EV Chargers in the pool.
125128
126129
If a formula engine to calculate EV Charger power is not already running, it
127-
will be started. Else, it will return a new receiver to the already existing
128-
data stream.
130+
will be started.
129131
130-
Returns:
131-
A *new* receiver that will stream EV Charger power values.
132+
A receiver from the formula engine can be created using the `new_receiver`
133+
method.
132134
135+
Returns:
136+
A FormulaEngine that will calculate and stream the total power of all EV
137+
Chargers.
133138
"""
134-
return await self._formula_pool.from_generator(
139+
return self._formula_pool.from_generator(
135140
"ev_charger_total_power",
136141
EVChargerPowerFormula,
137142
FormulaGeneratorConfig(component_ids=self._component_ids),
138-
)
143+
) # type: ignore[return-value]
139144

140-
async def component_data(self, component_id: int) -> Receiver[EVChargerData]:
145+
def component_data(self, component_id: int) -> Receiver[EVChargerData]:
141146
"""Stream 3-phase current values and state of an EV Charger.
142147
143148
Args:

src/frequenz/sdk/timeseries/logical_meter/_logical_meter.py

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from ...actor import ChannelRegistry, ComponentMetricRequest
1414
from ...microgrid import ComponentGraph
1515
from ...microgrid.component import ComponentMetricId
16-
from .._formula_engine import FormulaEnginePool, FormulaReceiver, FormulaReceiver3Phase
16+
from .._formula_engine import FormulaEngine, FormulaEngine3Phase, FormulaEnginePool
1717
from .._formula_engine._formula_generators import (
1818
BatteryPowerFormula,
1919
GridCurrentFormula,
@@ -112,12 +112,12 @@ def __init__(
112112
self._resampler_subscription_sender,
113113
)
114114

115-
async def start_formula(
115+
def start_formula(
116116
self,
117117
formula: str,
118118
component_metric_id: ComponentMetricId,
119119
nones_are_zeros: bool = False,
120-
) -> FormulaReceiver:
120+
) -> FormulaEngine:
121121
"""Start execution of the given formula.
122122
123123
Formulas can have Component IDs that are preceeded by a pound symbol("#"), and
@@ -134,64 +134,76 @@ async def start_formula(
134134
False, the returned value will be a None.
135135
136136
Returns:
137-
A FormulaReceiver that streams values with the formulas applied.
137+
A FormulaEngine that applies the formula and streams values.
138138
"""
139-
return await self._formula_pool.from_string(
139+
return self._formula_pool.from_string(
140140
formula, component_metric_id, nones_are_zeros
141141
)
142142

143-
async def grid_power(self) -> FormulaReceiver:
143+
def grid_power(self) -> FormulaEngine:
144144
"""Fetch the grid power for the microgrid.
145145
146146
If a formula engine to calculate grid power is not already running, it will be
147-
started. Else, it will return a new receiver to the already existing data
148-
stream.
147+
started.
149148
150-
Returns:
151-
A *new* receiver that will stream grid_power values.
149+
A receiver from the formula engine can be created using the `new_receiver`
150+
method.
152151
152+
Returns:
153+
A FormulaEngine that will calculate and stream grid power.
153154
"""
154-
return await self._formula_pool.from_generator("grid_power", GridPowerFormula)
155+
return self._formula_pool.from_generator(
156+
"grid_power",
157+
GridPowerFormula,
158+
) # type: ignore[return-value]
155159

156-
async def grid_current(self) -> FormulaReceiver3Phase:
160+
def grid_current(self) -> FormulaEngine3Phase:
157161
"""Fetch the grid power for the microgrid.
158162
159163
If a formula engine to calculate grid current is not already running, it will be
160-
started. Else, it will return a new receiver to the already existing data
161-
stream.
164+
started.
162165
163-
Returns:
164-
A *new* receiver that will stream grid_current values.
166+
A receiver from the formula engine can be created using the `new_receiver`
167+
method.
165168
169+
Returns:
170+
A FormulaEngine that will calculate and stream grid current.
166171
"""
167-
return await self._formula_pool.from_generator(
168-
"grid_current", GridCurrentFormula
169-
)
172+
return self._formula_pool.from_generator(
173+
"grid_current",
174+
GridCurrentFormula,
175+
) # type: ignore[return-value]
170176

171-
async def battery_power(self) -> FormulaReceiver:
177+
def battery_power(self) -> FormulaEngine:
172178
"""Fetch the cumulative battery power in the microgrid.
173179
174180
If a formula engine to calculate cumulative battery power is not already
175-
running, it will be started. Else, it will return a new receiver to the already
176-
existing data stream.
181+
running, it will be started.
177182
178-
Returns:
179-
A *new* receiver that will stream battery_power values.
183+
A receiver from the formula engine can be created using the `new_receiver`
184+
method.
180185
186+
Returns:
187+
A FormulaEngine that will calculate and stream battery power.
181188
"""
182-
return await self._formula_pool.from_generator(
183-
"battery_power", BatteryPowerFormula
184-
)
189+
return self._formula_pool.from_generator(
190+
"battery_power",
191+
BatteryPowerFormula,
192+
) # type: ignore[return-value]
185193

186-
async def pv_power(self) -> FormulaReceiver:
194+
def pv_power(self) -> FormulaEngine:
187195
"""Fetch the PV power production in the microgrid.
188196
189197
If a formula engine to calculate PV power production is not already running, it
190-
will be started. Else, it will return a new receiver to the already existing
191-
data stream.
198+
will be started.
192199
193-
Returns:
194-
A *new* receiver that will stream PV power production values.
200+
A receiver from the formula engine can be created using the `new_receiver`
201+
method.
195202
203+
Returns:
204+
A FormulaEngine that will calculate and stream PV power production.
196205
"""
197-
return await self._formula_pool.from_generator("pv_power", PVPowerFormula)
206+
return self._formula_pool.from_generator(
207+
"pv_power",
208+
PVPowerFormula,
209+
) # type: ignore[return-value]

0 commit comments

Comments
 (0)