From b85d8dabb51c22ca8593b91b33d4a74ee28d6280 Mon Sep 17 00:00:00 2001 From: LKuemmel Date: Tue, 17 Jun 2025 17:34:57 +0200 Subject: [PATCH 1/4] fix error handling shelly --- .../modules/devices/shelly/shelly/device.py | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/modules/devices/shelly/shelly/device.py b/packages/modules/devices/shelly/shelly/device.py index 3b06de3dfd..a491be42d2 100644 --- a/packages/modules/devices/shelly/shelly/device.py +++ b/packages/modules/devices/shelly/shelly/device.py @@ -29,29 +29,44 @@ def get_device_generation(address: str) -> int: def create_device(device_config: Shelly) -> ConfigurableDevice: + generation = 1 + def create_counter_component(component_config: ShellyCounterSetup) -> ShellyCounter: + nonlocal generation return ShellyCounter(component_config, device_id=device_config.id, ip_address=device_config.configuration.ip_address, factor=device_config.configuration.factor, - generation=get_device_generation(device_config.configuration.ip_address)) + generation=generation) def create_inverter_component(component_config: ShellyInverterSetup) -> ShellyInverter: + nonlocal generation return ShellyInverter(component_config, device_id=device_config.id, ip_address=device_config.configuration.ip_address, factor=device_config.configuration.factor, - generation=get_device_generation(device_config.configuration.ip_address)) + generation=generation) def create_bat_component(component_config: ShellyBatSetup) -> ShellyBat: + nonlocal generation return ShellyBat(component_config, device_id=device_config.id, ip_address=device_config.configuration.ip_address, factor=device_config.configuration.factor, - generation=get_device_generation(device_config.configuration.ip_address)) + generation=generation) + + def initializer() -> None: + nonlocal generation + device_info = req.get_http_session().get( + f"http://{device_config.configuration.ip_address}/shelly", timeout=3).json() + if 'gen' in device_info: # gen 2+ + generation = int(device_info['gen']) return ConfigurableDevice( device_config=device_config, + initializer=initializer, + # wenn das Auslesen nicht klappt, konnte evlt beim Start die Generation nicht ermittelt werden + error_handler=initializer, component_factory=ComponentFactoryByType( counter=create_counter_component, inverter=create_inverter_component, From 59c00cb5999ae952bad0300828344161dd3f606f Mon Sep 17 00:00:00 2001 From: LKuemmel Date: Tue, 17 Jun 2025 17:35:55 +0200 Subject: [PATCH 2/4] clean up --- packages/modules/devices/shelly/shelly/device.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/packages/modules/devices/shelly/shelly/device.py b/packages/modules/devices/shelly/shelly/device.py index a491be42d2..35bd1c36ba 100644 --- a/packages/modules/devices/shelly/shelly/device.py +++ b/packages/modules/devices/shelly/shelly/device.py @@ -19,15 +19,6 @@ log = logging.getLogger(__name__) -def get_device_generation(address: str) -> int: - url = "http://" + address + "/shelly" - generation = 1 - device_info = req.get_http_session().get(url, timeout=3).json() - if 'gen' in device_info: # gen 2+ - generation = int(device_info['gen']) - return generation - - def create_device(device_config: Shelly) -> ConfigurableDevice: generation = 1 From 2efc6281048c1e6cd8f19f271e13d247ecffe79b Mon Sep 17 00:00:00 2001 From: LKuemmel Date: Tue, 17 Jun 2025 17:37:32 +0200 Subject: [PATCH 3/4] clean up --- .../modules/devices/shelly/shelly/device.py | 37 ------------------- 1 file changed, 37 deletions(-) diff --git a/packages/modules/devices/shelly/shelly/device.py b/packages/modules/devices/shelly/shelly/device.py index 35bd1c36ba..e759d30004 100644 --- a/packages/modules/devices/shelly/shelly/device.py +++ b/packages/modules/devices/shelly/shelly/device.py @@ -67,41 +67,4 @@ def initializer() -> None: ) -def run_device_legacy(device_config: Shelly, - component_config: ShellyInverterSetup) -> None: - device = create_device(device_config) - device.add_component(component_config) - log.debug("Shelly Configuration: %s, Component Configuration: %s", device_config, component_config) - device.update() - - -def create_legacy_device_config(address: str, generation: int, - num: int) -> Shelly: - device_config = Shelly(configuration=ShellyConfiguration(ip_address=address, generation=generation), id=num) - log.debug("Config: %s", device_config.configuration) - return device_config - - -def read_legacy_inverter(address: str, num: int) -> None: - component_config = ShellyInverterSetup(configuration=ShellyInverterConfiguration()) - component_config.id = num - generation = 1 - generation_file_name = '/var/www/html/openWB/ramdisk/shelly_wr_ret.' + address + '_shelly_infog' - # ToDo: remove hardcoded path to ramdisk! - if os.path.isfile(generation_file_name): - with open(generation_file_name, 'r') as file: - generation = int(file.read()) - else: - generation = get_device_generation(address) - with open(generation_file_name, 'w') as file: - file.write(str(generation)) - run_device_legacy(create_legacy_device_config(address, generation, - num), component_config) - - -def main(argv: List[str]) -> None: - run_using_positional_cli_args( - {"inverter": read_legacy_inverter}, argv) - - device_descriptor = DeviceDescriptor(configuration_factory=Shelly) From 8d96f3120abf9774e4a8f48a367fb46df49cd824 Mon Sep 17 00:00:00 2001 From: LKuemmel Date: Tue, 17 Jun 2025 17:40:42 +0200 Subject: [PATCH 4/4] pytest --- packages/modules/devices/shelly/shelly/device.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/modules/devices/shelly/shelly/device.py b/packages/modules/devices/shelly/shelly/device.py index e759d30004..47097ad383 100644 --- a/packages/modules/devices/shelly/shelly/device.py +++ b/packages/modules/devices/shelly/shelly/device.py @@ -1,19 +1,13 @@ #!/usr/bin/env python3 import logging -from typing import List -import os from modules.common import req -from helpermodules.cli import run_using_positional_cli_args from modules.common.abstract_device import DeviceDescriptor -from modules.common.configurable_device import (ConfigurableDevice, ComponentFactoryByType, IndependentComponentUpdater) +from modules.common.configurable_device import ConfigurableDevice, ComponentFactoryByType, IndependentComponentUpdater from modules.devices.shelly.shelly.inverter import ShellyInverter from modules.devices.shelly.shelly.bat import ShellyBat from modules.devices.shelly.shelly.counter import ShellyCounter -from modules.devices.shelly.shelly.config import Shelly, ShellyConfiguration -from modules.devices.shelly.shelly.config import ShellyInverterSetup, ShellyInverterConfiguration -from modules.devices.shelly.shelly.config import ShellyBatSetup -from modules.devices.shelly.shelly.config import ShellyCounterSetup +from modules.devices.shelly.shelly.config import Shelly, ShellyInverterSetup, ShellyBatSetup, ShellyCounterSetup log = logging.getLogger(__name__)