Skip to content

Commit

Permalink
youless (snaptec#2715)
Browse files Browse the repository at this point in the history
* youless

* typecast
  • Loading branch information
LKuemmel committed Jun 6, 2023
1 parent 3bbfc70 commit 274690b
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 61 deletions.
8 changes: 2 additions & 6 deletions modules/wr_youless120/main.sh
Expand Up @@ -16,13 +16,9 @@ else
MYLOGFILE="${RAMDISKDIR}/wr_youless120.log"
fi

openwbDebugLog ${DMOD} 2 "PV IP: ${wryoulessip}"
openwbDebugLog ${DMOD} 2 "PV Alternative: ${wryoulessalt}"

bash "$OPENWBBASEDIR/packages/legacy_run.sh" "wr_youless120.youless" "${wryoulessip}" "${wryoulessalt}" >>$MYLOGFILE 2>&1
bash "$OPENWBBASEDIR/packages/legacy_run.sh" "modules.devices.youless.device" "inverter" "${wryoulessip}" "${wryoulessalt}" "1">>$MYLOGFILE 2>&1
ret=$?

openwbDebugLog ${DMOD} 2 "RET: ${ret}"

pvwatt=$(</var/www/html/openWB/ramdisk/pvwatt)
echo $pvwatt
cat "${RAMDISKDIR}/pvwatt"
55 changes: 0 additions & 55 deletions modules/wr_youless120/youless.py

This file was deleted.

Empty file.
38 changes: 38 additions & 0 deletions packages/modules/devices/youless/config.py
@@ -0,0 +1,38 @@
from typing import Optional
from helpermodules.auto_str import auto_str
from modules.common.component_setup import ComponentSetup


@auto_str
class YoulessConfiguration:
def __init__(self, ip_address: Optional[str] = None):
self.ip_address = ip_address


@auto_str
class Youless:
def __init__(self,
name: str = "Youless",
type: str = "youless",
id: int = 0,
configuration: YoulessConfiguration = None) -> None:
self.name = name
self.type = type
self.id = id
self.configuration = configuration or YoulessConfiguration()


@auto_str
class YoulessInverterConfiguration:
def __init__(self, source_s0: bool = True):
self.source_s0 = source_s0


@auto_str
class YoulessInverterSetup(ComponentSetup[YoulessInverterConfiguration]):
def __init__(self,
name: str = "Youless LS120 Wechselrichter",
type: str = "inverter",
id: int = 0,
configuration: YoulessInverterConfiguration = None) -> None:
super().__init__(name, type, id, configuration or YoulessInverterConfiguration())
65 changes: 65 additions & 0 deletions packages/modules/devices/youless/device.py
@@ -0,0 +1,65 @@
#!/usr/bin/env python3
import logging
from typing import Iterable, Optional, List

from helpermodules.cli import run_using_positional_cli_args
from modules.common import req
from modules.common.abstract_device import DeviceDescriptor
from modules.common.configurable_device import ConfigurableDevice, ComponentFactoryByType, MultiComponentUpdater
from modules.devices.youless import inverter
from modules.devices.youless.config import Youless, YoulessConfiguration, YoulessInverterSetup
from modules.devices.youless.inverter import YoulessInverter

log = logging.getLogger(__name__)


def create_device(device_config: Youless):
def create_inverter_component(component_config: YoulessInverterSetup):
return YoulessInverter(component_config)

def update_components(components: Iterable[YoulessInverter]):
response = req.get_http_session().get("http://"+device_config.configuration.ip_address+'/a',
params=(('f', 'j'),),
timeout=5).json()
for component in components:
component.update(response)

return ConfigurableDevice(
device_config=device_config,
component_factory=ComponentFactoryByType(
inverter=create_inverter_component,
),
component_updater=MultiComponentUpdater(update_components)
)


COMPONENT_TYPE_TO_MODULE = {
"inverter": inverter
}


def read_legacy(component_type: str, ip_address: str, source_s0: int, num: Optional[int]) -> None:
device_config = Youless(configuration=YoulessConfiguration(ip_address=ip_address))
dev = create_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
component_config.configuration.source_s0 = True if source_s0 == 0 else False
dev.add_component(component_config)

log.debug('Youless IP-Adresse: ' + ip_address)
log.debug('Youless source_s0: ' + str(source_s0))

dev.update()


def main(argv: List[str]):
run_using_positional_cli_args(read_legacy, argv)


device_descriptor = DeviceDescriptor(configuration_factory=Youless)
30 changes: 30 additions & 0 deletions packages/modules/devices/youless/inverter.py
@@ -0,0 +1,30 @@
#!/usr/bin/env python3
from modules.common.component_state import InverterState
from modules.common.component_type import ComponentDescriptor
from modules.common.fault_state import ComponentInfo
from modules.common.store import get_inverter_value_store
from modules.devices.youless.config import YoulessInverterSetup


class YoulessInverter:
def __init__(self, component_config: YoulessInverterSetup) -> None:
self.component_config = component_config
self.store = get_inverter_value_store(self.component_config.id)
self.component_info = ComponentInfo.from_component_config(self.component_config)

def update(self, response) -> None:
if self.component_config.configuration.source_s0:
power = int(response["ps0"])
exported = int(response["cs0"].replace(",", ""))
else:
power = int(response["pwr"])
exported = int(response["cnt"].replace(",", ""))

inverter_state = InverterState(
power=-abs(power),
exported=exported,
)
self.store.set(inverter_state)


component_descriptor = ComponentDescriptor(configuration_factory=YoulessInverterSetup)

0 comments on commit 274690b

Please sign in to comment.