mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 03:37:07 +00:00
Add devolo current and total consumption to sensors (#38386)
This commit is contained in:
parent
f6aa3e0cdc
commit
007873153e
@ -4,6 +4,7 @@ import logging
|
|||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
DEVICE_CLASS_HUMIDITY,
|
DEVICE_CLASS_HUMIDITY,
|
||||||
DEVICE_CLASS_ILLUMINANCE,
|
DEVICE_CLASS_ILLUMINANCE,
|
||||||
|
DEVICE_CLASS_POWER,
|
||||||
DEVICE_CLASS_TEMPERATURE,
|
DEVICE_CLASS_TEMPERATURE,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@ -18,6 +19,8 @@ DEVICE_CLASS_MAPPING = {
|
|||||||
"temperature": DEVICE_CLASS_TEMPERATURE,
|
"temperature": DEVICE_CLASS_TEMPERATURE,
|
||||||
"light": DEVICE_CLASS_ILLUMINANCE,
|
"light": DEVICE_CLASS_ILLUMINANCE,
|
||||||
"humidity": DEVICE_CLASS_HUMIDITY,
|
"humidity": DEVICE_CLASS_HUMIDITY,
|
||||||
|
"current": DEVICE_CLASS_POWER,
|
||||||
|
"total": DEVICE_CLASS_POWER,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -36,17 +39,39 @@ async def async_setup_entry(
|
|||||||
element_uid=multi_level_sensor,
|
element_uid=multi_level_sensor,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
for device in hass.data[DOMAIN]["homecontrol"].devices.values():
|
||||||
|
if hasattr(device, "consumption_property"):
|
||||||
|
for consumption in device.consumption_property:
|
||||||
|
for consumption_type in ["current", "total"]:
|
||||||
|
entities.append(
|
||||||
|
DevoloConsumptionEntity(
|
||||||
|
homecontrol=hass.data[DOMAIN]["homecontrol"],
|
||||||
|
device_instance=device,
|
||||||
|
element_uid=consumption,
|
||||||
|
consumption=consumption_type,
|
||||||
|
)
|
||||||
|
)
|
||||||
async_add_entities(entities, False)
|
async_add_entities(entities, False)
|
||||||
|
|
||||||
|
|
||||||
class DevoloMultiLevelDeviceEntity(DevoloDeviceEntity):
|
class DevoloMultiLevelDeviceEntity(DevoloDeviceEntity):
|
||||||
"""Representation o a multi level sensor within devolo Home Control."""
|
"""Representation of a multi level sensor within devolo Home Control."""
|
||||||
|
|
||||||
def __init__(self, homecontrol, device_instance, element_uid):
|
def __init__(
|
||||||
|
self,
|
||||||
|
homecontrol,
|
||||||
|
device_instance,
|
||||||
|
element_uid,
|
||||||
|
multi_level_sensor_property=None,
|
||||||
|
sync=None,
|
||||||
|
):
|
||||||
"""Initialize a devolo multi level sensor."""
|
"""Initialize a devolo multi level sensor."""
|
||||||
self._multi_level_sensor_property = device_instance.multi_level_sensor_property[
|
if multi_level_sensor_property is None:
|
||||||
element_uid
|
self._multi_level_sensor_property = (
|
||||||
]
|
device_instance.multi_level_sensor_property[element_uid]
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self._multi_level_sensor_property = multi_level_sensor_property
|
||||||
|
|
||||||
self._state = self._multi_level_sensor_property.value
|
self._state = self._multi_level_sensor_property.value
|
||||||
|
|
||||||
@ -66,7 +91,7 @@ class DevoloMultiLevelDeviceEntity(DevoloDeviceEntity):
|
|||||||
device_instance=device_instance,
|
device_instance=device_instance,
|
||||||
element_uid=element_uid,
|
element_uid=element_uid,
|
||||||
name=name,
|
name=name,
|
||||||
sync=self._sync,
|
sync=self._sync if sync is None else sync,
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -95,3 +120,46 @@ class DevoloMultiLevelDeviceEntity(DevoloDeviceEntity):
|
|||||||
else:
|
else:
|
||||||
_LOGGER.debug("No valid message received: %s", message)
|
_LOGGER.debug("No valid message received: %s", message)
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
|
|
||||||
|
class DevoloConsumptionEntity(DevoloMultiLevelDeviceEntity):
|
||||||
|
"""Representation of a consumption entity within devolo Home Control."""
|
||||||
|
|
||||||
|
def __init__(self, homecontrol, device_instance, element_uid, consumption):
|
||||||
|
"""Initialize a devolo consumption sensor."""
|
||||||
|
self._device_instance = device_instance
|
||||||
|
|
||||||
|
self.value = getattr(
|
||||||
|
device_instance.consumption_property[element_uid], consumption
|
||||||
|
)
|
||||||
|
self.sensor_type = consumption
|
||||||
|
self.unit = getattr(
|
||||||
|
device_instance.consumption_property[element_uid], f"{consumption}_unit"
|
||||||
|
)
|
||||||
|
self.element_uid = element_uid
|
||||||
|
|
||||||
|
super().__init__(
|
||||||
|
homecontrol,
|
||||||
|
device_instance,
|
||||||
|
element_uid,
|
||||||
|
multi_level_sensor_property=self,
|
||||||
|
sync=self._sync,
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self):
|
||||||
|
"""Return the unique ID of the entity."""
|
||||||
|
return f"{self._unique_id}_{self.sensor_type}"
|
||||||
|
|
||||||
|
def _sync(self, message=None):
|
||||||
|
"""Update the consumption sensor state."""
|
||||||
|
if message[0] == self.element_uid:
|
||||||
|
self._state = getattr(
|
||||||
|
self._device_instance.consumption_property[self.element_uid],
|
||||||
|
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.schedule_update_ha_state()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user