Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Solis inverter #1795

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions packages/modules/devices/solis/bat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python3
import logging
from dataclass_utils import dataclass_from_dict
from modules.common.component_state import BatState
from modules.common.component_type import ComponentDescriptor
from modules.common.fault_state import ComponentInfo, FaultState
from modules.common.modbus import ModbusDataType, ModbusTcpClient_
from modules.common.store import get_bat_value_store
from modules.devices.solis.config import SolisBatSetup

log = logging.getLogger(__name__)


class SolisBat:
def __init__(self, component_config: SolisBatSetup) -> None:
self.component_config = dataclass_from_dict(SolisBatSetup, component_config)
self.store = get_bat_value_store(self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))

def update(self, client: ModbusTcpClient_) -> None:
unit = self.component_config.configuration.modbus_id

power = client.read_holding_registers(33149, ModbusDataType.INT_32, unit=unit) * -1
soc = client.read_holding_registers(31038, ModbusDataType.UINT_16, unit=unit)
# Geladen in kWh
imported = client.read_holding_registers(33161, ModbusDataType.UINT_32, unit=unit) * 1000
# Entladen in kWh * 0,1
exported = client.read_holding_registers(33165, ModbusDataType.UINT_32, unit=unit) * 100

bat_state = BatState(
power=power,
soc=soc,
imported=imported,
exported=exported
)
self.store.set(bat_state)


component_descriptor = ComponentDescriptor(configuration_factory=SolisBatSetup)
72 changes: 72 additions & 0 deletions packages/modules/devices/solis/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from typing import Optional
from helpermodules.auto_str import auto_str

from modules.common.component_setup import ComponentSetup


class SolisConfiguration:
def __init__(self,
ip_address: Optional[str] = None,
port: int = 502):
self.ip_address = ip_address
self.port = port

benderl marked this conversation as resolved.
Show resolved Hide resolved

class Solis:
def __init__(self,
name: str = "Solis",
type: str = "solis",
id: int = 0,
configuration: SolisConfiguration = None) -> None:
self.name = name
self.type = type
self.id = id
self.configuration = configuration or SolisConfiguration()


@auto_str
class SolisBatConfiguration:
def __init__(self, modbus_id: int = 1):
self.modbus_id = modbus_id


@auto_str
class SolisBatSetup(ComponentSetup[SolisBatConfiguration]):
def __init__(self,
name: str = "Solis Speicher",
type: str = "bat",
id: int = 0,
configuration: SolisBatConfiguration = None) -> None:
super().__init__(name, type, id, configuration or SolisBatConfiguration())


@auto_str
class SolisCounterConfiguration:
def __init__(self, modbus_id: int = 1):
self.modbus_id = modbus_id


@auto_str
class SolisCounterSetup(ComponentSetup[SolisCounterConfiguration]):
def __init__(self,
name: str = "Solis Zähler",
type: str = "counter",
id: int = 0,
configuration: SolisCounterConfiguration = None) -> None:
super().__init__(name, type, id, configuration or SolisCounterConfiguration())


@auto_str
class SolisInverterConfiguration:
def __init__(self, modbus_id: int = 1):
self.modbus_id = modbus_id


@auto_str
class SolisInverterSetup(ComponentSetup[SolisInverterConfiguration]):
def __init__(self,
name: str = "Solis Wechselrichter",
type: str = "inverter",
id: int = 0,
configuration: SolisInverterConfiguration = None) -> None:
super().__init__(name, type, id, configuration or SolisInverterConfiguration())
46 changes: 46 additions & 0 deletions packages/modules/devices/solis/counter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
from dataclass_utils import dataclass_from_dict
from modules.common.component_state import CounterState
from modules.common.component_type import ComponentDescriptor
from modules.common.fault_state import ComponentInfo, FaultState
from modules.common.modbus import ModbusDataType, ModbusTcpClient_
from modules.common.store import get_counter_value_store
from modules.devices.solis.config import SolisCounterSetup
from modules.devices.solis.version import SolisVersion


