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:
parent
f0cd87e031
commit
c91f89260e
2 changed files with 35 additions and 0 deletions
|
@ -9,6 +9,7 @@ from typing import Any, Callable
|
|||
import aioshelly
|
||||
import async_timeout
|
||||
|
||||
from homeassistant.components.sensor import ATTR_STATE_CLASS
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers import (
|
||||
device_registry,
|
||||
|
@ -168,6 +169,7 @@ class RestAttributeDescription:
|
|||
unit: str | None = None
|
||||
value: Callable[[dict, Any], Any] | None = None
|
||||
device_class: str | None = None
|
||||
state_class: str | None = None
|
||||
default_enabled: bool = True
|
||||
extra_state_attributes: Callable[[dict], dict | None] | None = None
|
||||
|
||||
|
@ -429,6 +431,7 @@ class ShellySleepingBlockAttributeEntity(ShellyBlockAttributeEntity, RestoreEnti
|
|||
|
||||
if last_state is not None:
|
||||
self.last_state = last_state.state
|
||||
self.description.state_class = last_state.attributes.get(ATTR_STATE_CLASS)
|
||||
|
||||
@callback
|
||||
def _update_callback(self):
|
||||
|
|
|
@ -30,6 +30,7 @@ SENSORS = {
|
|||
name="Battery",
|
||||
unit=PERCENTAGE,
|
||||
device_class=sensor.DEVICE_CLASS_BATTERY,
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
removal_condition=lambda settings, _: settings.get("external_power") == 1,
|
||||
),
|
||||
("device", "deviceTemp"): BlockAttributeDescription(
|
||||
|
@ -37,6 +38,7 @@ SENSORS = {
|
|||
unit=temperature_unit,
|
||||
value=lambda value: round(value, 1),
|
||||
device_class=sensor.DEVICE_CLASS_TEMPERATURE,
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
default_enabled=False,
|
||||
),
|
||||
("emeter", "current"): BlockAttributeDescription(
|
||||
|
@ -44,12 +46,14 @@ SENSORS = {
|
|||
unit=ELECTRICAL_CURRENT_AMPERE,
|
||||
value=lambda value: value,
|
||||
device_class=sensor.DEVICE_CLASS_CURRENT,
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
("light", "power"): BlockAttributeDescription(
|
||||
name="Power",
|
||||
unit=POWER_WATT,
|
||||
value=lambda value: round(value, 1),
|
||||
device_class=sensor.DEVICE_CLASS_POWER,
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
default_enabled=False,
|
||||
),
|
||||
("device", "power"): BlockAttributeDescription(
|
||||
|
@ -57,60 +61,70 @@ SENSORS = {
|
|||
unit=POWER_WATT,
|
||||
value=lambda value: round(value, 1),
|
||||
device_class=sensor.DEVICE_CLASS_POWER,
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
("emeter", "power"): BlockAttributeDescription(
|
||||
name="Power",
|
||||
unit=POWER_WATT,
|
||||
value=lambda value: round(value, 1),
|
||||
device_class=sensor.DEVICE_CLASS_POWER,
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
("emeter", "voltage"): BlockAttributeDescription(
|
||||
name="Voltage",
|
||||
unit=VOLT,
|
||||
value=lambda value: round(value, 1),
|
||||
device_class=sensor.DEVICE_CLASS_VOLTAGE,
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
("emeter", "powerFactor"): BlockAttributeDescription(
|
||||
name="Power Factor",
|
||||
unit=PERCENTAGE,
|
||||
value=lambda value: round(value * 100, 1),
|
||||
device_class=sensor.DEVICE_CLASS_POWER_FACTOR,
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
("relay", "power"): BlockAttributeDescription(
|
||||
name="Power",
|
||||
unit=POWER_WATT,
|
||||
value=lambda value: round(value, 1),
|
||||
device_class=sensor.DEVICE_CLASS_POWER,
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
("roller", "rollerPower"): BlockAttributeDescription(
|
||||
name="Power",
|
||||
unit=POWER_WATT,
|
||||
value=lambda value: round(value, 1),
|
||||
device_class=sensor.DEVICE_CLASS_POWER,
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
("device", "energy"): BlockAttributeDescription(
|
||||
name="Energy",
|
||||
unit=ENERGY_KILO_WATT_HOUR,
|
||||
value=lambda value: round(value / 60 / 1000, 2),
|
||||
device_class=sensor.DEVICE_CLASS_ENERGY,
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
("emeter", "energy"): BlockAttributeDescription(
|
||||
name="Energy",
|
||||
unit=ENERGY_KILO_WATT_HOUR,
|
||||
value=lambda value: round(value / 1000, 2),
|
||||
device_class=sensor.DEVICE_CLASS_ENERGY,
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
("emeter", "energyReturned"): BlockAttributeDescription(
|
||||
name="Energy Returned",
|
||||
unit=ENERGY_KILO_WATT_HOUR,
|
||||
value=lambda value: round(value / 1000, 2),
|
||||
device_class=sensor.DEVICE_CLASS_ENERGY,
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
("light", "energy"): BlockAttributeDescription(
|
||||
name="Energy",
|
||||
unit=ENERGY_KILO_WATT_HOUR,
|
||||
value=lambda value: round(value / 60 / 1000, 2),
|
||||
device_class=sensor.DEVICE_CLASS_ENERGY,
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
default_enabled=False,
|
||||
),
|
||||
("relay", "energy"): BlockAttributeDescription(
|
||||
|
@ -118,17 +132,20 @@ SENSORS = {
|
|||
unit=ENERGY_KILO_WATT_HOUR,
|
||||
value=lambda value: round(value / 60 / 1000, 2),
|
||||
device_class=sensor.DEVICE_CLASS_ENERGY,
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
("roller", "rollerEnergy"): BlockAttributeDescription(
|
||||
name="Energy",
|
||||
unit=ENERGY_KILO_WATT_HOUR,
|
||||
value=lambda value: round(value / 60 / 1000, 2),
|
||||
device_class=sensor.DEVICE_CLASS_ENERGY,
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
("sensor", "concentration"): BlockAttributeDescription(
|
||||
name="Gas Concentration",
|
||||
unit=CONCENTRATION_PARTS_PER_MILLION,
|
||||
icon="mdi:gauge",
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
("sensor", "extTemp"): BlockAttributeDescription(
|
||||
name="Temperature",
|
||||
|
@ -143,17 +160,20 @@ SENSORS = {
|
|||
unit=PERCENTAGE,
|
||||
value=lambda value: round(value, 1),
|
||||
device_class=sensor.DEVICE_CLASS_HUMIDITY,
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
available=lambda block: block.extTemp != 999,
|
||||
),
|
||||
("sensor", "luminosity"): BlockAttributeDescription(
|
||||
name="Luminosity",
|
||||
unit=LIGHT_LUX,
|
||||
device_class=sensor.DEVICE_CLASS_ILLUMINANCE,
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
("sensor", "tilt"): BlockAttributeDescription(
|
||||
name="Tilt",
|
||||
unit=DEGREE,
|
||||
icon="mdi:angle-acute",
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
("relay", "totalWorkTime"): BlockAttributeDescription(
|
||||
name="Lamp Life",
|
||||
|
@ -169,6 +189,7 @@ SENSORS = {
|
|||
unit=VOLT,
|
||||
value=lambda value: round(value, 1),
|
||||
device_class=sensor.DEVICE_CLASS_VOLTAGE,
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
("sensor", "sensorOp"): BlockAttributeDescription(
|
||||
name="Operation",
|
||||
|
@ -184,6 +205,7 @@ REST_SENSORS = {
|
|||
unit=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
||||
value=lambda status, _: status["wifi_sta"]["rssi"],
|
||||
device_class=sensor.DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||
state_class=sensor.STATE_CLASS_MEASUREMENT,
|
||||
default_enabled=False,
|
||||
),
|
||||
"uptime": RestAttributeDescription(
|
||||
|
@ -218,6 +240,11 @@ class ShellySensor(ShellyBlockAttributeEntity, SensorEntity):
|
|||
"""Return value of sensor."""
|
||||
return self.attribute_value
|
||||
|
||||
@property
|
||||
def state_class(self):
|
||||
"""State class of sensor."""
|
||||
return self.description.state_class
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return unit of sensor."""
|
||||
|
@ -254,6 +281,11 @@ class ShellySleepingSensor(ShellySleepingBlockAttributeEntity, SensorEntity):
|
|||
|
||||
return self.last_state
|
||||
|
||||
@property
|
||||
def state_class(self):
|
||||
"""State class of sensor."""
|
||||
return self.description.state_class
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return unit of sensor."""
|
||||
|
|
Loading…
Add table
Reference in a new issue