Skip to content

Commit

Permalink
Fixes for SC2 with wrong byte-decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsK1 committed Nov 24, 2024
1 parent 35f7c0e commit fd921d6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
15 changes: 11 additions & 4 deletions custom_components/solvis_control/coordinator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Solvis Modbus Data Coordinator"""

import struct
from datetime import timedelta
import logging

Expand Down Expand Up @@ -71,7 +72,6 @@ async def _async_update_data(self):
if entity_entry and entity_entry.disabled:
_LOGGER.debug(f"Skipping disabled entity: {entity_id}")
continue

try:
if register.register == 1:
result = await self.modbus.read_input_registers(
Expand All @@ -94,9 +94,16 @@ async def _async_update_data(self):
f"Value from previous register before modification: {register_value}"
)
value = round(register_value * register.multiplier, 2)
parsed_data[register.name] = (
abs(value) if register.absolute_value else value
)
try:
value = round(
decoder.decode_16bit_int() * register.multiplier, 2
)
except struct.error:
parsed_data[register.name] = -300
else:
parsed_data[register.name] = (
abs(value) if register.absolute_value else value
)
# if "version" in register.name:
# parsed_data[register.name] = ".".join(
# parsed_data[register.name].split(0, 2)
Expand Down
13 changes: 9 additions & 4 deletions custom_components/solvis_control/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,21 @@ def _handle_coordinator_update(self) -> None:

response_data = self.coordinator.data.get(self._response_key)
if response_data is None:
_LOGGER.warning("No data available for (%s)", self._response_key)
_LOGGER.warning(f"No data available for {self._response_key}")
self._attr_available = False
return

# Validate the data type received from the coordinator
if not isinstance(response_data, (int, float, complex, Decimal)):
_LOGGER.warning(
"Invalid response data type from coordinator. %s has type %s",
response_data,
type(response_data),
f"Invalid response data type from coordinator. {response_data} has type {type(response_data)}"
)
self._attr_available = False
return

if response_data == -300:
_LOGGER.warning(
f"The coordinator failed to fetch data for entity: {self._response_key}"
)
self._attr_available = False
return
Expand Down
13 changes: 9 additions & 4 deletions custom_components/solvis_control/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,21 @@ def _handle_coordinator_update(self) -> None:

response_data = self.coordinator.data.get(self._response_key)
if response_data is None:
_LOGGER.warning("No data available for (%s)", self._response_key)
_LOGGER.warning(f"No data available for {self._response_key}")
self._attr_available = False
return

# Validate the data type received from the coordinator
if not isinstance(response_data, (int, float, complex, Decimal)):
_LOGGER.warning(
"Invalid response data type from coordinator. %s has type %s",
response_data,
type(response_data),
f"Invalid response data type from coordinator. {response_data} has type {type(response_data)}"
)
self._attr_available = False
return

if response_data == -300:
_LOGGER.warning(
f"The coordinator failed to fetch data for entity: {self._response_key}"
)
self._attr_available = False
return
Expand Down
12 changes: 8 additions & 4 deletions custom_components/solvis_control/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,24 @@ def _handle_coordinator_update(self) -> None:

response_data = self.coordinator.data.get(self._response_key)
if response_data is None:
_LOGGER.warning("No data available for (%s)", self._response_key)
_LOGGER.warning(f"No data available for {self._response_key}")
self._attr_available = False
return

# Validate the data type received from the coordinator
if not isinstance(response_data, (int, float, complex, Decimal)):
_LOGGER.warning(
"Invalid response data type from coordinator. %s has type %s",
response_data,
type(response_data),
f"Invalid response data type from coordinator. {response_data} has type {type(response_data)}"
)
self._attr_available = False
return

if response_data == -300:
_LOGGER.warning(
f"The coordinator failed to fetch data for entity: {self._response_key}"
)
self._attr_available = False
return
self._attr_available = True
self._attr_native_value = response_data # Update the sensor value
self.async_write_ha_state()
13 changes: 9 additions & 4 deletions custom_components/solvis_control/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,21 @@ def _handle_coordinator_update(self) -> None:

response_data = self.coordinator.data.get(self._response_key)
if response_data is None:
_LOGGER.warning("No data available for (%s)", self._response_key)
_LOGGER.warning(f"No data available for {self._response_key}")
self._attr_available = False
return

# Validate the data type received from the coordinator
if not isinstance(response_data, (int, float, complex, Decimal)):
_LOGGER.warning(
"Invalid response data type from coordinator. %s has type %s",
response_data,
type(response_data),
f"Invalid response data type from coordinator. {response_data} has type {type(response_data)}"
)
self._attr_available = False
return

if response_data == -300:
_LOGGER.warning(
f"The coordinator failed to fetch data for entity: {self._response_key}"
)
self._attr_available = False
return
Expand Down

0 comments on commit fd921d6

Please sign in to comment.