77from datetime import datetime , timezone
88from typing import Dict , List , Optional
99
10- import frequenz .api .microgrid .microgrid_pb2 as microgrid_pb
1110import pytest
12- from frequenz .api .microgrid .battery_pb2 import Battery
13- from frequenz .api .microgrid .battery_pb2 import Data as PbBatteryData
14- from frequenz .api .microgrid .battery_pb2 import Properties as BatteryProperties
15- from frequenz .api .microgrid .common_pb2 import AC , DC , Bounds
16- from frequenz .api .microgrid .common_pb2 import Metric as PbMetric
17- from frequenz .api .microgrid .common_pb2 import MetricAggregation
18- from frequenz .api .microgrid .inverter_pb2 import Data as PbInverterData
19- from frequenz .api .microgrid .inverter_pb2 import Inverter
20- from google .protobuf .timestamp_pb2 import Timestamp # pylint: disable=no-name-in-module
11+ from frequenz .api .microgrid .common_pb2 import Bounds
2112
2213from frequenz .sdk .microgrid .component import BatteryData , InverterData
2314from frequenz .sdk .power import DistributionAlgorithm , InvBatPair
2415
16+ from ..utils .component_data_wrapper import BatteryDataWrapper , InverterDataWrapper
17+
2518
2619@dataclass
2720class Bound :
@@ -46,21 +39,6 @@ class Metric:
4639 now : Optional [float ]
4740 bound : Optional [Bound ] = None
4841
49- def to_protobuf (self ) -> PbMetric :
50- """Create protobuf Metric message from that instance.
51-
52- Returns:
53- Protobuf Metric
54- """
55- if self .now is None :
56- if self .bound is None :
57- return PbMetric ()
58- return PbMetric (system_bounds = self .bound .to_protobuf ())
59- if self .bound is None :
60- return PbMetric (value = self .now )
61-
62- return PbMetric (value = self .now , system_bounds = self .bound .to_protobuf ())
63-
6442
6543def battery_msg ( # pylint: disable=too-many-arguments
6644 component_id : int ,
@@ -82,26 +60,16 @@ def battery_msg( # pylint: disable=too-many-arguments
8260 Returns:
8361 Protobuf battery component with data above
8462 """
85- pb_timestamp = Timestamp ()
86- pb_timestamp .FromDatetime (timestamp )
87- capacitypb = capacity .to_protobuf ()
88- socpb = soc .to_protobuf ()
89- pb_data = microgrid_pb .ComponentData (
90- id = component_id ,
91- ts = pb_timestamp ,
92- battery = Battery (
93- properties = BatteryProperties (capacity = capacitypb .value ),
94- data = PbBatteryData (
95- soc = MetricAggregation (
96- avg = socpb .value , system_bounds = socpb .system_bounds
97- ),
98- dc = DC (
99- power = PbMetric (system_bounds = power .to_protobuf ()),
100- ),
101- ),
102- ),
63+ return BatteryDataWrapper (
64+ component_id = component_id ,
65+ capacity = capacity .now if capacity .now is not None else float ("NaN" ),
66+ soc = soc .now if soc .now is not None else float ("NaN" ),
67+ soc_lower_bound = soc .bound .lower if soc .bound is not None else float ("NaN" ),
68+ soc_upper_bound = soc .bound .upper if soc .bound is not None else float ("NaN" ),
69+ power_lower_bound = power .lower ,
70+ power_upper_bound = power .upper ,
71+ timestamp = timestamp ,
10372 )
104- return BatteryData .from_proto (pb_data )
10573
10674
10775def inverter_msg (
@@ -120,20 +88,13 @@ def inverter_msg(
12088 Returns:
12189 Protobuf inverter component with data above.
12290 """
123- pb_timestamp = Timestamp ()
124- pb_timestamp .FromDatetime (timestamp )
125- pb_data = microgrid_pb .ComponentData (
126- id = component_id ,
127- ts = pb_timestamp ,
128- inverter = Inverter (
129- data = PbInverterData (
130- ac = AC (power_active = PbMetric (system_bounds = power .to_protobuf ())),
131- )
132- ),
91+ return InverterDataWrapper (
92+ component_id = component_id ,
93+ timestamp = timestamp ,
94+ active_power_lower_bound = power .lower ,
95+ active_power_upper_bound = power .upper ,
13396 )
13497
135- return InverterData .from_proto (pb_data )
136-
13798
13899class TestDistributionAlgorithm : # pylint: disable=too-many-public-methods
139100 """Test whether the algorithm works as expected."""
@@ -147,16 +108,16 @@ def create_components_with_capacity(
147108
148109 components : List [InvBatPair ] = []
149110 for i in range (0 , num ):
150- bat_msg = microgrid_pb .ComponentData (
151- id = 2 * i ,
152- battery = Battery (properties = BatteryProperties (capacity = capacity [i ])),
111+ battery_data = BatteryDataWrapper (
112+ component_id = 2 * i ,
113+ timestamp = datetime .now (tz = timezone .utc ),
114+ capacity = capacity [i ],
115+ )
116+ inverter_data = InverterDataWrapper (
117+ component_id = 2 * i + 1 , timestamp = datetime .now (tz = timezone .utc )
153118 )
154- battery = BatteryData .from_proto (bat_msg )
155-
156- inv_msg = microgrid_pb .ComponentData (id = 2 * i + 1 , inverter = Inverter ())
157- inverter = InverterData .from_proto (inv_msg )
158119
159- components .append (InvBatPair (battery , inverter ))
120+ components .append (InvBatPair (battery_data , inverter_data ))
160121 return components
161122
162123 def test_total_capacity_all_0 (self ) -> None :
0 commit comments