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

fix-alpha_ess-counter-bug #1713

Merged
merged 1 commit into from
Jul 1, 2024
Merged
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
21 changes: 11 additions & 10 deletions packages/modules/devices/alpha_ess/counter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
import time
from typing import Dict, Union
from typing import Callable, Dict, Union

from dataclass_utils import dataclass_from_dict
from modules.devices.alpha_ess.config import AlphaEssConfiguration, AlphaEssCounterSetup
Expand Down Expand Up @@ -28,22 +28,23 @@ def __init__(self,

def update(self):
time.sleep(0.1)
counter_state = self.__get_values_factory()
factory_method = self.__get_values_factory()
counter_state = factory_method(self.__modbus_id)
self.store.set(counter_state)

def __get_values_factory(self) -> CounterState:
def __get_values_factory(self) -> Callable[[int], CounterState]:
if self.__device_config.source == 0 and self.__device_config.version == 0:
return self.__get_values_before_v123
else:
return self.__get_values_since_v123

def __get_values_before_v123(self) -> CounterState:
def __get_values_before_v123(self, unit: int) -> CounterState:
power, exported, imported = self.__tcp_client.read_holding_registers(
0x6, [modbus.ModbusDataType.INT_32] * 3, unit=self.__modbus_id)
0x6, [modbus.ModbusDataType.INT_32] * 3, unit=unit)
exported *= 10
imported *= 10
currents = [val / 230 for val in self.__tcp_client.read_holding_registers(
0x0000, [ModbusDataType.INT_32]*3, unit=self.__modbus_id)]
0x0000, [ModbusDataType.INT_32]*3, unit=unit)]

counter_state = CounterState(
currents=currents,
Expand All @@ -53,14 +54,14 @@ def __get_values_before_v123(self) -> CounterState:
)
return counter_state

def __get_values_since_v123(self) -> CounterState:
power = self.__tcp_client.read_holding_registers(0x0021, ModbusDataType.INT_32, unit=self.__modbus_id)
def __get_values_since_v123(self, unit: int) -> CounterState:
power = self.__tcp_client.read_holding_registers(0x0021, ModbusDataType.INT_32, unit=unit)
exported, imported = [
val * 10 for val in self.__tcp_client.read_holding_registers(
0x0010, [ModbusDataType.INT_32] * 2, unit=self.__modbus_id
0x0010, [ModbusDataType.INT_32] * 2, unit=unit
)]
currents = [val / 1000 for val in self.__tcp_client.read_holding_registers(
0x0017, [ModbusDataType.INT_16]*3, unit=self.__modbus_id)]
0x0017, [ModbusDataType.INT_16]*3, unit=unit)]

counter_state = CounterState(
currents=currents,
Expand Down