From a6b26dbec4596d1a1813ac4da4db9f649859596a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 25 Jan 2022 18:37:39 -1000 Subject: [PATCH] Add state class to nexia sensors (#64932) --- homeassistant/components/nexia/sensor.py | 55 ++++++++++-------------- 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/homeassistant/components/nexia/sensor.py b/homeassistant/components/nexia/sensor.py index d16f90e369d..8137da8e5cc 100644 --- a/homeassistant/components/nexia/sensor.py +++ b/homeassistant/components/nexia/sensor.py @@ -3,7 +3,11 @@ from __future__ import annotations from nexia.const import UNIT_CELSIUS -from homeassistant.components.sensor import SensorDeviceClass, SensorEntity +from homeassistant.components.sensor import ( + SensorDeviceClass, + SensorEntity, + SensorStateClass, +) from homeassistant.config_entries import ConfigEntry from homeassistant.const import PERCENTAGE, TEMP_CELSIUS, TEMP_FAHRENHEIT from homeassistant.core import HomeAssistant @@ -38,6 +42,7 @@ async def async_setup_entry( "System Status", None, None, + None, ) ) # Air cleaner @@ -49,6 +54,7 @@ async def async_setup_entry( "Air Cleaner Mode", None, None, + None, ) ) # Compressor Speed @@ -61,6 +67,7 @@ async def async_setup_entry( "Current Compressor Speed", None, PERCENTAGE, + SensorStateClass.MEASUREMENT, percent_conv, ) ) @@ -72,6 +79,7 @@ async def async_setup_entry( "Requested Compressor Speed", None, PERCENTAGE, + SensorStateClass.MEASUREMENT, percent_conv, ) ) @@ -90,6 +98,7 @@ async def async_setup_entry( "Outdoor Temperature", SensorDeviceClass.TEMPERATURE, unit, + SensorStateClass.MEASUREMENT, ) ) # Relative Humidity @@ -102,6 +111,7 @@ async def async_setup_entry( "Relative Humidity", SensorDeviceClass.HUMIDITY, PERCENTAGE, + SensorStateClass.MEASUREMENT, percent_conv, ) ) @@ -123,18 +133,14 @@ async def async_setup_entry( "Temperature", SensorDeviceClass.TEMPERATURE, unit, + SensorStateClass.MEASUREMENT, None, ) ) # Zone Status entities.append( NexiaThermostatZoneSensor( - coordinator, - zone, - "get_status", - "Zone Status", - None, - None, + coordinator, zone, "get_status", "Zone Status", None, None, None ) ) # Setpoint Status @@ -146,6 +152,7 @@ async def async_setup_entry( "Zone Setpoint Status", None, None, + None, ) ) @@ -163,6 +170,7 @@ class NexiaThermostatSensor(NexiaThermostatEntity, SensorEntity): sensor_name, sensor_class, sensor_unit, + state_class, modifier=None, ): """Initialize the sensor.""" @@ -173,15 +181,10 @@ class NexiaThermostatSensor(NexiaThermostatEntity, SensorEntity): unique_id=f"{thermostat.thermostat_id}_{sensor_call}", ) self._call = sensor_call - self._class = sensor_class - self._state = None - self._unit_of_measurement = sensor_unit self._modifier = modifier - - @property - def device_class(self): - """Return the device class of the sensor.""" - return self._class + self._attr_device_class = sensor_class + self._attr_native_unit_of_measurement = sensor_unit + self._attr_state_class = state_class @property def native_value(self): @@ -193,11 +196,6 @@ class NexiaThermostatSensor(NexiaThermostatEntity, SensorEntity): val = round(val, 1) return val - @property - def native_unit_of_measurement(self): - """Return the unit of measurement this sensor expresses itself in.""" - return self._unit_of_measurement - class NexiaThermostatZoneSensor(NexiaThermostatZoneEntity, SensorEntity): """Nexia Zone Sensor Support.""" @@ -210,6 +208,7 @@ class NexiaThermostatZoneSensor(NexiaThermostatZoneEntity, SensorEntity): sensor_name, sensor_class, sensor_unit, + state_class, modifier=None, ): """Create a zone sensor.""" @@ -221,15 +220,10 @@ class NexiaThermostatZoneSensor(NexiaThermostatZoneEntity, SensorEntity): unique_id=f"{zone.zone_id}_{sensor_call}", ) self._call = sensor_call - self._class = sensor_class - self._state = None - self._unit_of_measurement = sensor_unit self._modifier = modifier - - @property - def device_class(self): - """Return the device class of the sensor.""" - return self._class + self._attr_device_class = sensor_class + self._attr_native_unit_of_measurement = sensor_unit + self._attr_state_class = state_class @property def native_value(self): @@ -240,8 +234,3 @@ class NexiaThermostatZoneSensor(NexiaThermostatZoneEntity, SensorEntity): if isinstance(val, float): val = round(val, 1) return val - - @property - def native_unit_of_measurement(self): - """Return the unit of measurement this sensor expresses itself in.""" - return self._unit_of_measurement