class SolisCounter:
def __init__(self, component_config: SolisCounterSetup,
version: SolisVersion) -> None:
self.component_config = dataclass_from_dict(SolisCounterSetup, component_config)
self.store = get_counter_value_store(self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
self.version = version

def update(self, client: ModbusTcpClient_):
unit = self.component_config.configuration.modbus_id
if self.version == SolisVersion.inverter:
# register offset -1
power = client.read_holding_registers(3262, ModbusDataType.INT_32, unit=unit)
powers = client.read_holding_registers(3256, [ModbusDataType.INT_32]*3, unit=unit)
frequency = client.read_holding_registers(3281, ModbusDataType.UINT_16, unit=unit) / 100
imported = client.read_holding_registers(3282, ModbusDataType.UINT_32, unit=unit) * 10
exported = client.read_holding_registers(3284, ModbusDataType.UINT_32, unit=unit) * 10
elif self.version == SolisVersion.hybrid:
power = client.read_holding_registers(33263, ModbusDataType.INT_32, unit=unit)
powers = client.read_holding_registers(33257, [ModbusDataType.INT_32]*3, unit=unit)
frequency = client.read_holding_registers(33282, ModbusDataType.UINT_16, unit=unit) / 100
imported = client.read_holding_registers(33283, ModbusDataType.UINT_32, unit=unit) * 10
exported = client.read_holding_registers(33285, ModbusDataType.UINT_32, unit=unit) * 10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wenn Du das offset in eine Variable packst, wird es übersichtlicher:

Suggested change
if self.version == SolisVersion.inverter:
# register offset -1
power = client.read_holding_registers(3262, ModbusDataType.INT_32, unit=unit)
powers = client.read_holding_registers(3256, [ModbusDataType.INT_32]*3, unit=unit)
frequency = client.read_holding_registers(3281, ModbusDataType.UINT_16, unit=unit) / 100
imported = client.read_holding_registers(3282, ModbusDataType.UINT_32, unit=unit) * 10
exported = client.read_holding_registers(3284, ModbusDataType.UINT_32, unit=unit) * 10
elif self.version == SolisVersion.hybrid:
power = client.read_holding_registers(33263, ModbusDataType.INT_32, unit=unit)
powers = client.read_holding_registers(33257, [ModbusDataType.INT_32]*3, unit=unit)
frequency = client.read_holding_registers(33282, ModbusDataType.UINT_16, unit=unit) / 100
imported = client.read_holding_registers(33283, ModbusDataType.UINT_32, unit=unit) * 10
exported = client.read_holding_registers(33285, ModbusDataType.UINT_32, unit=unit) * 10
if self.version == SolisVersion.inverter:
register_offset = 0
elif self.version == SolisVersion.hybrid:
register_offset = 1
power = client.read_holding_registers(3262 + register_offset, ModbusDataType.INT_32, unit=unit)
powers = client.read_holding_registers(3256 + register_offset, [ModbusDataType.INT_32]*3, unit=unit)
frequency = client.read_holding_registers(3281 + register_offset, ModbusDataType.UINT_16, unit=unit) / 100
imported = client.read_holding_registers(3282 + register_offset, ModbusDataType.UINT_32, unit=unit) * 10
exported = client.read_holding_registers(3284 + register_offset, ModbusDataType.UINT_32, unit=unit) * 10


counter_state = CounterState(
imported=imported,
exported=exported,
power=power,
powers=powers,
frequency=frequency
)
self.store.set(counter_state)


component_descriptor = ComponentDescriptor(configuration_factory=SolisCounterSetup)
48 changes: 48 additions & 0 deletions packages/modules/devices/solis/device.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3
import logging
from typing import Iterable, Union

from modules.common.abstract_device import DeviceDescriptor
from modules.common.component_context import SingleComponentUpdateContext
from modules.common.configurable_device import ConfigurableDevice, ComponentFactoryByType, MultiComponentUpdater
from modules.common.modbus import ModbusTcpClient_
from modules.devices.solis.bat import SolisBat
from modules.devices.solis.counter import SolisCounter
from modules.devices.solis.inverter import SolisInverter
from modules.devices.solis.config import Solis, SolisBatSetup, SolisCounterSetup, SolisInverterSetup

log = logging.getLogger(__name__)


def create_device(device_config: Solis):
def create_bat_component(component_config: SolisBatSetup):
return SolisBat(component_config)

def create_counter_component(component_config: SolisCounterSetup):
return SolisCounter(component_config, device_config.configuration.version)

def create_inverter_component(component_config: SolisInverterSetup):
return SolisInverter(component_config, device_config.configuration.version)

def update_components(components: Iterable[Union[SolisBat, SolisCounter, SolisInverter]]):
with client as c:
for component in components:
with SingleComponentUpdateContext(component.fault_state):
component.update(c)

try:
client = 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(
bat=create_bat_component,
counter=create_counter_component,
inverter=create_inverter_component,
),
component_updater=MultiComponentUpdater(update_components)
)


device_descriptor = DeviceDescriptor(configuration_factory=Solis)
41 changes: 41 additions & 0 deletions packages/modules/devices/solis/inverter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python3
from typing import Dict, Union

from dataclass_utils import dataclass_from_dict
from modules.common.component_state import InverterState
from modules.common.component_type import ComponentDescriptor
from modules.common.fault_state import ComponentInfo, FaultState
from modules.common.modbus import ModbusDataType, ModbusTcpClient_
from modules.common.store import get_inverter_value_store
from modules.devices.solis.config import SolisInverterSetup
from modules.devices.solis.version import SolisVersion


class SolisInverter:
def __init__(self, component_config: Union[Dict, SolisInverterSetup],
version: SolisVersion) -> None:
self.component_config = dataclass_from_dict(SolisInverterSetup, component_config)
self.store = get_inverter_value_store(self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
self.version = version

def update(self, client: ModbusTcpClient_) -> None:
unit = self.component_config.configuration.modbus_id

if self.version == SolisVersion.inverter:
power = client.read_holding_registers(3004, ModbusDataType.UINT_32, unit=unit) * -1
# Unit 1kWh
exported = client.read_holding_registers(3008, ModbusDataType.UINT_32, unit=unit) * 1000
elif self.version == SolisVersion.hybrid:
power = client.read_holding_registers(33057, ModbusDataType.UINT_32, unit=unit) * -1
# Unit 1kWh
exported = client.read_holding_registers(33029, ModbusDataType.UINT_32, unit=unit) * 1000

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


component_descriptor = ComponentDescriptor(configuration_factory=SolisInverterSetup)
6 changes: 6 additions & 0 deletions packages/modules/devices/solis/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from enum import Enum


class SolisVersion(Enum):
inverter = "inverter"
hybrid = "hybrid"