Skip to content

Commit

Permalink
fix BatterX
Browse files Browse the repository at this point in the history
  • Loading branch information
LKuemmel committed Aug 25, 2023
1 parent a435c84 commit 5c565ad
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 57 deletions.
5 changes: 3 additions & 2 deletions packages/modules/devices/batterx/batterx_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@


from modules.common.component_state import BatState, CounterState, InverterState
from modules.devices.batterx import bat, counter, device, inverter
from modules.devices.batterx import bat, counter, inverter
from modules.devices.batterx.config import (BatterX, BatterXBatSetup, BatterXConfiguration, BatterXCounterSetup,
BatterXInverterSetup)
from modules.devices.batterx.device import create_device


def test_batterx(monkeypatch, requests_mock: requests_mock.mock):
Expand All @@ -19,7 +20,7 @@ def test_batterx(monkeypatch, requests_mock: requests_mock.mock):
monkeypatch.setattr(inverter, 'get_inverter_value_store', Mock(return_value=mock_inverter_value_store))
requests_mock.get("http://1.1.1.1/api.php?get=currentstate", json=SAMPLE)

dev = device.Device(BatterX(configuration=BatterXConfiguration(ip_address="1.1.1.1")))
dev = create_device(BatterX(configuration=BatterXConfiguration(ip_address="1.1.1.1")))
dev.add_component(BatterXBatSetup(id=2))
dev.add_component(BatterXCounterSetup(id=0))
dev.add_component(BatterXInverterSetup(id=1))
Expand Down
94 changes: 39 additions & 55 deletions packages/modules/devices/batterx/device.py
Original file line number Diff line number Diff line change
@@ -1,72 +1,56 @@
#!/usr/bin/env python3
import logging
from typing import Dict, Optional, Union, List
from typing import Iterable, Optional, Union, List

from dataclass_utils import dataclass_from_dict
from helpermodules.cli import run_using_positional_cli_args
from modules.common.abstract_device import AbstractDevice, DeviceDescriptor
from modules.common.abstract_device import DeviceDescriptor
from modules.common.component_context import MultiComponentUpdateContext
from modules.common.configurable_device import ComponentFactoryByType, ConfigurableDevice, MultiComponentUpdater
from modules.common.store import get_inverter_value_store
from modules.devices.batterx import bat, external_inverter
from modules.devices.batterx import counter
from modules.devices.batterx import inverter
from modules.devices.batterx.config import BatterX, BatterXBatSetup, BatterXCounterSetup, BatterXInverterSetup
from modules.devices.batterx.config import (BatterX, BatterXBatSetup, BatterXCounterSetup,
BatterXExternalInverterSetup, BatterXInverterSetup)
from modules.common import req

log = logging.getLogger(__name__)


batterx_component_classes = Union[bat.BatterXBat, counter.BatterXCounter,
inverter.BatterXInverter]


class Device(AbstractDevice):
COMPONENT_TYPE_TO_CLASS = {
"bat": bat.BatterXBat,
"counter": counter.BatterXCounter,
"inverter": inverter.BatterXInverter,
}

def __init__(self, device_config: Union[Dict, BatterX]) -> None:
self.components = {} # type: Dict[str, batterx_component_classes]
try:
self.device_config = dataclass_from_dict(BatterX, device_config)
except Exception:
log.exception("Fehler im Modul "+self.device_config.name)

def add_component(self, component_config: Union[Dict,
BatterXBatSetup,
BatterXCounterSetup,
BatterXInverterSetup]) -> None:
if isinstance(component_config, Dict):
component_type = component_config["type"]
else:
component_type = component_config.type
component_config = dataclass_from_dict(COMPONENT_TYPE_TO_MODULE[
component_type].component_descriptor.configuration_factory, component_config)
if component_type in self.COMPONENT_TYPE_TO_CLASS:
self.components["component"+str(component_config.id)] = (self.COMPONENT_TYPE_TO_CLASS[component_type](
self.device_config.id, component_config))
else:
raise Exception(
"illegal component type " + component_type + ". Allowed values: " +
','.join(self.COMPONENT_TYPE_TO_CLASS.keys())
)

def update(self) -> None:
log.debug("Start device reading " + str(self.components))
if self.components:
with MultiComponentUpdateContext(self.components):
resp_json = req.get_http_session().get(
'http://' + self.device_config.configuration.ip_address + '/api.php?get=currentstate',
inverter.BatterXInverter, external_inverter.BatterXExternalInverter]


def create_device(device_config: BatterX):
def create_bat_component(component_config: BatterXBatSetup):
return bat.BatterXBat(device_config.id, component_config)

def create_counter_component(component_config: BatterXCounterSetup):
return counter.BatterXCounter(device_config.id, component_config)

def create_inverter_component(component_config: BatterXInverterSetup):
return inverter.BatterXInverter(device_config.id, component_config)

def create_external_inverter_component(component_config: BatterXExternalInverterSetup):
return external_inverter.BatterXExternalInverter(device_config.id, component_config)

def update_components(components: Iterable[batterx_component_classes]):
resp_json = req.get_http_session().get(
'http://' + device_config.configuration.ip_address + '/api.php?get=currentstate',
timeout=5).json()
for component in self.components:
self.components[component].update(resp_json)
else:
log.warning(
self.device_config.name +
": Es konnten keine Werte gelesen werden, da noch keine Komponenten konfiguriert wurden."
)
for component in components:
component.update(resp_json)

return ConfigurableDevice(
device_config=device_config,
component_factory=ComponentFactoryByType(
bat=create_bat_component,
counter=create_counter_component,
inverter=create_inverter_component,
external_inverter=create_external_inverter_component,
),
component_updater=MultiComponentUpdater(update_components)
)


COMPONENT_TYPE_TO_MODULE = {
Expand All @@ -87,7 +71,7 @@ def read_legacy(

device_config = BatterX()
device_config.configuration.ip_address = ip_address
dev = Device(device_config)
dev = create_device(device_config)
dev = _add_component(dev, component_type, num)
if evu_counter == "bezug_batterx":
dev = _add_component(dev, "counter", 0)
Expand Down Expand Up @@ -115,7 +99,7 @@ def read_legacy(
get_inverter_value_store(num).set(state)


def _add_component(dev: Device, component_type: str, num: Optional[int]) -> Device:
def _add_component(dev: ConfigurableDevice, component_type: str, num: Optional[int]) -> ConfigurableDevice:
if component_type in COMPONENT_TYPE_TO_MODULE:
component_config = COMPONENT_TYPE_TO_MODULE[component_type].component_descriptor.configuration_factory()
else:
Expand Down

0 comments on commit 5c565ad

Please sign in to comment.