diff --git a/homeassistant/components/devolo_home_control/devolo_device.py b/homeassistant/components/devolo_home_control/devolo_device.py index 44f3814825e35..48f9b59af4a2a 100644 --- a/homeassistant/components/devolo_home_control/devolo_device.py +++ b/homeassistant/components/devolo_home_control/devolo_device.py @@ -84,8 +84,10 @@ def _sync(self, message): self.schedule_update_ha_state() def _generic_message(self, message): - """Handle unexpected messages.""" - if message[0].startswith("hdm"): + """Handle generic messages.""" + if len(message) == 3 and message[2] == "battery_level": + self._value = message[1] + elif len(message) == 3 and message[2] == "status": # Maybe the API wants to tell us, that the device went on- or offline. self._available = self._device_instance.is_online() else: diff --git a/homeassistant/components/devolo_home_control/sensor.py b/homeassistant/components/devolo_home_control/sensor.py index bbd16dc856099..d32a288784283 100644 --- a/homeassistant/components/devolo_home_control/sensor.py +++ b/homeassistant/components/devolo_home_control/sensor.py @@ -2,12 +2,14 @@ import logging from homeassistant.components.sensor import ( + DEVICE_CLASS_BATTERY, DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_ILLUMINANCE, DEVICE_CLASS_POWER, DEVICE_CLASS_TEMPERATURE, ) from homeassistant.config_entries import ConfigEntry +from homeassistant.const import PERCENTAGE from homeassistant.helpers.typing import HomeAssistantType from .const import DOMAIN @@ -16,6 +18,7 @@ _LOGGER = logging.getLogger(__name__) DEVICE_CLASS_MAPPING = { + "battery": DEVICE_CLASS_BATTERY, "temperature": DEVICE_CLASS_TEMPERATURE, "light": DEVICE_CLASS_ILLUMINANCE, "humidity": DEVICE_CLASS_HUMIDITY, @@ -51,6 +54,14 @@ async def async_setup_entry( consumption=consumption_type, ) ) + if hasattr(device, "battery_level"): + entities.append( + DevoloBatteryEntity( + homecontrol=hass.data[DOMAIN]["homecontrol"], + device_instance=device, + element_uid=f"devolo.BatterySensor:{device.uid}", + ) + ) async_add_entities(entities, False) @@ -104,6 +115,24 @@ def __init__( self._name += f" {self._multi_level_sensor_property.sensor_type}" +class DevoloBatteryEntity(DevoloMultiLevelDeviceEntity): + """Representation of a battery entity within devolo Home Control.""" + + def __init__(self, homecontrol, device_instance, element_uid): + """Initialize a battery sensor.""" + + super().__init__( + homecontrol=homecontrol, + device_instance=device_instance, + element_uid=element_uid, + ) + + self._device_class = DEVICE_CLASS_MAPPING.get("battery") + + self._value = device_instance.battery_level + self._unit = PERCENTAGE + + class DevoloConsumptionEntity(DevoloMultiLevelDeviceEntity): """Representation of a consumption entity within devolo Home Control."""