From c5cf69dd9b15cda1594eecd283b18ecfbdaa5b19 Mon Sep 17 00:00:00 2001 From: FlavorFx Date: Tue, 26 Oct 2021 20:01:42 +0200 Subject: [PATCH] Support Energy Sensor and Statistics in Homematic IP Cloud Integration (#57734) * Update sensor.py * Update test_device.py --- .../components/homematicip_cloud/sensor.py | 44 ++++++++++++++++++- .../homematicip_cloud/test_device.py | 2 +- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/homematicip_cloud/sensor.py b/homeassistant/components/homematicip_cloud/sensor.py index ae2bb9f0c6d..ae866bb42e2 100644 --- a/homeassistant/components/homematicip_cloud/sensor.py +++ b/homeassistant/components/homematicip_cloud/sensor.py @@ -26,13 +26,19 @@ from homematicip.aio.device import ( ) from homematicip.base.enums import ValveState -from homeassistant.components.sensor import SensorEntity +from homeassistant.components.sensor import ( + STATE_CLASS_MEASUREMENT, + STATE_CLASS_TOTAL_INCREASING, + SensorEntity, +) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( + DEVICE_CLASS_ENERGY, DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_ILLUMINANCE, DEVICE_CLASS_POWER, DEVICE_CLASS_TEMPERATURE, + ENERGY_KILO_WATT_HOUR, LENGTH_MILLIMETERS, LIGHT_LUX, PERCENTAGE, @@ -111,6 +117,7 @@ async def async_setup_entry( ), ): entities.append(HomematicipPowerSensor(hap, device)) + entities.append(HomematicipEnergySensor(hap, device)) if isinstance( device, (AsyncWeatherSensor, AsyncWeatherSensorPlus, AsyncWeatherSensorPro) ): @@ -127,6 +134,8 @@ async def async_setup_entry( class HomematicipAccesspointDutyCycle(HomematicipGenericEntity, SensorEntity): """Representation of then HomeMaticIP access point.""" + _attr_state_class = STATE_CLASS_MEASUREMENT + def __init__(self, hap: HomematicipHAP, device) -> None: """Initialize access point status entity.""" super().__init__(hap, device, post="Duty Cycle") @@ -179,6 +188,8 @@ class HomematicipHeatingThermostat(HomematicipGenericEntity, SensorEntity): class HomematicipHumiditySensor(HomematicipGenericEntity, SensorEntity): """Representation of the HomematicIP humidity sensor.""" + _attr_state_class = STATE_CLASS_MEASUREMENT + def __init__(self, hap: HomematicipHAP, device) -> None: """Initialize the thermometer device.""" super().__init__(hap, device, post="Humidity") @@ -202,6 +213,8 @@ class HomematicipHumiditySensor(HomematicipGenericEntity, SensorEntity): class HomematicipTemperatureSensor(HomematicipGenericEntity, SensorEntity): """Representation of the HomematicIP thermometer.""" + _attr_state_class = STATE_CLASS_MEASUREMENT + def __init__(self, hap: HomematicipHAP, device) -> None: """Initialize the thermometer device.""" super().__init__(hap, device, post="Temperature") @@ -239,6 +252,8 @@ class HomematicipTemperatureSensor(HomematicipGenericEntity, SensorEntity): class HomematicipIlluminanceSensor(HomematicipGenericEntity, SensorEntity): """Representation of the HomematicIP Illuminance sensor.""" + _attr_state_class = STATE_CLASS_MEASUREMENT + def __init__(self, hap: HomematicipHAP, device) -> None: """Initialize the device.""" super().__init__(hap, device, post="Illuminance") @@ -277,6 +292,8 @@ class HomematicipIlluminanceSensor(HomematicipGenericEntity, SensorEntity): class HomematicipPowerSensor(HomematicipGenericEntity, SensorEntity): """Representation of the HomematicIP power measuring sensor.""" + _attr_state_class = STATE_CLASS_MEASUREMENT + def __init__(self, hap: HomematicipHAP, device) -> None: """Initialize the device.""" super().__init__(hap, device, post="Power") @@ -297,6 +314,31 @@ class HomematicipPowerSensor(HomematicipGenericEntity, SensorEntity): return POWER_WATT +class HomematicipEnergySensor(HomematicipGenericEntity, SensorEntity): + """Representation of the HomematicIP energy measuring sensor.""" + + _attr_state_class = STATE_CLASS_TOTAL_INCREASING + + def __init__(self, hap: HomematicipHAP, device) -> None: + """Initialize the device.""" + super().__init__(hap, device, post="Energy") + + @property + def device_class(self) -> str: + """Return the device class of the sensor.""" + return DEVICE_CLASS_ENERGY + + @property + def native_value(self) -> float: + """Return the energy counter value.""" + return self._device.energyCounter + + @property + def native_unit_of_measurement(self) -> str: + """Return the unit this state is expressed in.""" + return ENERGY_KILO_WATT_HOUR + + class HomematicipWindspeedSensor(HomematicipGenericEntity, SensorEntity): """Representation of the HomematicIP wind speed sensor.""" diff --git a/tests/components/homematicip_cloud/test_device.py b/tests/components/homematicip_cloud/test_device.py index 6f1d3071714..8e3d80ca839 100644 --- a/tests/components/homematicip_cloud/test_device.py +++ b/tests/components/homematicip_cloud/test_device.py @@ -22,7 +22,7 @@ async def test_hmip_load_all_supported_devices(hass, default_mock_hap_factory): test_devices=None, test_groups=None ) - assert len(mock_hap.hmip_device_by_entity_id) == 253 + assert len(mock_hap.hmip_device_by_entity_id) == 258 async def test_hmip_remove_device(hass, default_mock_hap_factory):