Add power sensor to fibaro integration (#68848)

This commit is contained in:
rappenze 2022-03-29 21:53:43 +02:00 committed by GitHub
parent 7f5ee53d51
commit 93a66cc357
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,6 +15,7 @@ from homeassistant.const import (
ENERGY_KILO_WATT_HOUR,
LIGHT_LUX,
PERCENTAGE,
POWER_WATT,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
)
@ -68,6 +69,8 @@ async def async_setup_entry(
for device in hass.data[DOMAIN][entry.entry_id][FIBARO_DEVICES][device_type]:
if "energy" in device.interfaces:
entities.append(FibaroEnergySensor(device))
if "power" in device.interfaces:
entities.append(FibaroPowerSensor(device))
async_add_entities(entities, True)
@ -146,3 +149,25 @@ class FibaroEnergySensor(FibaroDevice, SensorEntity):
self._attr_native_value = convert(
self.fibaro_device.properties.energy, float
)
class FibaroPowerSensor(FibaroDevice, SensorEntity):
"""Representation of a Fibaro Power Sensor."""
_attr_device_class = SensorDeviceClass.POWER
_attr_state_class = SensorStateClass.MEASUREMENT
_attr_native_unit_of_measurement = POWER_WATT
def __init__(self, fibaro_device):
"""Initialize the sensor."""
super().__init__(fibaro_device)
self.entity_id = ENTITY_ID_FORMAT.format(f"{self.ha_id}_power")
self._attr_name = f"{fibaro_device.friendly_name} Power"
self._attr_unique_id = f"{fibaro_device.unique_id_str}_power"
def update(self):
"""Update the state."""
with suppress(KeyError, ValueError):
self._attr_native_value = convert(
self.fibaro_device.properties.power, float
)