Skip to content

Commit

Permalink
fix feature device list (openWB#1912)
Browse files Browse the repository at this point in the history
* fix feature device list

* fix powerdog
  • Loading branch information
LKuemmel authored Oct 7, 2024
1 parent c007797 commit 0f78046
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 867 deletions.
91 changes: 13 additions & 78 deletions packages/modules/devices/byd/byd/device.py
Original file line number Diff line number Diff line change
@@ -1,90 +1,25 @@
#!/usr/bin/env python3
import logging
from typing import Dict, Optional, List, Union

from dataclass_utils import dataclass_from_dict
from helpermodules.cli import run_using_positional_cli_args
from modules.devices.byd.byd.config import BYD, BYDBatSetup, BYDConfiguration
from modules.common.configurable_device import ComponentFactoryByType, ConfigurableDevice, IndependentComponentUpdater
from modules.devices.byd.byd.config import BYD, BYDBatSetup
from modules.devices.byd.byd import bat
from modules.common.abstract_device import AbstractDevice, DeviceDescriptor
from modules.common.component_context import SingleComponentUpdateContext
from modules.common.abstract_device import DeviceDescriptor

log = logging.getLogger(__name__)


class Device(AbstractDevice):
COMPONENT_TYPE_TO_CLASS = {
"bat": bat.BYDBat
}
def create_device(device_config: BYD):
def create_bat_component(component_config: BYDBatSetup):
return bat.BYDBat(component_config, device_config)

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

def add_component(self, component_config: Union[Dict, BYDBatSetup]) -> 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](
component_config,
self.device_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:
for component in self.components:
# Auch wenn bei einer Komponente ein Fehler auftritt, sollen alle anderen noch ausgelesen werden.
with SingleComponentUpdateContext(self.components[component].fault_state):
self.components[component].update()
else:
log.warning(
self.device_config.name +
": Es konnten keine Werte gelesen werden, da noch keine Komponenten konfiguriert wurden."
)


COMPONENT_TYPE_TO_MODULE = {
"bat": bat
}


def read_legacy(component_type: str,
ip_address: str,
username: str,
password: str,
num: Optional[int] = None) -> None:
dev = Device(BYD(configuration=BYDConfiguration(user=username, password=password, ip_address=ip_address)))
if component_type in COMPONENT_TYPE_TO_MODULE:
component_config = COMPONENT_TYPE_TO_MODULE[component_type].component_descriptor.configuration_factory()
else:
raise Exception(
"illegal component type " + component_type + ". Allowed values: " +
','.join(COMPONENT_TYPE_TO_MODULE.keys())
)
component_config.id = num
dev.add_component(component_config)

log.debug('byd IP-Adresse: ' + ip_address)
log.debug('byd Benutzer: ' + username)
log.debug('byd Passwort: ' + password)

dev.update()


def main(argv: List[str]):
run_using_positional_cli_args(read_legacy, argv)
return ConfigurableDevice(
device_config=device_config,
component_factory=ComponentFactoryByType(
bat=create_bat_component
),
component_updater=IndependentComponentUpdater(lambda component: component.update())
)


device_descriptor = DeviceDescriptor(configuration_factory=BYD)
100 changes: 25 additions & 75 deletions packages/modules/devices/carlo_gavazzi/carlo_gavazzi/device.py
Original file line number Diff line number Diff line change
@@ -1,89 +1,39 @@
#!/usr/bin/env python3
import logging
from typing import Dict, Optional, List, Union
from typing import Iterable

from dataclass_utils import dataclass_from_dict
from helpermodules.cli import run_using_positional_cli_args
from modules.common.configurable_device import ComponentFactoryByType, ConfigurableDevice, MultiComponentUpdater
from modules.devices.carlo_gavazzi.carlo_gavazzi import counter
from modules.devices.carlo_gavazzi.carlo_gavazzi.config import CarloGavazzi, CarloGavazziCounterSetup
from modules.common import modbus
from modules.common.abstract_device import AbstractDevice, DeviceDescriptor
from modules.common.abstract_device import DeviceDescriptor
from modules.common.component_context import SingleComponentUpdateContext

log = logging.getLogger(__name__)


class Device(AbstractDevice):
COMPONENT_TYPE_TO_CLASS = {
"counter": counter.CarloGavazziCounter,
}

def __init__(self, device_config: Union[Dict, CarloGavazzi]) -> None:
self.components = {} # type: Dict[str, counter.CarloGavazziCounter]
try:
self.device_config = dataclass_from_dict(CarloGavazzi, device_config)
self.client = modbus.ModbusTcpClient_(
self.device_config.configuration.ip_address, self.device_config.configuration.port)
except Exception:
log.exception("Fehler im Modul "+self.device_config.name)

def add_component(self, component_config: Union[Dict, CarloGavazziCounterSetup]) -> 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, self.client,
self.device_config.configuration.modbus_id)
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:
for component in self.components:
# Auch wenn bei einer Komponente ein Fehler auftritt, sollen alle anderen noch ausgelesen werden.
with SingleComponentUpdateContext(self.components[component].fault_state):
self.components[component].update()
else:
log.warning(
self.device_config.name +
": Es konnten keine Werte gelesen werden, da noch keine Komponenten konfiguriert wurden."
)


