From 93a66cc357b226389967668441000498a10453bb Mon Sep 17 00:00:00 2001 From: rappenze Date: Tue, 29 Mar 2022 21:53:43 +0200 Subject: [PATCH] Add power sensor to fibaro integration (#68848) --- homeassistant/components/fibaro/sensor.py | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/homeassistant/components/fibaro/sensor.py b/homeassistant/components/fibaro/sensor.py index 9a7098b018c..45d33728ab9 100644 --- a/homeassistant/components/fibaro/sensor.py +++ b/homeassistant/components/fibaro/sensor.py @@ -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 + )