From 70ed838571818477d28f268e1958652e0e0ad6b5 Mon Sep 17 00:00:00 2001 From: Guido Schmitz Date: Mon, 28 Sep 2020 17:37:46 +0200 Subject: [PATCH] Improve devolo Home Control code quality (#40708) --- .../components/devolo_home_control/binary_sensor.py | 4 +--- .../components/devolo_home_control/devolo_device.py | 12 +++++++++--- .../components/devolo_home_control/sensor.py | 4 +--- .../components/devolo_home_control/switch.py | 4 +--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/devolo_home_control/binary_sensor.py b/homeassistant/components/devolo_home_control/binary_sensor.py index ab07df9f770..f884ce0219d 100644 --- a/homeassistant/components/devolo_home_control/binary_sensor.py +++ b/homeassistant/components/devolo_home_control/binary_sensor.py @@ -131,8 +131,6 @@ class DevoloRemoteControl(DevoloDeviceEntity, BinarySensorEntity): message[0] == self._remote_control_property.element_uid and message[1] == 0 ): self._state = False - elif message[0].startswith("hdm"): - self._available = self._device_instance.is_online() else: - _LOGGER.debug("No valid message received: %s", message) + self._generic_message(message) self.schedule_update_ha_state() diff --git a/homeassistant/components/devolo_home_control/devolo_device.py b/homeassistant/components/devolo_home_control/devolo_device.py index 4e47b19806a..651e1947bb6 100644 --- a/homeassistant/components/devolo_home_control/devolo_device.py +++ b/homeassistant/components/devolo_home_control/devolo_device.py @@ -78,11 +78,17 @@ class DevoloDeviceEntity(Entity): return self._available def _sync(self, message): - """Update the binary sensor state.""" + """Update the state.""" if message[0] == self._unique_id: self._value = message[1] - elif message[0].startswith("hdm"): + else: + self._generic_message(message) + self.schedule_update_ha_state() + + def _generic_message(self, message): + """Handle unexpected messages.""" + if message[0].startswith("hdm"): + # Maybe the API wants to tell us, that the device went on- or offline. self._available = self._device_instance.is_online() else: _LOGGER.debug("No valid message received: %s", message) - self.schedule_update_ha_state() diff --git a/homeassistant/components/devolo_home_control/sensor.py b/homeassistant/components/devolo_home_control/sensor.py index 15e3ab47ef5..bbd16dc8560 100644 --- a/homeassistant/components/devolo_home_control/sensor.py +++ b/homeassistant/components/devolo_home_control/sensor.py @@ -140,8 +140,6 @@ class DevoloConsumptionEntity(DevoloMultiLevelDeviceEntity): self._device_instance.consumption_property[self._unique_id], self._sensor_type, ) - elif message[0].startswith("hdm"): - self._available = self._device_instance.is_online() else: - _LOGGER.debug("No valid message received: %s", message) + self._generic_message(message) self.schedule_update_ha_state() diff --git a/homeassistant/components/devolo_home_control/switch.py b/homeassistant/components/devolo_home_control/switch.py index d668cf3071a..bc62dbfe2b9 100644 --- a/homeassistant/components/devolo_home_control/switch.py +++ b/homeassistant/components/devolo_home_control/switch.py @@ -83,8 +83,6 @@ class DevoloSwitch(DevoloDeviceEntity, SwitchEntity): self._consumption = self._device_instance.consumption_property[ message[0] ].current - elif message[0].startswith("hdm"): - self._available = self._device_instance.is_online() else: - _LOGGER.debug("No valid message received: %s", message) + self._generic_message(message) self.schedule_update_ha_state()