diff --git a/custom_components/solvis_control/__init__.py b/custom_components/solvis_control/__init__.py index 51532f6..e2d3656 100644 --- a/custom_components/solvis_control/__init__.py +++ b/custom_components/solvis_control/__init__.py @@ -120,9 +120,7 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry): new_data = {**config_entry.data} if config_entry.minor_version < 3: - _LOGGER.info( - f"Migrating from version {config_entry.version}_{config_entry.minor_version}" - ) + _LOGGER.info(f"Migrating from version {config_entry.version}_{config_entry.minor_version}") if CONF_OPTION_1 not in new_data: new_data[CONF_OPTION_1] = False if CONF_OPTION_2 not in new_data: @@ -135,9 +133,7 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry): new_data[DEVICE_VERSION] = "SC3" current_minor_version = 3 if config_entry.minor_version < 4: - _LOGGER.info( - f"Migrating from version {config_entry.version}_{config_entry.minor_version}" - ) + _LOGGER.info(f"Migrating from version {config_entry.version}_{config_entry.minor_version}") if POLL_RATE_DEFAULT not in new_data: new_data[POLL_RATE_DEFAULT] = 30 if POLL_RATE_SLOW not in new_data: @@ -149,7 +145,6 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry): minor_version=current_minor_version, version=current_version, ) - _LOGGER.info( - f"Migration to version {current_version}_{current_minor_version} successful" - ) + _LOGGER.info(f"Migration to version {current_version}_{current_minor_version} successful") + return True diff --git a/custom_components/solvis_control/const.py b/custom_components/solvis_control/const.py index 2ebc3dd..7aecea6 100644 --- a/custom_components/solvis_control/const.py +++ b/custom_components/solvis_control/const.py @@ -11,6 +11,7 @@ POLL_RATE_DEFAULT = "poll_rate_default" POLL_RATE_SLOW = "poll_rate_slow" + # Option attributes to make certain values configurable CONF_OPTION_1 = "HKR2" # HKR 2 CONF_OPTION_2 = "HKR3" # HKR 3 diff --git a/custom_components/solvis_control/coordinator.py b/custom_components/solvis_control/coordinator.py index 282f015..0a11742 100644 --- a/custom_components/solvis_control/coordinator.py +++ b/custom_components/solvis_control/coordinator.py @@ -59,9 +59,7 @@ async def _async_update_data(self): try: await self.modbus.connect() - _LOGGER.debug( - "Connected to Modbus for Solvis" - ) # Moved here for better context + _LOGGER.debug("Connected to Modbus for Solvis") # Moved here for better context for register in REGISTERS: if not self.option_hkr2 and register.conf_option == 1: @@ -81,9 +79,7 @@ async def _async_update_data(self): if register.poll_rate: if register.poll_time > 0: register.poll_time -= self.poll_rate_default - _LOGGER.debug( - f"Skipping entity {register.name}/{register.address} due to slow poll rate. Remaining time: {register.poll_time}s" - ) + _LOGGER.debug(f"Skipping entity {register.name}/{register.address} due to slow poll rate. Remaining time: {register.poll_time}s") continue if register.poll_time <= 0: register.poll_time = self.poll_rate_slow @@ -95,36 +91,23 @@ async def _async_update_data(self): continue try: if register.register == 1: - result = await self.modbus.read_input_registers( - register.address, 1, 1 - ) + + result = await self.modbus.read_input_registers(register.address, 1, 1) _LOGGER.debug(f"Reading input register {register.name}") else: - result = await self.modbus.read_holding_registers( - register.address, 1, 1 - ) - _LOGGER.debug( - f"Reading holding register {register.name}/{register.address}" - ) - - decoder = BinaryPayloadDecoder.fromRegisters( - result.registers, byteorder=Endian.BIG - ) + result = await self.modbus.read_holding_registers(register.address, 1, 1) + _LOGGER.debug(f"Reading holding register {register.name}/{register.address}") + + decoder = BinaryPayloadDecoder.fromRegisters(result.registers, byteorder=Endian.BIG) try: - value = round( - decoder.decode_16bit_int() * register.multiplier, 2 - ) + 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 - ) + parsed_data[register.name] = abs(value) if register.absolute_value else value except ModbusException as error: - _LOGGER.error( - f"Modbus error reading register {register.name}: {error}" - ) + _LOGGER.error(f"Modbus error reading register {register.name}: {error}") except ConnectionException: _LOGGER.warning("Couldn't connect to Solvis device") diff --git a/custom_components/solvis_control/number.py b/custom_components/solvis_control/number.py index 0138ea2..d04a6b7 100644 --- a/custom_components/solvis_control/number.py +++ b/custom_components/solvis_control/number.py @@ -32,9 +32,7 @@ _LOGGER = logging.getLogger(__name__) -async def async_setup_entry( - hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback -) -> None: +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> None: """Set up Solvis number entities.""" coordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR] @@ -183,16 +181,12 @@ def _handle_coordinator_update(self) -> None: # Validate the data type received from the coordinator if not isinstance(response_data, (int, float, complex, Decimal)): - _LOGGER.warning( - f"Invalid response data type from coordinator. {response_data} has type {type(response_data)}" - ) + _LOGGER.warning(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}" - ) + _LOGGER.warning(f"The coordinator failed to fetch data for entity: {self._response_key}") self._attr_available = False return @@ -206,9 +200,7 @@ async def async_set_native_value(self, value: float) -> None: """Update the current value.""" try: await self.coordinator.modbus.connect() - await self.coordinator.modbus.write_register( - self.modbus_address, int(value / self.multiplier), slave=1 - ) + await self.coordinator.modbus.write_register(self.modbus_address, int(value / self.multiplier), slave=1) except ConnectionException: _LOGGER.warning("Couldn't connect to device") finally: diff --git a/custom_components/solvis_control/select.py b/custom_components/solvis_control/select.py index ed9cf4d..00e1222 100644 --- a/custom_components/solvis_control/select.py +++ b/custom_components/solvis_control/select.py @@ -32,9 +32,7 @@ _LOGGER = logging.getLogger(__name__) -async def async_setup_entry( - hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback -) -> None: +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> None: """Set up Solvis select entities.""" coordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR] @@ -161,34 +159,26 @@ def _handle_coordinator_update(self) -> None: # Validate the data type received from the coordinator if not isinstance(response_data, (int, float, complex, Decimal)): - _LOGGER.warning( - f"Invalid response data type from coordinator. {response_data} has type {type(response_data)}" - ) + _LOGGER.warning(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}" - ) + _LOGGER.warning(f"The coordinator failed to fetch data for entity: {self._response_key}") self._attr_available = False return self._attr_available = True match self.data_processing: case _: - self._attr_current_option = str( - response_data - ) # Update the selected option + self._attr_current_option = str(response_data) # Update the selected option self.async_write_ha_state() async def async_select_option(self, option: str) -> None: """Change the selected option.""" try: await self.coordinator.modbus.connect() - await self.coordinator.modbus.write_register( - self.modbus_address, int(option), slave=1 - ) + await self.coordinator.modbus.write_register(self.modbus_address, int(option), slave=1) except ConnectionException: _LOGGER.warning("Couldn't connect to device") finally: diff --git a/custom_components/solvis_control/sensor.py b/custom_components/solvis_control/sensor.py index 3a98fd8..ee343a0 100644 --- a/custom_components/solvis_control/sensor.py +++ b/custom_components/solvis_control/sensor.py @@ -32,9 +32,7 @@ _LOGGER = logging.getLogger(__name__) -async def async_setup_entry( - hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback -) -> None: +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> None: """Set up Solvis sensor entities.""" coordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR] @@ -168,16 +166,12 @@ def _handle_coordinator_update(self) -> None: # Validate the data type received from the coordinator if not isinstance(response_data, (int, float, complex, Decimal)): - _LOGGER.warning( - f"Invalid response data type from coordinator. {response_data} has type {type(response_data)}" - ) + _LOGGER.warning(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}" - ) + _LOGGER.warning(f"The coordinator failed to fetch data for entity: {self._response_key}") self._attr_available = False return self._attr_available = True @@ -185,17 +179,13 @@ def _handle_coordinator_update(self) -> None: case 1: # Version if len(str(response_data)) == 5: response_data = str(response_data) - self._attr_native_value = ( - f"{response_data[0]}.{response_data[1:3]}.{response_data[3:5]}" - ) + self._attr_native_value = f"{response_data[0]}.{response_data[1:3]}.{response_data[3:5]}" if self._address in (32770, 32771): # Hole den Device-Registry device_registry = async_get(self.hass) # Aktualisiere Geräteinformationen - device = device_registry.async_get_device( - self.device_info.identifiers - ) + device = device_registry.async_get_device(self.device_info.identifiers) if device is not None: if self._address == 32770: device_registry.async_update_device( @@ -210,9 +200,7 @@ def _handle_coordinator_update(self) -> None: else: _LOGGER.warning("Couldn't process version string to Version.") self._attr_native_value = response_data - case ( - 2 - ): # https://github.com/LarsK1/hass_solvis_control/issues/58#issuecomment-2496245943 + case 2: # https://github.com/LarsK1/hass_solvis_control/issues/58#issuecomment-2496245943 self._attr_native_value = ((1 / (response_data / 60)) * 1000) / 42 * 60 case _: self._attr_native_value = response_data # Update the sensor value diff --git a/custom_components/solvis_control/switch.py b/custom_components/solvis_control/switch.py index 05bc0ee..383b679 100644 --- a/custom_components/solvis_control/switch.py +++ b/custom_components/solvis_control/switch.py @@ -32,9 +32,7 @@ _LOGGER = logging.getLogger(__name__) -async def async_setup_entry( - hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback -) -> None: +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> None: """Set up Solvis switch entities.""" coordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR] @@ -159,16 +157,13 @@ def _handle_coordinator_update(self) -> None: # Validate the data type received from the coordinator if not isinstance(response_data, (int, float, complex, Decimal)): - _LOGGER.warning( - f"Invalid response data type from coordinator. {response_data} has type {type(response_data)}" - ) + _LOGGER.warning(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}" - ) + _LOGGER.warning(f"The coordinator failed to fetch data for entity: {self._response_key}") self._attr_available = False return @@ -181,9 +176,7 @@ async def async_turn_on(self, **kwargs) -> None: """Turn the entity on.""" try: await self.coordinator.modbus.connect() - await self.coordinator.modbus.write_register( - self.modbus_address, 1, slave=1 - ) + await self.coordinator.modbus.write_register(self.modbus_address, 1, slave=1) except ConnectionException: _LOGGER.warning("Couldn't connect to device") finally: @@ -193,9 +186,7 @@ async def async_turn_off(self, **kwargs) -> None: """Turn the entity off.""" try: await self.coordinator.modbus.connect() - await self.coordinator.modbus.write_register( - self.modbus_address, 0, slave=1 - ) + await self.coordinator.modbus.write_register(self.modbus_address, 0, slave=1) except ConnectionException: _LOGGER.warning("Couldn't connect to device") finally: