diff --git a/packages/modules/devices/json/config.py b/packages/modules/devices/json/config.py index 645a58ec18..bbc0fea8da 100644 --- a/packages/modules/devices/json/config.py +++ b/packages/modules/devices/json/config.py @@ -41,10 +41,14 @@ def __init__(self, class JsonCounterConfiguration: - def __init__(self, jq_power: str = "", jq_exported: Optional[str] = None, jq_imported: Optional[str] = None): + def __init__(self, jq_power: str = "", jq_exported: Optional[str] = None, jq_imported: Optional[str] = None, + jq_power_l1: Optional[str] = None, jq_power_l2: Optional[str] = None, jq_power_l3: Optional[str] = None): self.jq_power = jq_power self.jq_exported = jq_exported self.jq_imported = jq_imported + self.jq_power_l1 = jq_power_l1 + self.jq_power_l2 = jq_power_l2 + self.jq_power_l3 = jq_power_l3 class JsonCounterSetup(ComponentSetup[JsonCounterConfiguration]): diff --git a/packages/modules/devices/json/counter.py b/packages/modules/devices/json/counter.py index e3fb62fb85..6c36a76513 100644 --- a/packages/modules/devices/json/counter.py +++ b/packages/modules/devices/json/counter.py @@ -24,7 +24,15 @@ def update(self, response): config = self.component_config.configuration power = float(jq.compile(config.jq_power).input(response).first()) - # ToDo: add current or power per phase + + if config.jq_power_l1 is None or config.jq_power_l2 is None or config.jq_power_l3 is None: + powers = None + else: + powers = [float(jq.compile(config.jq_power_l1).input(response).first()), + float(jq.compile(config.jq_power_l2).input(response).first()), + float(jq.compile(config.jq_power_l3).input(response).first()) + ] + if config.jq_imported is None or config.jq_exported is None: imported, exported = self.sim_counter.sim_count(power) else: @@ -34,7 +42,8 @@ def update(self, response): counter_state = CounterState( imported=imported, exported=exported, - power=power + power=power, + powers=powers ) self.store.set(counter_state) diff --git a/packages/modules/devices/json/device.py b/packages/modules/devices/json/device.py index cc5aad8d9c..103076c356 100644 --- a/packages/modules/devices/json/device.py +++ b/packages/modules/devices/json/device.py @@ -56,10 +56,15 @@ def read_legacy_bat(ip_address: str, jq_power: str, jq_soc: str): read_legacy(ip_address, bat.component_descriptor.configuration_factory(id=None, configuration=config)) -def read_legacy_counter(ip_address: str, jq_power: str, jq_imported: str, jq_exported: str): +def read_legacy_counter(ip_address: str, jq_power: str, jq_imported: str, jq_exported: str, + jq_power_l1: str, jq_power_l2: str, jq_power_l3: str): config = JsonCounterConfiguration(jq_power=jq_power, jq_imported=None if jq_imported == "" else jq_imported, - jq_exported=None if jq_exported == "" else jq_exported) + jq_exported=None if jq_exported == "" else jq_exported, + jq_power_l1=None if jq_power_l1 == "" else jq_power_l1, + jq_power_l2=None if jq_power_l2 == "" else jq_power_l2, + jq_power_l3=None if jq_power_l3 == "" else jq_power_l3 + ) read_legacy( ip_address, counter.component_descriptor.configuration_factory(id=None, configuration=config)) diff --git a/packages/modules/devices/json/device_test.py b/packages/modules/devices/json/device_test.py index 46a6a79a56..68b72d19b0 100644 --- a/packages/modules/devices/json/device_test.py +++ b/packages/modules/devices/json/device_test.py @@ -40,3 +40,23 @@ def test_device(monkeypatch, mock_value_store: Mock, requests_mock: requests_moc # evaluation assert len(mock_value_store.set.mock_calls) == 1 assert mock_value_store.set.call_args[0][0].power == expected_power + + +def test_counter_powers(monkeypatch, mock_value_store: Mock, requests_mock: requests_mock.Mocker): + # setup + monkeypatch.setattr(FaultState, "store_error", Mock()) + requests_mock.get("http://sample_host/sample_path", json={"power": 42, "power_l1": 11, "power_l2": 12, "power_l3": 13}) + device_config = Json(configuration=JsonConfiguration("http://sample_host/sample_path")) + + # execution + device = create_device(device_config) + device.add_component(JsonCounterSetup(configuration=JsonCounterConfiguration(jq_power=".power", + jq_power_l1=".power_l1", + jq_power_l2=".power_l2", + jq_power_l3=".power_l3"))) + device.update() + + # evaluation + assert len(mock_value_store.set.mock_calls) == 1 + assert mock_value_store.set.call_args[0][0].power == 42 + assert mock_value_store.set.call_args[0][0].powers == [11, 12, 13]