-
Notifications
You must be signed in to change notification settings - Fork 118
Deye LSW-Dongle (pysolarman) #3307
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c667201
add support for Deye LSW Dongle
ndrsnhs 547f476
Merge branch 'openWB:master' into deye-pysolarman
ndrsnhs c8338a6
change default port
ndrsnhs 040d781
fix update function
ndrsnhs 193c905
Update packages/modules/devices/deye/deye_solarman/counter.py
LKuemmel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import logging | ||
| from typing import TypedDict, Any | ||
| 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.simcount import SimCounter | ||
| from modules.common.utils.peak_filter import PeakFilter | ||
| from modules.common.store import get_bat_value_store | ||
| from modules.devices.deye.deye_solarman.config import DeyeSolarmanBatSetup | ||
| from modules.devices.deye.deye_solarman.device_type import DeviceType | ||
| from modules.common.component_type import ComponentType | ||
| from pysolarmanv5 import PySolarmanV5 as ModbusSolarmanClient_ | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class KwargsDict(TypedDict): | ||
| device_id: int | ||
| client: ModbusSolarmanClient_ | ||
|
|
||
|
|
||
| class DeyeSolarmanBat(AbstractBat): | ||
| def __init__(self, component_config: DeyeSolarmanBatSetup, **kwargs: Any) -> None: | ||
| self.component_config = component_config | ||
| self.kwargs: KwargsDict = kwargs | ||
|
|
||
| def initialize(self) -> None: | ||
| self.__device_id: int = self.kwargs['device_id'] | ||
| self.client: ModbusSolarmanClient_ = self.kwargs['client'] | ||
| self.store = get_bat_value_store(self.component_config.id) | ||
| self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config)) | ||
| self.peak_filter = PeakFilter(ComponentType.BAT, self.component_config.id, self.fault_state) | ||
| self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="speicher") | ||
| self.device_type = DeviceType(self.client.read_holding_registers(0, 1)[0]) | ||
|
|
||
| def update(self) -> None: | ||
| if self.device_type == DeviceType.SINGLE_PHASE_STRING or self.device_type == DeviceType.SINGLE_PHASE_HYBRID: | ||
| power = self.client.read_holding_registers(190, 1)[0] * -1 | ||
| soc = self.client.read_holding_registers(184, 1)[0] | ||
|
|
||
| if self.device_type == DeviceType.SINGLE_PHASE_HYBRID: | ||
| imported = self.client.read_holding_registers(72, 1)[0] * 100 | ||
| exported = self.client.read_holding_registers(74, 1)[0] * 100 | ||
| imported, exported = self.peak_filter.check_values(power, imported, exported) | ||
|
|
||
| elif self.device_type == DeviceType.SINGLE_PHASE_STRING: | ||
| self.peak_filter.check_values(power) | ||
| imported, exported = self.sim_counter.sim_count(power) | ||
|
|
||
| else: # THREE_PHASE_LV (0x0500, 0x0005), THREE_PHASE_HV (0x0006) | ||
| power = self.client.read_holding_registers(590, 1)[0] * -1 | ||
|
|
||
| if self.device_type == DeviceType.THREE_PHASE_HV: | ||
| power = power * 10 | ||
| soc = self.client.read_holding_registers(588, 1)[0] | ||
| self.peak_filter.check_values(power) | ||
| imported, exported = self.sim_counter.sim_count(power) | ||
|
|
||
| bat_state = BatState( | ||
| power=power, | ||
| soc=soc, | ||
| imported=imported, | ||
| exported=exported | ||
| ) | ||
| self.store.set(bat_state) | ||
|
|
||
|
|
||
| component_descriptor = ComponentDescriptor(configuration_factory=DeyeSolarmanBatSetup) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| from typing import Optional | ||
| from helpermodules.auto_str import auto_str | ||
|
|
||
| from modules.common.component_setup import ComponentSetup | ||
| from ..vendor import vendor_descriptor | ||
|
|
||
|
|
||
| class DeyeSolarmanConfiguration: | ||
| def __init__(self, | ||
| ip_address: Optional[str] = None, | ||
| serial: int = None, | ||
| port: int = 8899, | ||
| modbus_id: int = 1): | ||
| self.ip_address = ip_address | ||
| self.serial = serial | ||
| self.port = port | ||
| self.modbus_id = modbus_id | ||
|
|
||
|
|
||
| class DeyeSolarman: | ||
| def __init__(self, | ||
| name: str = "Deye/Jinko (Anbindung per LSW Dongle)", | ||
| type: str = "deye_solarman", | ||
| id: int = 0, | ||
| configuration: DeyeSolarmanConfiguration = None) -> None: | ||
| self.name = name | ||
| self.type = type | ||
| self.vendor = vendor_descriptor.configuration_factory().type | ||
| self.id = id | ||
| self.configuration = configuration or DeyeSolarmanConfiguration() | ||
|
|
||
|
|
||
| @auto_str | ||
| class DeyeSolarmanBatConfiguration: | ||
| def __init__(self): | ||
| pass | ||
|
|
||
|
|
||
| @auto_str | ||
| class DeyeSolarmanBatSetup(ComponentSetup[DeyeSolarmanBatConfiguration]): | ||
| def __init__(self, | ||
| name: str = "Deye/Jinko Speicher", | ||
| type: str = "bat", | ||
| id: int = 0, | ||
| configuration: DeyeSolarmanBatConfiguration = None) -> None: | ||
| super().__init__(name, type, id, configuration or DeyeSolarmanBatConfiguration()) | ||
|
|
||
|
|
||
| @auto_str | ||
| class DeyeSolarmanCounterConfiguration: | ||
| def __init__(self): | ||
| pass | ||
|
|
||
|
|
||
| @auto_str | ||
| class DeyeSolarmanCounterSetup(ComponentSetup[DeyeSolarmanCounterConfiguration]): | ||
| def __init__(self, | ||
| name: str = "Deye/Jinko Zähler", | ||
| type: str = "counter", | ||
| id: int = 0, | ||
| configuration: DeyeSolarmanCounterConfiguration = None) -> None: | ||
| super().__init__(name, type, id, configuration or DeyeSolarmanCounterConfiguration()) | ||
|
|
||
|
|
||
| @auto_str | ||
| class DeyeSolarmanInverterConfiguration: | ||
| def __init__(self): | ||
| pass | ||
|
|
||
|
|
||
| @auto_str | ||
| class DeyeSolarmanInverterSetup(ComponentSetup[DeyeSolarmanInverterConfiguration]): | ||
| def __init__(self, | ||
| name: str = "Deye/Jinko Wechselrichter", | ||
| type: str = "inverter", | ||
| id: int = 0, | ||
| configuration: DeyeSolarmanInverterConfiguration = None) -> None: | ||
| super().__init__(name, type, id, configuration or DeyeSolarmanInverterConfiguration()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,74 @@ | ||||
| #!/usr/bin/env python3 | ||||
| from typing import TypedDict, Any | ||||
|
|
||||
| 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 | ||||
|
||||
| # from modules.common.modbus import ModbusDataType |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| #!/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.devices.deye.deye_solarman.counter import DeyeSolarmanCounter | ||
| from modules.devices.deye.deye_solarman.inverter import DeyeSolarmanInverter | ||
| from modules.devices.deye.deye_solarman.bat import DeyeSolarmanBat | ||
| from modules.devices.deye.deye_solarman.config import (DeyeSolarman, DeyeSolarmanCounterSetup, | ||
| DeyeSolarmanInverterSetup, DeyeSolarmanBatSetup) | ||
| from pysolarmanv5 import PySolarmanV5 as ModbusSolarmanClient_ | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def create_device(device_config: DeyeSolarman): | ||
| client = None | ||
|
|
||
| def create_bat_component(component_config: DeyeSolarmanBatSetup): | ||
| nonlocal client | ||
| return DeyeSolarmanBat(component_config=component_config, device_id=device_config.id, client=client) | ||
|
|
||
| def create_counter_component(component_config: DeyeSolarmanCounterSetup): | ||
| nonlocal client | ||
| return DeyeSolarmanCounter(component_config=component_config, device_id=device_config.id, client=client) | ||
|
|
||
| def create_inverter_component(component_config: DeyeSolarmanInverterSetup): | ||
| nonlocal client | ||
| return DeyeSolarmanInverter(component_config=component_config, device_id=device_config.id, client=client) | ||
|
|
||
| def update_components(components: Iterable[Union[DeyeSolarmanBat, DeyeSolarmanCounter, DeyeSolarmanInverter]]): | ||
| nonlocal client | ||
| for component in components: | ||
| with SingleComponentUpdateContext(component.fault_state): | ||
| component.update() | ||
|
|
||
| def initializer(): | ||
| nonlocal client | ||
| client = ModbusSolarmanClient_(device_config.configuration.ip_address, | ||
| device_config.configuration.serial, | ||
| port=device_config.configuration.port, | ||
| mb_slave_id=device_config.configuration.modbus_id, | ||
| auto_reconnect=True) | ||
|
|
||
| return ConfigurableDevice( | ||
| device_config=device_config, | ||
| initializer=initializer, | ||
| 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=DeyeSolarman) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| from enum import IntEnum | ||
|
|
||
|
|
||
| class DeviceType(IntEnum): | ||
| SINGLE_PHASE_STRING = 0x0200 | ||
| SINGLE_PHASE_HYBRID = 0x0300 | ||
| THREE_PHASE_LV_0 = 0x0500 | ||
| THREE_PHASE_LV_1 = 0x0005 | ||
| THREE_PHASE_HV = 0x0006 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| #!/usr/bin/env python3 | ||
| from typing import TypedDict, Any | ||
|
|
||
| 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.simcount import SimCounter | ||
| from modules.common.utils.peak_filter import PeakFilter | ||
| from modules.common.store import get_inverter_value_store | ||
| from modules.devices.deye.deye_solarman.config import DeyeSolarmanInverterSetup | ||
| from modules.devices.deye.deye_solarman.device_type import DeviceType | ||
| from modules.common.component_type import ComponentType | ||
| from pysolarmanv5 import PySolarmanV5 as ModbusSolarmanClient_ | ||
|
|
||
|
|
||
| class KwargsDict(TypedDict): | ||
| device_id: int | ||
| client: ModbusSolarmanClient_ | ||
|
|
||
|
|
||
| class DeyeSolarmanInverter(AbstractInverter): | ||
| def __init__(self, component_config: DeyeSolarmanInverterSetup, **kwargs: Any) -> None: | ||
| self.component_config = component_config | ||
| self.kwargs: KwargsDict = kwargs | ||
|
|
||
| def initialize(self) -> None: | ||
| self.__device_id: int = self.kwargs['device_id'] | ||
| self.client: ModbusSolarmanClient_ = self.kwargs['client'] | ||
| self.store = get_inverter_value_store(self.component_config.id) | ||
| self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config)) | ||
| self.peak_filter = PeakFilter(ComponentType.INVERTER, self.component_config.id, self.fault_state) | ||
| self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="pv") | ||
| self.device_type = DeviceType(self.client.read_holding_registers(0, 1)[0]) | ||
|
|
||
| def update(self) -> None: | ||
| if self.device_type == DeviceType.SINGLE_PHASE_STRING or self.device_type == DeviceType.SINGLE_PHASE_HYBRID: | ||
| power = sum(self.client.read_holding_registers(186, 4)) * -1 | ||
|
|
||
| else: # THREE_PHASE_LV (0x0500, 0x0005), THREE_PHASE_HV (0x0006) | ||
| power = sum(self.client.read_holding_registers(672, 2)) * -1 | ||
|
|
||
| if self.device_type == DeviceType.THREE_PHASE_HV: | ||
| power = power * 10 | ||
| self.peak_filter.check_values(power) | ||
| imported, exported = self.sim_counter.sim_count(power) | ||
|
|
||
| inverter_state = InverterState( | ||
| power=power, | ||
| imported=imported, | ||
| exported=exported, | ||
| ) | ||
| self.store.set(inverter_state) | ||
|
|
||
|
|
||
| component_descriptor = ComponentDescriptor(configuration_factory=DeyeSolarmanInverterSetup) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,3 +25,4 @@ websockets==12.0 | |
| pycarwings3==0.7.14 | ||
| asyncio==3.4.3 | ||
| passlib==1.7.4 | ||
| pysolarmanv5==3.0.6 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
serialis annotated asintbut defaults toNone. This is inconsistent with the type hint and makes static analysis / IDE assistance inaccurate. Consider changing it toOptional[int](and/or validating that a serial is provided before creating the client).