-
Notifications
You must be signed in to change notification settings - Fork 116
fix kostal counter power peaks #2579
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 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
88 changes: 32 additions & 56 deletions
88
packages/modules/devices/kostal/kostal_plenticore/device.py
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 |
|---|---|---|
| @@ -1,88 +1,64 @@ | ||
| # !/usr/bin/env python3 | ||
| from enum import IntEnum | ||
| from typing import Any, Callable, Iterable, Union | ||
| from pymodbus.constants import Endian | ||
| import functools | ||
| #!/usr/bin/env python3 | ||
| import logging | ||
| from typing import Iterable, Union | ||
|
|
||
| from modules.common import modbus | ||
| from modules.common.abstract_device import DeviceDescriptor | ||
| from modules.common.configurable_device import ConfigurableDevice, ComponentFactoryByType, MultiComponentUpdater | ||
| from modules.common.modbus import ModbusTcpClient_ | ||
| from modules.devices.kostal.kostal_plenticore.bat import KostalPlenticoreBat | ||
| from modules.devices.kostal.kostal_plenticore.inverter import KostalPlenticoreInverter | ||
| from modules.devices.kostal.kostal_plenticore.config import (KostalPlenticore, KostalPlenticoreBatSetup, | ||
| KostalPlenticoreCounterSetup, | ||
| KostalPlenticoreInverterSetup) | ||
| from modules.devices.kostal.kostal_plenticore.counter import KostalPlenticoreCounter | ||
|
|
||
| from modules.devices.kostal.kostal_plenticore.inverter import KostalPlenticoreInverter | ||
| from modules.devices.kostal.kostal_plenticore.config import KostalPlenticore, KostalPlenticoreBatSetup | ||
| from modules.devices.kostal.kostal_plenticore.config import KostalPlenticoreCounterSetup, KostalPlenticoreInverterSetup | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class LegacyCounterPosition(IntEnum): | ||
| HOME_CONSUMPTION = 0 | ||
| GRID = 1 | ||
|
|
||
|
|
||
| def update( | ||
| components: Iterable[Union[KostalPlenticoreBat, KostalPlenticoreCounter, KostalPlenticoreInverter]], | ||
| reader: Callable[[int, modbus.ModbusDataType], Any], | ||
| set_inverter_state: bool = True): | ||
| battery = next((component for component in components if isinstance(component, KostalPlenticoreBat)), None) | ||
| bat_state = battery.read_state(reader) if battery else None | ||
| for component in components: | ||
| if isinstance(component, KostalPlenticoreInverter): | ||
| # Fürs erste nur die WR-Werte nutzen ohne Verlustberechnung. | ||
| # power: R575(inverter generation power (actual)) | ||
| # exported: R320 (Total yield) | ||
| inverter_state = component.read_state(reader) | ||
| pv_state = inverter_state | ||
| if set_inverter_state: | ||
| component.update(pv_state) | ||
| elif isinstance(component, KostalPlenticoreCounter): | ||
| component.update(reader) | ||
| if bat_state: | ||
| battery.update(bat_state) | ||
| if set_inverter_state is False: | ||
| return pv_state | ||
|
|
||
|
|
||
| def create_device(device_config: KostalPlenticore): | ||
| client = None | ||
| reader = None | ||
|
|
||
| def create_bat_component(component_config: KostalPlenticoreBatSetup): | ||
| return KostalPlenticoreBat(component_config, device_id=device_config.id) | ||
| nonlocal client | ||
| return KostalPlenticoreBat(component_config, | ||
| device_id=device_config.id, | ||
| modbus_id=device_config.configuration.modbus_id, | ||
| client=client) | ||
|
|
||
| def create_counter_component(component_config: KostalPlenticoreCounterSetup): | ||
| return KostalPlenticoreCounter(component_config, device_id=device_config.id) | ||
| nonlocal client | ||
| return KostalPlenticoreCounter(component_config, | ||
| device_id=device_config.id, | ||
| modbus_id=device_config.configuration.modbus_id, | ||
| client=client) | ||
|
|
||
| def create_inverter_component(component_config: KostalPlenticoreInverterSetup): | ||
| return KostalPlenticoreInverter(component_config) | ||
| nonlocal client | ||
| return KostalPlenticoreInverter(component_config, | ||
| device_id=device_config.id, | ||
| modbus_id=device_config.configuration.modbus_id, | ||
| client=client) | ||
|
|
||
| def update_components( | ||
| components: Iterable[Union[KostalPlenticoreBat, KostalPlenticoreCounter, KostalPlenticoreInverter]] | ||
| ): | ||
| nonlocal client, reader | ||
| components: Iterable[Union[KostalPlenticoreBat, KostalPlenticoreCounter, KostalPlenticoreInverter]]): | ||
| nonlocal client | ||
| with client: | ||
| update(components, reader) | ||
| for component in components: | ||
| component.update() | ||
|
|
||
| def initializer(): | ||
| nonlocal client, reader | ||
| client = modbus.ModbusTcpClient_(device_config.configuration.ip_address, device_config.configuration.port) | ||
| reader = _create_reader(client, device_config.configuration.modbus_id) | ||
| nonlocal client | ||
| client = ModbusTcpClient_(device_config.configuration.ip_address, device_config.configuration.port) | ||
|
|
||
| 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), | ||
| bat=create_bat_component, | ||
| counter=create_counter_component, | ||
| inverter=create_inverter_component, | ||
| ), | ||
| component_updater=MultiComponentUpdater(update_components) | ||
| ) | ||
|
|
||
|
|
||
| def _create_reader(tcp_client: modbus.ModbusTcpClient_, modbus_id: int) -> Callable[[int, modbus.ModbusDataType], Any]: | ||
| return functools.partial(tcp_client.read_holding_registers, unit=modbus_id, wordorder=Endian.Little) | ||
|
|
||
|
|
||
| device_descriptor = DeviceDescriptor(configuration_factory=KostalPlenticore) |
35 changes: 21 additions & 14 deletions
35
packages/modules/devices/kostal/kostal_plenticore/inverter.py
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 |
|---|---|---|
| @@ -1,37 +1,44 @@ | ||
| #!/usr/bin/env python3 | ||
| from typing import Any, Callable | ||
| 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.modbus import ModbusDataType | ||
| from modules.common.modbus import ModbusDataType, ModbusTcpClient_ | ||
| from modules.common.simcount import SimCounter | ||
| from modules.common.store import get_inverter_value_store | ||
| from modules.devices.kostal.kostal_plenticore.config import KostalPlenticoreInverterSetup | ||
|
|
||
|
|
||
| class KwargsDict(TypedDict): | ||
| device_id: int | ||
| modbus_id: int | ||
| client: ModbusTcpClient_ | ||
|
|
||
|
|
||
| class KostalPlenticoreInverter(AbstractInverter): | ||
| def __init__(self, component_config: KostalPlenticoreInverterSetup) -> None: | ||
| def __init__(self, component_config: KostalPlenticoreInverterSetup, **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.modbus_id: int = self.kwargs['modbus_id'] | ||
| self.client: ModbusTcpClient_ = 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.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="pv") | ||
|
|
||
| def read_state(self, reader: Callable[[int, ModbusDataType], Any]) -> InverterState: | ||
| # PV-Anlage kann nichts verbrauchen, also ggf. Register-/Rundungsfehler korrigieren. | ||
| power = reader(575, ModbusDataType.INT_16) * -1 | ||
| exported = reader(320, ModbusDataType.FLOAT_32) | ||
| def update(self) -> None: | ||
| power = self.client.read_holding_registers(575, ModbusDataType.INT_16, unit=self.modbus_id) * -1 | ||
| exported = self.client.read_holding_registers(320, ModbusDataType.FLOAT_32, unit=self.modbus_id) | ||
|
|
||
| return InverterState( | ||
| inverter_state = InverterState( | ||
| power=power, | ||
| exported=exported | ||
| ) | ||
|
|
||
| def dc_in_string_1_2(self, reader: Callable[[int, ModbusDataType], Any]): | ||
| return reader(260, ModbusDataType.FLOAT_32) + reader(270, ModbusDataType.FLOAT_32) | ||
|
|
||
| def update(self, state): | ||
| self.store.set(state) | ||
| self.store.set(inverter_state) | ||
|
|
||
|
|
||
| component_descriptor = ComponentDescriptor(configuration_factory=KostalPlenticoreInverterSetup) |
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.