Skip to content

Commit 6bc7172

Browse files
committed
Add mocks for meter_data, ev_charger_data in MockMicrogridClient
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 6785d38 commit 6bc7172

File tree

1 file changed

+103
-17
lines changed

1 file changed

+103
-17
lines changed

tests/utils/mock_microgrid.py

Lines changed: 103 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
Component,
1919
ComponentCategory,
2020
ComponentData,
21+
EVChargerData,
2122
InverterData,
23+
MeterData,
2224
)
2325

2426

@@ -42,8 +44,12 @@ def __init__(self, components: Set[Component], connections: Set[Connection]):
4244

4345
bat_channels = self._create_battery_channels()
4446
inv_channels = self._create_inverter_channels()
47+
meter_channels = self._create_meter_channels()
48+
ev_charger_channels = self._create_ev_charger_channels()
4549

46-
mock_api = self._create_mock_api(components, bat_channels, inv_channels)
50+
mock_api = self._create_mock_api(
51+
components, bat_channels, inv_channels, meter_channels, ev_charger_channels
52+
)
4753
kwargs: Dict[str, Any] = {
4854
"api_client": mock_api,
4955
"component_graph": self._component_graph,
@@ -56,6 +62,12 @@ def __init__(self, components: Set[Component], connections: Set[Connection]):
5662
self._inverter_data_senders = {
5763
id: channel.new_sender() for id, channel in inv_channels.items()
5864
}
65+
self._meter_data_senders = {
66+
id: channel.new_sender() for id, channel in meter_channels.items()
67+
}
68+
self._ev_charger_data_senders = {
69+
id: channel.new_sender() for id, channel in ev_charger_channels.items()
70+
}
5971

6072
def initialize(self, mocker: MockerFixture) -> None:
6173
"""Mock `microgrid.get` call to return this mock_microgrid.
@@ -107,15 +119,16 @@ async def send(self, data: ComponentData) -> bool:
107119
return await self._battery_data_senders[cid].send(data)
108120
if isinstance(data, InverterData):
109121
return await self._inverter_data_senders[cid].send(data)
122+
if isinstance(data, MeterData):
123+
return await self._meter_data_senders[cid].send(data)
124+
if isinstance(data, EVChargerData):
125+
return await self._ev_charger_data_senders[cid].send(data)
110126

111127
raise RuntimeError(f"{type(data)} is not supported in MockMicrogridClient.")
112128

113129
def _create_battery_channels(self) -> Dict[int, Broadcast[BatteryData]]:
114130
"""Create channels for the batteries.
115131
116-
Args:
117-
components: set of components.
118-
119132
Returns:
120133
Dictionary where the key is battery id and the value is channel for this
121134
battery.
@@ -131,15 +144,28 @@ def _create_battery_channels(self) -> Dict[int, Broadcast[BatteryData]]:
131144
bid: Broadcast[BatteryData]("battery_data_" + str(bid)) for bid in batteries
132145
}
133146

147+
def _create_meter_channels(self) -> Dict[int, Broadcast[MeterData]]:
148+
"""Create channels for the meters.
149+
150+
Returns:
151+
Dictionary where the key is meter id and the value is channel for this
152+
meter.
153+
"""
154+
meters = [
155+
c.component_id
156+
for c in self.component_graph.components(
157+
component_category={ComponentCategory.METER}
158+
)
159+
]
160+
161+
return {cid: Broadcast[MeterData]("meter_data_" + str(cid)) for cid in meters}
162+
134163
def _create_inverter_channels(self) -> Dict[int, Broadcast[InverterData]]:
135164
"""Create channels for the inverters.
136165
137-
Args:
138-
components: set of components.
139-
140166
Returns:
141-
Dictionary where the key is inverter id and the value is channel for this
142-
battery.
167+
Dictionary where the key is inverter id and the value is channel for
168+
this inverter.
143169
"""
144170
inverters = [
145171
c.component_id
@@ -153,11 +179,31 @@ def _create_inverter_channels(self) -> Dict[int, Broadcast[InverterData]]:
153179
for cid in inverters
154180
}
155181

