Skip to content
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
2 changes: 1 addition & 1 deletion custom_components/victron/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def is_on(self) -> bool:
self.description.slave,
self.description.key,
)
return cast(bool, data)
return cast("bool", data)

@property
def available(self) -> bool:
Expand Down
54 changes: 23 additions & 31 deletions custom_components/victron/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import logging

import pymodbus
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder

if "3.7.0" <= pymodbus.__version__ <= "3.7.4":
from pymodbus.pdu.register_read_message import ReadHoldingRegistersResponse
Expand Down Expand Up @@ -112,42 +110,36 @@ async def _async_update_data(self) -> dict:
def parse_register_data(
self,
buffer: ReadHoldingRegistersResponse,
registerInfo: OrderedDict(str, RegisterInfo),
registerInfo: OrderedDict[str, RegisterInfo],
unit: int,
) -> dict:
"""Parse the register data."""
decoder = BinaryPayloadDecoder.fromRegisters(
buffer.registers, byteorder=Endian.BIG
)
"""Parse the register data using convert_from_registers."""
decoded_data = OrderedDict()
registers = buffer.registers
offset = 0

for key, value in registerInfo.items():
full_key = str(unit) + "." + key
if value.dataType == UINT16:
decoded_data[full_key] = self.decode_scaling(
decoder.decode_16bit_uint(), value.scale, value.unit
)
elif value.dataType == INT16:
decoded_data[full_key] = self.decode_scaling(
decoder.decode_16bit_int(), value.scale, value.unit
)
elif value.dataType == UINT32:
decoded_data[full_key] = self.decode_scaling(
decoder.decode_32bit_uint(), value.scale, value.unit
)
elif value.dataType == INT32:
decoded_data[full_key] = self.decode_scaling(
decoder.decode_32bit_int(), value.scale, value.unit
)
full_key = f"{unit}.{key}"
count = 0
if value.dataType in (INT16, UINT16):
count = 1
elif value.dataType in (INT32, UINT32):
count = 2
elif isinstance(value.dataType, STRING):
decoded_data[full_key] = (
decoder.decode_string(value.dataType.readLength)
.split(b"\x00")[0]
.decode()
)
count = value.dataType.length
segment = registers[offset : offset + count]

if isinstance(value.dataType, STRING):
raw = self.api.convert_string_from_register(segment)
decoded_data[full_key] = raw
else:
raise DecodeDataTypeUnsupported(
f"Not supported dataType: {value.dataType}"
raw = self.api.convert_number_from_register(segment, value.dataType)
# _LOGGER.warning("trying to decode %s with value %s", key, raw)
decoded_data[full_key] = self.decode_scaling(
raw, value.scale, value.unit
)
offset += count

return decoded_data

def decode_scaling(self, number, scale, unit):
Expand Down
36 changes: 35 additions & 1 deletion custom_components/victron/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@

from homeassistant.exceptions import HomeAssistantError

from .const import INT32, STRING, UINT32, register_info_dict, valid_unit_ids
from .const import (
INT16,
INT32,
STRING,
UINT16,
UINT32,
register_info_dict,
valid_unit_ids,
)

_LOGGER = logging.getLogger(__name__)

Expand All @@ -27,6 +35,32 @@ def is_still_connected(self):
"""Check if the connection is still open."""
return self._client.is_socket_open()

def convert_string_from_register(self, segment, string_encoding="ascii"):
"""Convert from registers to the appropriate data type."""
return self._client.convert_from_registers(
segment, self._client.DATATYPE.STRING, string_encoding="ascii"
).split("\x00")[0]

def convert_number_from_register(self, segment, dataType):
"""Convert from registers to the appropriate data type."""
if dataType == UINT16:
raw = self._client.convert_from_registers(
segment, data_type=self._client.DATATYPE.UINT16
)
elif dataType == INT16:
raw = self._client.convert_from_registers(
segment, data_type=self._client.DATATYPE.INT16
)
elif dataType == UINT32:
raw = self._client.convert_from_registers(
segment, data_type=self._client.DATATYPE.UINT32
)
elif dataType == INT32:
raw = self._client.convert_from_registers(
segment, data_type=self._client.DATATYPE.INT32
)
return raw

def connect(self):
"""Connect to the Modbus TCP server."""
return self._client.connect()
Expand Down
1 change: 0 additions & 1 deletion custom_components/victron/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ def _handle_coordinator_update(self) -> None:
"The reported value %s for entity %s isn't a decobale value. Please report this error to the integrations maintainer",
data,
self._attr_name,
exc_info=1,
)
else:
self._attr_native_value = data
Expand Down
2 changes: 1 addition & 1 deletion custom_components/victron/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def is_on(self) -> bool:
self.description.slave,
self.description.key,
)
return cast(bool, data)
return cast("bool", data)

@property
def available(self) -> bool:
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,6 @@ select = [
"S317", # suspicious-xml-sax-usage
"S318", # suspicious-xml-mini-dom-usage
"S319", # suspicious-xml-pull-dom-usage
"S320", # suspicious-xmle-tree-usage
"S601", # paramiko-call
"S602", # subprocess-popen-with-shell-equals-true
"S604", # call-with-shell-equals-true
Expand Down
Loading