COMPONENT_TYPE_TO_MODULE = {
"counter": counter
}


def read_legacy(component_type: str, ip_address: str, num: Optional[int] = None) -> None:
device_config = CarloGavazzi()
device_config.configuration.ip_address = ip_address
dev = Device(device_config)
if component_type in COMPONENT_TYPE_TO_MODULE:
component_config = COMPONENT_TYPE_TO_MODULE[component_type].component_descriptor.configuration_factory()
else:
raise Exception(
"illegal component type " + component_type + ". Allowed values: " +
','.join(COMPONENT_TYPE_TO_MODULE.keys())
)
component_config.id = num
dev.add_component(component_config)

log.debug('carlo gavazzi IP-Adresse: ' + str(ip_address))

dev.update()


def main(argv: List[str]):
run_using_positional_cli_args(read_legacy, argv)
def create_device(device_config: CarloGavazzi):
def create_counter_component(component_config: CarloGavazziCounterSetup):
return counter.CarloGavazziCounter(device_config.id, component_config, client,
device_config.configuration.modbus_id)

def update_components(components: Iterable[counter.CarloGavazziCounter]):
with client:
for component in components:
with SingleComponentUpdateContext(component.fault_state):
component.update()

try:
client = modbus.ModbusTcpClient_(device_config.configuration.ip_address, device_config.configuration.port)
except Exception:
log.exception("Fehler in create_device")
return ConfigurableDevice(
device_config=device_config,
component_factory=ComponentFactoryByType(
counter=create_counter_component
),
component_updater=MultiComponentUpdater(update_components)
)


device_descriptor = DeviceDescriptor(configuration_factory=CarloGavazzi)
68 changes: 25 additions & 43 deletions packages/modules/devices/generic/mqtt/device.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,37 @@
#!/usr/bin/env python3
from typing import Dict, Union
from typing import Iterable, Union
import logging

from dataclass_utils import dataclass_from_dict
from modules.common.abstract_device import AbstractDevice, DeviceDescriptor
from modules.common.component_context import MultiComponentUpdateContext
from modules.common.abstract_device import DeviceDescriptor
from modules.common.configurable_device import ComponentFactoryByType, ConfigurableDevice, MultiComponentUpdater
from modules.devices.generic.mqtt import bat, counter, inverter
from modules.devices.generic.mqtt.config import Mqtt, MqttBatSetup, MqttCounterSetup, MqttInverterSetup

log = logging.getLogger(__name__)


class Device(AbstractDevice):
COMPONENT_TYPE_TO_CLASS = {
"bat": bat.MqttBat,
"counter": counter.MqttCounter,
"inverter": inverter.MqttInverter
}
COMPONENT_TYPE_TO_MODULE = {
"bat": bat,
"counter": counter,
"inverter": inverter
}

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

def add_component(self, component_config: Union[Dict, MqttBatSetup, MqttCounterSetup, MqttInverterSetup]) -> None:
if isinstance(component_config, Dict):
component_type = component_config["type"]
else:
component_type = component_config.type
component_config = dataclass_from_dict(self.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](component_config))

def update(self) -> None:
if self.components:
with MultiComponentUpdateContext(self.components):
log.debug("MQTT-Module müssen nicht ausgelesen werden.")
else:
log.warning(
self.device_config.name +
": Es konnten keine Werte gelesen werden, da noch keine Komponenten konfiguriert wurden."
)
def create_device(device_config: Mqtt):
def create_bat_component(component_config: MqttBatSetup):
return bat.MqttBat(component_config)

def create_counter_component(component_config: MqttCounterSetup):
return counter.MqttCounter(component_config)

def create_inverter_component(component_config: MqttInverterSetup):
return inverter.MqttInverter(component_config)

def update_components(components: Iterable[Union[bat.MqttBat, counter.MqttCounter, inverter.MqttInverter]]):
log.debug("MQTT-Module müssen nicht ausgelesen werden.")

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


device_descriptor = DeviceDescriptor(configuration_factory=Mqtt)
Loading

0 comments on commit 0f78046

Please sign in to comment.