Extract fibaro switch energy attributes into sensors (#63697)
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
parent
4bcf71b1f3
commit
62e64eb8ab
2 changed files with 47 additions and 11 deletions
|
@ -29,7 +29,6 @@ from homeassistant.util import convert, slugify
|
|||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ATTR_CURRENT_ENERGY_KWH = "current_energy_kwh"
|
||||
ATTR_CURRENT_POWER_W = "current_power_w"
|
||||
|
||||
CONF_COLOR = "color"
|
||||
|
@ -190,11 +189,13 @@ class FibaroController:
|
|||
except (ValueError, KeyError):
|
||||
pass
|
||||
for item in callback_set:
|
||||
self._callbacks[item]()
|
||||
for callback in self._callbacks[item]:
|
||||
callback()
|
||||
|
||||
def register(self, device_id, callback):
|
||||
"""Register device with a callback for updates."""
|
||||
self._callbacks[device_id] = callback
|
||||
self._callbacks.setdefault(device_id, [])
|
||||
self._callbacks[device_id].append(callback)
|
||||
|
||||
def get_children(self, device_id):
|
||||
"""Get a list of child devices."""
|
||||
|
@ -523,10 +524,6 @@ class FibaroDevice(Entity):
|
|||
attr[ATTR_CURRENT_POWER_W] = convert(
|
||||
self.fibaro_device.properties.power, float, 0.0
|
||||
)
|
||||
if "energy" in self.fibaro_device.interfaces:
|
||||
attr[ATTR_CURRENT_ENERGY_KWH] = convert(
|
||||
self.fibaro_device.properties.energy, float, 0.0
|
||||
)
|
||||
except (ValueError, KeyError):
|
||||
pass
|
||||
|
||||
|
|
|
@ -3,9 +3,15 @@ from __future__ import annotations
|
|||
|
||||
from contextlib import suppress
|
||||
|
||||
from homeassistant.components.sensor import DOMAIN, SensorDeviceClass, SensorEntity
|
||||
from homeassistant.components.sensor import (
|
||||
DOMAIN,
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
CONCENTRATION_PARTS_PER_MILLION,
|
||||
ENERGY_KILO_WATT_HOUR,
|
||||
LIGHT_LUX,
|
||||
PERCENTAGE,
|
||||
TEMP_CELSIUS,
|
||||
|
@ -14,6 +20,7 @@ from homeassistant.const import (
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
from homeassistant.util import convert
|
||||
|
||||
from . import FIBARO_DEVICES, FibaroDevice
|
||||
|
||||
|
@ -57,9 +64,15 @@ def setup_platform(
|
|||
if discovery_info is None:
|
||||
return
|
||||
|
||||
add_entities(
|
||||
[FibaroSensor(device) for device in hass.data[FIBARO_DEVICES]["sensor"]], True
|
||||
)
|
||||
entities: list[SensorEntity] = []
|
||||
for device in hass.data[FIBARO_DEVICES]["sensor"]:
|
||||
entities.append(FibaroSensor(device))
|
||||
for device_type in ("cover", "light", "switch"):
|
||||
for device in hass.data[FIBARO_DEVICES][device_type]:
|
||||
if "energy" in device.interfaces:
|
||||
entities.append(FibaroEnergySensor(device))
|
||||
|
||||
add_entities(entities, True)
|
||||
|
||||
|
||||
class FibaroSensor(FibaroDevice, SensorEntity):
|
||||
|
@ -114,3 +127,29 @@ class FibaroSensor(FibaroDevice, SensorEntity):
|
|||
"""Update the state."""
|
||||
with suppress(KeyError, ValueError):
|
||||
self.current_value = float(self.fibaro_device.properties.value)
|
||||
|
||||
|
||||
class FibaroEnergySensor(FibaroDevice, SensorEntity):
|
||||
"""Representation of a Fibaro Energy Sensor."""
|
||||
|
||||
_attr_device_class = SensorDeviceClass.ENERGY
|
||||
_attr_state_class = SensorStateClass.TOTAL_INCREASING
|
||||
_attr_native_unit_of_measurement = ENERGY_KILO_WATT_HOUR
|
||||
|
||||
def __init__(self, fibaro_device):
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(fibaro_device)
|
||||
self.entity_id = f"{DOMAIN}.{self.ha_id}_energy"
|
||||
self._name = f"{fibaro_device.friendly_name} Energy"
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return a unique ID."""
|
||||
return f"{self.fibaro_device.unique_id_str}_energy"
|
||||
|
||||
def update(self):
|
||||
"""Update the state."""
|
||||
with suppress(KeyError, ValueError):
|
||||
self._attr_native_value = convert(
|
||||
self.fibaro_device.properties.energy, float
|
||||
)
|
||||
|
|
Loading…
Add table
Reference in a new issue