77
88import asyncio
99import typing
10+ from datetime import datetime , timezone
1011from typing import Callable , Set , Tuple
1112
12- from frequenz .api .microgrid import (
13- battery_pb2 ,
14- common_pb2 ,
15- ev_charger_pb2 ,
16- inverter_pb2 ,
17- meter_pb2 ,
18- microgrid_pb2 ,
19- )
2013from frequenz .channels import Broadcast
21- from google .protobuf .timestamp_pb2 import Timestamp # pylint: disable=no-name-in-module
2214from pytest_mock import MockerFixture
2315
2416from frequenz .sdk import microgrid
3224)
3325from frequenz .sdk .microgrid .client import Connection
3426from frequenz .sdk .microgrid .component import (
35- BatteryData ,
3627 Component ,
3728 ComponentCategory ,
3829 ComponentData ,
39- EVChargerData ,
40- InverterData ,
30+ EVChargerCableState ,
31+ EVChargerComponentState ,
4132 InverterType ,
42- MeterData ,
4333)
4434from tests .utils .mock_microgrid import MockMicrogridClient
4535
36+ from ..utils .component_data_wrapper import (
37+ BatteryDataWrapper ,
38+ EvChargerDataWrapper ,
39+ InverterDataWrapper ,
40+ MeterDataWrapper ,
41+ )
42+
4643
4744class MockMicrogrid : # pylint: disable=too-many-instance-attributes
4845 """Setup a MockApi instance with multiple component layouts for tests."""
@@ -79,7 +76,8 @@ def __init__(self, grid_side_meter: bool, sample_rate_s: float = 0.01):
7976 self .evc_ids : list [int ] = []
8077 self .meter_ids : list [int ] = [4 ]
8178
82- self .evc_states : dict [int , ev_charger_pb2 .State ] = {}
79+ self .evc_component_states : dict [int , EVChargerComponentState ] = {}
80+ self .evc_cable_states : dict [int , EVChargerCableState ] = {}
8381
8482 self ._streaming_coros : list [typing .Coroutine [None , None , None ]] = []
8583 self ._streaming_tasks : list [asyncio .Task [None ]] = []
@@ -105,11 +103,10 @@ async def start(
105103 return ret
106104
107105 async def _comp_data_send_task (
108- self , comp_id : int , make_comp_data : Callable [[int , Timestamp ], ComponentData ]
106+ self , comp_id : int , make_comp_data : Callable [[int , datetime ], ComponentData ]
109107 ) -> None :
110108 for value in range (1 , 2000 ):
111- timestamp = Timestamp ()
112- timestamp .GetCurrentTime ()
109+ timestamp = datetime .now (tz = timezone .utc )
113110 val_to_send = value + int (comp_id / 10 )
114111 # for inverters with component_id > 100, send only half the messages.
115112 if comp_id % 10 == self .inverter_id_suffix :
@@ -123,27 +120,11 @@ def _start_meter_streaming(self, meter_id: int) -> None:
123120 self ._streaming_coros .append (
124121 self ._comp_data_send_task (
125122 meter_id ,
126- lambda value , ts : MeterData .from_proto (
127- microgrid_pb2 .ComponentData (
128- id = meter_id ,
129- ts = ts ,
130- meter = meter_pb2 .Meter (
131- data = meter_pb2 .Data (
132- ac = common_pb2 .AC (
133- power_active = common_pb2 .Metric (value = value ),
134- phase_1 = common_pb2 .AC .ACPhase (
135- current = common_pb2 .Metric (value = value + 100.0 )
136- ),
137- phase_2 = common_pb2 .AC .ACPhase (
138- current = common_pb2 .Metric (value = value + 101.0 )
139- ),
140- phase_3 = common_pb2 .AC .ACPhase (
141- current = common_pb2 .Metric (value = value + 102.0 )
142- ),
143- )
144- )
145- ),
146- ),
123+ lambda value , ts : MeterDataWrapper (
124+ component_id = meter_id ,
125+ timestamp = ts ,
126+ active_power = value ,
127+ current_per_phase = (value + 100.0 , value + 101.0 , value + 102.0 ),
147128 ),
148129 )
149130 )
@@ -152,16 +133,8 @@ def _start_battery_streaming(self, bat_id: int) -> None:
152133 self ._streaming_coros .append (
153134 self ._comp_data_send_task (
154135 bat_id ,
155- lambda value , ts : BatteryData .from_proto (
156- microgrid_pb2 .ComponentData (
157- id = bat_id ,
158- ts = ts ,
159- battery = battery_pb2 .Battery (
160- data = battery_pb2 .Data (
161- soc = common_pb2 .MetricAggregation (avg = value )
162- )
163- ),
164- )
136+ lambda value , ts : BatteryDataWrapper (
137+ component_id = bat_id , timestamp = ts , soc = value
165138 ),
166139 )
167140 )
@@ -170,18 +143,8 @@ def _start_inverter_streaming(self, inv_id: int) -> None:
170143 self ._streaming_coros .append (
171144 self ._comp_data_send_task (
172145 inv_id ,
173- lambda value , ts : InverterData .from_proto (
174- microgrid_pb2 .ComponentData (
175- id = inv_id ,
176- ts = ts ,
177- inverter = inverter_pb2 .Inverter (
178- data = inverter_pb2 .Data (
179- ac = common_pb2 .AC (
180- power_active = common_pb2 .Metric (value = value )
181- )
182- )
183- ),
184- )
146+ lambda value , ts : InverterDataWrapper (
147+ component_id = inv_id , timestamp = ts , active_power = value
185148 ),
186149 )
187150 )
@@ -190,28 +153,13 @@ def _start_ev_charger_streaming(self, evc_id: int) -> None:
190153 self ._streaming_coros .append (
191154 self ._comp_data_send_task (
192155 evc_id ,
193- lambda value , ts : EVChargerData .from_proto (
194- microgrid_pb2 .ComponentData (
195- id = evc_id ,
196- ts = ts ,
197- ev_charger = ev_charger_pb2 .EVCharger (
198- data = ev_charger_pb2 .Data (
199- ac = common_pb2 .AC (
200- power_active = common_pb2 .Metric (value = value ),
201- phase_1 = common_pb2 .AC .ACPhase (
202- current = common_pb2 .Metric (value = value + 10.0 )
203- ),
204- phase_2 = common_pb2 .AC .ACPhase (
205- current = common_pb2 .Metric (value = value + 11.0 )
206- ),
207- phase_3 = common_pb2 .AC .ACPhase (
208- current = common_pb2 .Metric (value = value + 12.0 )
209- ),
210- )
211- ),
212- state = self .evc_states [evc_id ],
213- ),
214- ),
156+ lambda value , ts : EvChargerDataWrapper (
157+ component_id = evc_id ,
158+ timestamp = ts ,
159+ active_power = value ,
160+ current_per_phase = (value + 10.0 , value + 11.0 , value + 12.0 ),
161+ component_state = self .evc_component_states [evc_id ],
162+ cable_state = self .evc_cable_states [evc_id ],
215163 ),
216164 ),
217165 )
@@ -297,10 +245,9 @@ def add_ev_chargers(self, count: int) -> None:
297245 self ._id_increment += 1
298246
299247 self .evc_ids .append (evc_id )
300- self .evc_states [evc_id ] = ev_charger_pb2 .State (
301- cable_state = ev_charger_pb2 .CABLE_STATE_UNPLUGGED ,
302- component_state = ev_charger_pb2 .COMPONENT_STATE_READY ,
303- )
248+ self .evc_component_states [evc_id ] = EVChargerComponentState .READY
249+ self .evc_cable_states [evc_id ] = EVChargerCableState .UNPLUGGED
250+
304251 self ._components .add (
305252 Component (
306253 evc_id ,
0 commit comments