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

Feature growatt #1939

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
61 changes: 61 additions & 0 deletions packages/modules/devices/growatt/growatt/bat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env python3
from typing import Dict, Union

from dataclass_utils import dataclass_from_dict
from modules.common.abstract_device import AbstractBat
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.growatt.growatt.config import GrowattBatSetup
from modules.devices.growatt.growatt.version import GrowattVersion


class GrowattBat(AbstractBat):
def __init__(self,
component_config: Union[Dict, GrowattBatSetup],
modbus_id: int,
version: GrowattVersion) -> None:
self.__modbus_id = modbus_id
self.version = version
self.component_config = dataclass_from_dict(GrowattBatSetup, 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:
if self.version == GrowattVersion.max_series:
power_in = client.read_input_registers(
1011, ModbusDataType.UINT_32, unit=self.__modbus_id) * -0.1
power_out = client.read_input_registers(
1009, ModbusDataType.UINT_32, unit=self.__modbus_id) * 0.1
power = power_in + power_out

soc = client.read_input_registers(1014, ModbusDataType.UINT_16, unit=self.__modbus_id)
imported = client.read_input_registers(
1058, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100
exported = client.read_input_registers(
1054, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100
else:
power_in = client.read_input_registers(
3180, ModbusDataType.UINT_32, unit=self.__modbus_id) * -0.1
power_out = client.read_input_registers(
3178, ModbusDataType.UINT_32, unit=self.__modbus_id) * 0.1
power = power_in + power_out

soc = client.read_input_registers(3171, ModbusDataType.UINT_16, unit=self.__modbus_id)
imported = client.read_input_registers(
3131, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100
exported = client.read_input_registers(
3127, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100

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


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

from modules.common.component_setup import ComponentSetup
from ..vendor import vendor_descriptor
from modules.devices.growatt.growatt.version import GrowattVersion


class GrowattConfiguration:
def __init__(self, modbus_id: int = 1,
ip_address: Optional[str] = None,
port: int = 502,
version: GrowattVersion = GrowattVersion.max_series):
self.modbus_id = modbus_id
self.ip_address = ip_address
self.port = port
self.version = version


class Growatt:
def __init__(self,
name: str = "Growatt",
type: str = "growatt",
id: int = 0,
configuration: GrowattConfiguration = None) -> None:
self.name = name
self.type = type
self.vendor = vendor_descriptor.configuration_factory().type
self.id = id
self.configuration = configuration or GrowattConfiguration()


class GrowattBatConfiguration:
def __init__(self):
pass


class GrowattBatSetup(ComponentSetup[GrowattBatConfiguration]):
def __init__(self,
name: str = "Growatt Speicher",
type: str = "bat",
id: int = 0,
configuration: GrowattBatConfiguration = None) -> None:
super().__init__(name, type, id, configuration or GrowattBatConfiguration())


class GrowattCounterConfiguration:
def __init__(self):
pass


class GrowattCounterSetup(ComponentSetup[GrowattCounterConfiguration]):
def __init__(self,
name: str = "Growatt Zähler",
type: str = "counter",
id: int = 0,
configuration: GrowattCounterConfiguration = None) -> None:
super().__init__(name, type, id, configuration or GrowattCounterConfiguration())


class GrowattInverterConfiguration:
def __init__(self):
pass


class GrowattInverterSetup(ComponentSetup[GrowattInverterConfiguration]):
def __init__(self,
name: str = "Growatt Wechselrichter",
type: str = "inverter",
id: int = 0,
configuration: GrowattInverterConfiguration = None) -> None:
super().__init__(name, type, id, configuration or GrowattInverterConfiguration())
72 changes: 72 additions & 0 deletions packages/modules/devices/growatt/growatt/counter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env python3
from typing import Dict, Union

from dataclass_utils import dataclass_from_dict
from modules.common.abstract_device import AbstractCounter
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.growatt.growatt.config import GrowattCounterSetup
from modules.devices.growatt.growatt.version import GrowattVersion


class GrowattCounter(AbstractCounter):
def __init__(self,
component_config: Union[Dict, GrowattCounterSetup],
modbus_id: int,
version: GrowattVersion) -> None:
self.component_config = dataclass_from_dict(GrowattCounterSetup, component_config)
self.__modbus_id = modbus_id
self.version = version
self.store = get_counter_value_store(self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))

def update(self, client: ModbusTcpClient_):
if self.version == GrowattVersion.max_series:
power_in = client.read_input_registers(1021, ModbusDataType.UINT_32, unit=self.__modbus_id) * 0.1
power_out = client.read_input_registers(1029, ModbusDataType.UINT_32, unit=self.__modbus_id) * -0.1
power = power_in + power_out

powers = [
client.read_input_registers(
40, ModbusDataType.INT_32, unit=self.__modbus_id) / 10,
client.read_input_registers(
44, ModbusDataType.INT_32, unit=self.__modbus_id) / 10,
client.read_input_registers(
48, ModbusDataType.INT_32, unit=self.__modbus_id) / 10]

# Einheit 0.1 kWh
exported = client.read_input_registers(1050, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100
imported = client.read_input_registers(1046, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100

# TL-X Dokumentation hat die gleichen Register wie die MAX Serie,
# zusätzlich sind aber auch unten abweichende enthalten
else:
power_in = client.read_input_registers(3041, ModbusDataType.UINT_32, unit=self.__modbus_id) * 0.1
power_out = client.read_input_registers(3043, ModbusDataType.UINT_32, unit=self.__modbus_id) * -0.1
power = power_in + power_out

powers = [
client.read_input_registers(
3028, ModbusDataType.INT_32, unit=self.__modbus_id) / 10,
client.read_input_registers(
3032, ModbusDataType.INT_32, unit=self.__modbus_id) / 10,
client.read_input_registers(
3036, ModbusDataType.INT_32, unit=self.__modbus_id) / 10]

# Einheit 0.1 kWh
exported = client.read_input_registers(3073, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100
imported = client.read_input_registers(3069, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100

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


component_descriptor = ComponentDescriptor(configuration_factory=GrowattCounterSetup)
55 changes: 55 additions & 0 deletions packages/modules/devices/growatt/growatt/device.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/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.growatt.growatt.bat import GrowattBat
from modules.devices.growatt.growatt.config import Growatt, GrowattBatSetup, GrowattCounterSetup, GrowattInverterSetup
from modules.devices.growatt.growatt.counter import GrowattCounter
from modules.devices.growatt.growatt.inverter import GrowattInverter

log = logging.getLogger(__name__)


def create_device(device_config: Growatt):
def create_bat_component(component_config: GrowattBatSetup):
return GrowattBat(component_config,
device_config.configuration.modbus_id,
device_config.configuration.version)

def create_counter_component(component_config: GrowattCounterSetup):
return GrowattCounter(component_config,
device_config.configuration.modbus_id,
device_config.configuration.version)

def create_inverter_component(component_config: GrowattInverterSetup):
return GrowattInverter(component_config,
device_config.configuration.modbus_id,
device_config.configuration.version)

def update_components(components: Iterable[Union[GrowattBat, GrowattCounter, GrowattInverter]]):
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=Growatt)
45 changes: 45 additions & 0 deletions packages/modules/devices/growatt/growatt/inverter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
from typing import Dict, Union

from dataclass_utils import dataclass_from_dict
from modules.common.abstract_device import AbstractInverter
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.growatt.growatt.config import GrowattInverterSetup
from modules.devices.growatt.growatt.version import GrowattVersion


class GrowattInverter(AbstractInverter):
def __init__(self,
component_config: Union[Dict, GrowattInverterSetup],
modbus_id: int,
version: GrowattVersion) -> None:
self.component_config = dataclass_from_dict(GrowattInverterSetup, component_config)
self.__modbus_id = modbus_id
self.version = version
self.store = get_inverter_value_store(self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))

def update(self, client: ModbusTcpClient_) -> None:
if self.version == GrowattVersion.max_series:
power = client.read_input_registers(
1, ModbusDataType.UINT_32, unit=self.__modbus_id) / 10
exported = client.read_input_registers(
91, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100
else:
power = client.read_input_registers(
3001, ModbusDataType.UINT_32, unit=self.__modbus_id) / 10
exported = client.read_input_registers(
3053, ModbusDataType.UINT_32, unit=self.__modbus_id) * 100

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


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


class GrowattVersion(Enum):
tlx = "TL-X"
max_series = "MAX"
14 changes: 14 additions & 0 deletions packages/modules/devices/growatt/vendor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from pathlib import Path

from modules.common.abstract_device import DeviceDescriptor
from modules.devices.vendors import VendorGroup


class Vendor:
def __init__(self):
self.type = Path(__file__).parent.name
self.vendor = "Growatt"
self.group = VendorGroup.VENDORS.value


vendor_descriptor = DeviceDescriptor(configuration_factory=Vendor)