Add battery state for devolo Home Control devices (#41329)

This commit is contained in:
Guido Schmitz 2020-10-06 15:43:12 +02:00 committed by GitHub
parent 0460166c7c
commit bfb00b9bb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 2 deletions

View File

@ -84,8 +84,10 @@ class DevoloDeviceEntity(Entity):
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:

View File

@ -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 @@ from .devolo_device import DevoloDeviceEntity
_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 @@ class DevoloGenericMultiLevelDeviceEntity(DevoloMultiLevelDeviceEntity):
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."""