182+
def _create_ev_charger_channels(self) -> Dict[int, Broadcast[EVChargerData]]:
183+
"""Create channels for the ev chargers.
184+
185+
Returns:
186+
Dictionary where the key is the id of the ev_charger and the value is
187+
channel for this ev_charger.
188+
"""
189+
meters = [
190+
c.component_id
191+
for c in self.component_graph.components(
192+
component_category={ComponentCategory.EV_CHARGER}
193+
)
194+
]
195+
196+
return {
197+
cid: Broadcast[EVChargerData]("meter_data_" + str(cid)) for cid in meters
198+
}
199+
156200
def _create_mock_api(
157201
self,
158202
components: Set[Component],
159-
bat_channel: Dict[int, Broadcast[BatteryData]],
160-
inv_channel: Dict[int, Broadcast[InverterData]],
203+
bat_channels: Dict[int, Broadcast[BatteryData]],
204+
inv_channels: Dict[int, Broadcast[InverterData]],
205+
meter_channels: Dict[int, Broadcast[MeterData]],
206+
ev_charger_channels: Dict[int, Broadcast[EVChargerData]],
161207
) -> MagicMock:
162208
"""Create mock of MicrogridApiClient.
163209
@@ -176,11 +222,21 @@ def _create_mock_api(
176222
# NOTE that has to be partial, because battery_data has id argument and takes
177223
# channel based on the argument.
178224
api.battery_data = AsyncMock(
179-
side_effect=partial(self._get_battery_receiver, channels=bat_channel)
225+
side_effect=partial(self._get_battery_receiver, channels=bat_channels)
180226
)
181227

182228
api.inverter_data = AsyncMock(
183-
side_effect=partial(self._get_inverter_receiver, channels=inv_channel)
229+
side_effect=partial(self._get_inverter_receiver, channels=inv_channels)
230+
)
231+
232+
api.meter_data = AsyncMock(
233+
side_effect=partial(self._get_meter_receiver, channels=meter_channels)
234+
)
235+
236+
api.ev_charger_data = AsyncMock(
237+
side_effect=partial(
238+
self._get_ev_charger_receiver, channels=ev_charger_channels
239+
)
184240
)
185241

186242
# Can be override in the future
@@ -199,8 +255,39 @@ def _get_battery_receiver(
199255
channels: Broadcast channels
200256
201257
Returns:
202-
Receiver[Union[BatteryData, InverterData]]: Receiver from the given
203-
channels.
258+
Receiver from the given channels.
259+
"""
260+
return channels[component_id].new_receiver("component" + str(component_id))
261+
262+
def _get_meter_receiver(
263+
self,
264+
component_id: int,
265+
channels: Dict[int, Broadcast[MeterData]],
266+
) -> Receiver[MeterData]:
267+
"""Return receiver of the broadcast channel for given component_id.
268+
269+
Args:
270+
component_id: component_id
271+
channels: Broadcast channels
272+
273+
Returns:
274+
Receiver from the given channels.
275+
"""
276+
return channels[component_id].new_receiver("component" + str(component_id))
277+
278+
def _get_ev_charger_receiver(
279+
self,
280+
component_id: int,
281+
channels: Dict[int, Broadcast[EVChargerData]],
282+
) -> Receiver[EVChargerData]:
283+
"""Return receiver of the broadcast channel for given component_id.
284+
285+
Args:
286+
component_id: component_id
287+
channels: Broadcast channels
288+
289+
Returns:
290+
Receiver from the given channels.
204291
"""
205292
return channels[component_id].new_receiver("component" + str(component_id))
206293

@@ -216,7 +303,6 @@ def _get_inverter_receiver(
216303
channels: Broadcast channels
217304
218305
Returns:
219-
Receiver[Union[BatteryData, InverterData]]: Receiver from the given
220-
channels.
306+
Receiver from the given channels.
221307
"""
222308
return channels[component_id].new_receiver("component" + str(component_id))

0 commit comments

Comments
 (0)