Skip to content

Commit

Permalink
Anpaasungen Black
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsK1 committed Nov 29, 2024
1 parent c6fc294 commit 17167ee
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 97 deletions.
13 changes: 4 additions & 9 deletions custom_components/solvis_control/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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
1 change: 1 addition & 0 deletions custom_components/solvis_control/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 11 additions & 28 deletions custom_components/solvis_control/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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")
Expand Down
16 changes: 4 additions & 12 deletions custom_components/solvis_control/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand Down
20 changes: 5 additions & 15 deletions custom_components/solvis_control/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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:
Expand Down
24 changes: 6 additions & 18 deletions custom_components/solvis_control/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -168,34 +166,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 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(
Expand All @@ -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
Expand Down
21 changes: 6 additions & 15 deletions custom_components/solvis_control/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 17167ee

Please sign in to comment.