diff --git a/custom_components/solvis_control/coordinator.py b/custom_components/solvis_control/coordinator.py index 396715e..9ba6448 100644 --- a/custom_components/solvis_control/coordinator.py +++ b/custom_components/solvis_control/coordinator.py @@ -1,5 +1,6 @@ """Solvis Modbus Data Coordinator""" +import struct from datetime import timedelta import logging @@ -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( @@ -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) diff --git a/custom_components/solvis_control/number.py b/custom_components/solvis_control/number.py index 2f2d69d..8666245 100644 --- a/custom_components/solvis_control/number.py +++ b/custom_components/solvis_control/number.py @@ -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 diff --git a/custom_components/solvis_control/select.py b/custom_components/solvis_control/select.py index a8593ed..ecddfbe 100644 --- a/custom_components/solvis_control/select.py +++ b/custom_components/solvis_control/select.py @@ -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 diff --git a/custom_components/solvis_control/sensor.py b/custom_components/solvis_control/sensor.py index 13935be..10a6fbf 100644 --- a/custom_components/solvis_control/sensor.py +++ b/custom_components/solvis_control/sensor.py @@ -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() diff --git a/custom_components/solvis_control/switch.py b/custom_components/solvis_control/switch.py index 0dae73a..6ba1201 100644 --- a/custom_components/solvis_control/switch.py +++ b/custom_components/solvis_control/switch.py @@ -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