Add state_class to entities coming from battery powered devices in Shelly integration (#51009)

* Fix sensor state_class

* Remove state class from total work time sensor

* Add state_class restore mechanism

* Remove commented code

* Remove unnecessary code
This commit is contained in:
Maciej Bieniek 2021-05-23 22:10:22 +02:00 committed by GitHub
parent f0cd87e031
commit c91f89260e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -9,6 +9,7 @@ from typing import Any, Callable
import aioshelly import aioshelly
import async_timeout import async_timeout
from homeassistant.components.sensor import ATTR_STATE_CLASS
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers import ( from homeassistant.helpers import (
device_registry, device_registry,
@ -168,6 +169,7 @@ class RestAttributeDescription:
unit: str | None = None unit: str | None = None
value: Callable[[dict, Any], Any] | None = None value: Callable[[dict, Any], Any] | None = None
device_class: str | None = None device_class: str | None = None
state_class: str | None = None
default_enabled: bool = True default_enabled: bool = True
extra_state_attributes: Callable[[dict], dict | None] | None = None extra_state_attributes: Callable[[dict], dict | None] | None = None
@ -429,6 +431,7 @@ class ShellySleepingBlockAttributeEntity(ShellyBlockAttributeEntity, RestoreEnti
if last_state is not None: if last_state is not None:
self.last_state = last_state.state self.last_state = last_state.state
self.description.state_class = last_state.attributes.get(ATTR_STATE_CLASS)
@callback @callback
def _update_callback(self): def _update_callback(self):

View File

@ -30,6 +30,7 @@ SENSORS = {
name="Battery", name="Battery",
unit=PERCENTAGE, unit=PERCENTAGE,
device_class=sensor.DEVICE_CLASS_BATTERY, device_class=sensor.DEVICE_CLASS_BATTERY,
state_class=sensor.STATE_CLASS_MEASUREMENT,
removal_condition=lambda settings, _: settings.get("external_power") == 1, removal_condition=lambda settings, _: settings.get("external_power") == 1,
), ),
("device", "deviceTemp"): BlockAttributeDescription( ("device", "deviceTemp"): BlockAttributeDescription(
@ -37,6 +38,7 @@ SENSORS = {
unit=temperature_unit, unit=temperature_unit,
value=lambda value: round(value, 1), value=lambda value: round(value, 1),
device_class=sensor.DEVICE_CLASS_TEMPERATURE, device_class=sensor.DEVICE_CLASS_TEMPERATURE,
state_class=sensor.STATE_CLASS_MEASUREMENT,
default_enabled=False, default_enabled=False,
), ),
("emeter", "current"): BlockAttributeDescription( ("emeter", "current"): BlockAttributeDescription(
@ -44,12 +46,14 @@ SENSORS = {
unit=ELECTRICAL_CURRENT_AMPERE, unit=ELECTRICAL_CURRENT_AMPERE,
value=lambda value: value, value=lambda value: value,
device_class=sensor.DEVICE_CLASS_CURRENT, device_class=sensor.DEVICE_CLASS_CURRENT,
state_class=sensor.STATE_CLASS_MEASUREMENT,
), ),
("light", "power"): BlockAttributeDescription( ("light", "power"): BlockAttributeDescription(
name="Power", name="Power",
unit=POWER_WATT, unit=POWER_WATT,
value=lambda value: round(value, 1), value=lambda value: round(value, 1),
device_class=sensor.DEVICE_CLASS_POWER, device_class=sensor.DEVICE_CLASS_POWER,
state_class=sensor.STATE_CLASS_MEASUREMENT,
default_enabled=False, default_enabled=False,
), ),
("device", "power"): BlockAttributeDescription( ("device", "power"): BlockAttributeDescription(
@ -57,60 +61,70 @@ SENSORS = {
unit=POWER_WATT, unit=POWER_WATT,
value=lambda value: round(value, 1), value=lambda value: round(value, 1),
device_class=sensor.DEVICE_CLASS_POWER, device_class=sensor.DEVICE_CLASS_POWER,
state_class=sensor.STATE_CLASS_MEASUREMENT,
), ),
("emeter", "power"): BlockAttributeDescription( ("emeter", "power"): BlockAttributeDescription(
name="Power", name="Power",
unit=POWER_WATT, unit=POWER_WATT,
value=lambda value: round(value, 1), value=lambda value: round(value, 1),
device_class=sensor.DEVICE_CLASS_POWER, device_class=sensor.DEVICE_CLASS_POWER,
state_class=sensor.STATE_CLASS_MEASUREMENT,
), ),
("emeter", "voltage"): BlockAttributeDescription( ("emeter", "voltage"): BlockAttributeDescription(
name="Voltage", name="Voltage",
unit=VOLT, unit=VOLT,
value=lambda value: round(value, 1), value=lambda value: round(value, 1),
device_class=sensor.DEVICE_CLASS_VOLTAGE, device_class=sensor.DEVICE_CLASS_VOLTAGE,
state_class=sensor.STATE_CLASS_MEASUREMENT,
), ),
("emeter", "powerFactor"): BlockAttributeDescription( ("emeter", "powerFactor"): BlockAttributeDescription(
name="Power Factor", name="Power Factor",
unit=PERCENTAGE, unit=PERCENTAGE,
value=lambda value: round(value * 100, 1), value=lambda value: round(value * 100, 1),
device_class=sensor.DEVICE_CLASS_POWER_FACTOR, device_class=sensor.DEVICE_CLASS_POWER_FACTOR,
state_class=sensor.STATE_CLASS_MEASUREMENT,
), ),
("relay", "power"): BlockAttributeDescription( ("relay", "power"): BlockAttributeDescription(
name="Power", name="Power",
unit=POWER_WATT, unit=POWER_WATT,
value=lambda value: round(value, 1), value=lambda value: round(value, 1),
device_class=sensor.DEVICE_CLASS_POWER, device_class=sensor.DEVICE_CLASS_POWER,
state_class=sensor.STATE_CLASS_MEASUREMENT,
), ),
("roller", "rollerPower"): BlockAttributeDescription( ("roller", "rollerPower"): BlockAttributeDescription(
name="Power", name="Power",
unit=POWER_WATT, unit=POWER_WATT,
value=lambda value: round(value, 1), value=lambda value: round(value, 1),
device_class=sensor.DEVICE_CLASS_POWER, device_class=sensor.DEVICE_CLASS_POWER,
state_class=sensor.STATE_CLASS_MEASUREMENT,
), ),
("device", "energy"): BlockAttributeDescription( ("device", "energy"): BlockAttributeDescription(
name="Energy", name="Energy",
unit=ENERGY_KILO_WATT_HOUR, unit=ENERGY_KILO_WATT_HOUR,
value=lambda value: round(value / 60 / 1000, 2), value=lambda value: round(value / 60 / 1000, 2),
device_class=sensor.DEVICE_CLASS_ENERGY, device_class=sensor.DEVICE_CLASS_ENERGY,
state_class=sensor.STATE_CLASS_MEASUREMENT,
), ),
("emeter", "energy"): BlockAttributeDescription( ("emeter", "energy"): BlockAttributeDescription(
name="Energy", name="Energy",
unit=ENERGY_KILO_WATT_HOUR, unit=ENERGY_KILO_WATT_HOUR,
value=lambda value: round(value / 1000, 2), value=lambda value: round(value / 1000, 2),
device_class=sensor.DEVICE_CLASS_ENERGY, device_class=sensor.DEVICE_CLASS_ENERGY,
state_class=sensor.STATE_CLASS_MEASUREMENT,
), ),
("emeter", "energyReturned"): BlockAttributeDescription( ("emeter", "energyReturned"): BlockAttributeDescription(
name="Energy Returned", name="Energy Returned",
unit=ENERGY_KILO_WATT_HOUR, unit=ENERGY_KILO_WATT_HOUR,
value=lambda value: round(value / 1000, 2), value=lambda value: round(value / 1000, 2),
device_class=sensor.DEVICE_CLASS_ENERGY, device_class=sensor.DEVICE_CLASS_ENERGY,
state_class=sensor.STATE_CLASS_MEASUREMENT,
), ),
("light", "energy"): BlockAttributeDescription( ("light", "energy"): BlockAttributeDescription(
name="Energy", name="Energy",
unit=ENERGY_KILO_WATT_HOUR, unit=ENERGY_KILO_WATT_HOUR,
value=lambda value: round(value / 60 / 1000, 2), value=lambda value: round(value / 60 / 1000, 2),
device_class=sensor.DEVICE_CLASS_ENERGY, device_class=sensor.DEVICE_CLASS_ENERGY,
state_class=sensor.STATE_CLASS_MEASUREMENT,
default_enabled=False, default_enabled=False,
), ),
("relay", "energy"): BlockAttributeDescription( ("relay", "energy"): BlockAttributeDescription(
@ -118,17 +132,20 @@ SENSORS = {
unit=ENERGY_KILO_WATT_HOUR, unit=ENERGY_KILO_WATT_HOUR,
value=lambda value: round(value / 60 / 1000, 2), value=lambda value: round(value / 60 / 1000, 2),
device_class=sensor.DEVICE_CLASS_ENERGY, device_class=sensor.DEVICE_CLASS_ENERGY,
state_class=sensor.STATE_CLASS_MEASUREMENT,
), ),
("roller", "rollerEnergy"): BlockAttributeDescription( ("roller", "rollerEnergy"): BlockAttributeDescription(
name="Energy", name="Energy",
unit=ENERGY_KILO_WATT_HOUR, unit=ENERGY_KILO_WATT_HOUR,
value=lambda value: round(value / 60 / 1000, 2), value=lambda value: round(value / 60 / 1000, 2),
device_class=sensor.DEVICE_CLASS_ENERGY, device_class=sensor.DEVICE_CLASS_ENERGY,
state_class=sensor.STATE_CLASS_MEASUREMENT,
), ),
("sensor", "concentration"): BlockAttributeDescription( ("sensor", "concentration"): BlockAttributeDescription(
name="Gas Concentration", name="Gas Concentration",
unit=CONCENTRATION_PARTS_PER_MILLION, unit=CONCENTRATION_PARTS_PER_MILLION,
icon="mdi:gauge", icon="mdi:gauge",
state_class=sensor.STATE_CLASS_MEASUREMENT,
), ),
("sensor", "extTemp"): BlockAttributeDescription( ("sensor", "extTemp"): BlockAttributeDescription(
name="Temperature", name="Temperature",
@ -143,17 +160,20 @@ SENSORS = {
unit=PERCENTAGE, unit=PERCENTAGE,
value=lambda value: round(value, 1), value=lambda value: round(value, 1),
device_class=sensor.DEVICE_CLASS_HUMIDITY, device_class=sensor.DEVICE_CLASS_HUMIDITY,
state_class=sensor.STATE_CLASS_MEASUREMENT,
available=lambda block: block.extTemp != 999, available=lambda block: block.extTemp != 999,
), ),
("sensor", "luminosity"): BlockAttributeDescription( ("sensor", "luminosity"): BlockAttributeDescription(
name="Luminosity", name="Luminosity",
unit=LIGHT_LUX, unit=LIGHT_LUX,
device_class=sensor.DEVICE_CLASS_ILLUMINANCE, device_class=sensor.DEVICE_CLASS_ILLUMINANCE,
state_class=sensor.STATE_CLASS_MEASUREMENT,
), ),
("sensor", "tilt"): BlockAttributeDescription( ("sensor", "tilt"): BlockAttributeDescription(
name="Tilt", name="Tilt",
unit=DEGREE, unit=DEGREE,
icon="mdi:angle-acute", icon="mdi:angle-acute",
state_class=sensor.STATE_CLASS_MEASUREMENT,
), ),
("relay", "totalWorkTime"): BlockAttributeDescription( ("relay", "totalWorkTime"): BlockAttributeDescription(
name="Lamp Life", name="Lamp Life",
@ -169,6 +189,7 @@ SENSORS = {
unit=VOLT, unit=VOLT,
value=lambda value: round(value, 1), value=lambda value: round(value, 1),
device_class=sensor.DEVICE_CLASS_VOLTAGE, device_class=sensor.DEVICE_CLASS_VOLTAGE,
state_class=sensor.STATE_CLASS_MEASUREMENT,
), ),
("sensor", "sensorOp"): BlockAttributeDescription( ("sensor", "sensorOp"): BlockAttributeDescription(
name="Operation", name="Operation",
@ -184,6 +205,7 @@ REST_SENSORS = {
unit=SIGNAL_STRENGTH_DECIBELS_MILLIWATT, unit=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
value=lambda status, _: status["wifi_sta"]["rssi"], value=lambda status, _: status["wifi_sta"]["rssi"],
device_class=sensor.DEVICE_CLASS_SIGNAL_STRENGTH, device_class=sensor.DEVICE_CLASS_SIGNAL_STRENGTH,
state_class=sensor.STATE_CLASS_MEASUREMENT,
default_enabled=False, default_enabled=False,
), ),
"uptime": RestAttributeDescription( "uptime": RestAttributeDescription(
@ -218,6 +240,11 @@ class ShellySensor(ShellyBlockAttributeEntity, SensorEntity):
"""Return value of sensor.""" """Return value of sensor."""
return self.attribute_value return self.attribute_value
@property
def state_class(self):
"""State class of sensor."""
return self.description.state_class
@property @property
def unit_of_measurement(self): def unit_of_measurement(self):
"""Return unit of sensor.""" """Return unit of sensor."""
@ -254,6 +281,11 @@ class ShellySleepingSensor(ShellySleepingBlockAttributeEntity, SensorEntity):
return self.last_state return self.last_state
@property
def state_class(self):
"""State class of sensor."""
return self.description.state_class
@property @property
def unit_of_measurement(self): def unit_of_measurement(self):
"""Return unit of sensor.""" """Return unit of sensor."""