Migrate sensor state classes to StrEnum (#60709)

This commit is contained in:
Franck Nijhof 2021-12-01 12:09:31 +01:00 committed by GitHub
parent c6cbfe8c37
commit 2ec49d4ffd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 24 deletions

View file

@ -4,9 +4,9 @@ from __future__ import annotations
from typing import Any from typing import Any
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
STATE_CLASS_MEASUREMENT,
SensorDeviceClass, SensorDeviceClass,
SensorEntity, SensorEntity,
SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
@ -39,7 +39,7 @@ async def async_setup_platform(
"Outside Temperature", "Outside Temperature",
15.6, 15.6,
SensorDeviceClass.TEMPERATURE, SensorDeviceClass.TEMPERATURE,
STATE_CLASS_MEASUREMENT, SensorStateClass.MEASUREMENT,
TEMP_CELSIUS, TEMP_CELSIUS,
12, 12,
), ),
@ -48,7 +48,7 @@ async def async_setup_platform(
"Outside Humidity", "Outside Humidity",
54, 54,
SensorDeviceClass.HUMIDITY, SensorDeviceClass.HUMIDITY,
STATE_CLASS_MEASUREMENT, SensorStateClass.MEASUREMENT,
PERCENTAGE, PERCENTAGE,
None, None,
), ),
@ -57,7 +57,7 @@ async def async_setup_platform(
"Carbon monoxide", "Carbon monoxide",
54, 54,
SensorDeviceClass.CO, SensorDeviceClass.CO,
STATE_CLASS_MEASUREMENT, SensorStateClass.MEASUREMENT,
CONCENTRATION_PARTS_PER_MILLION, CONCENTRATION_PARTS_PER_MILLION,
None, None,
), ),
@ -66,7 +66,7 @@ async def async_setup_platform(
"Carbon dioxide", "Carbon dioxide",
54, 54,
SensorDeviceClass.CO2, SensorDeviceClass.CO2,
STATE_CLASS_MEASUREMENT, SensorStateClass.MEASUREMENT,
CONCENTRATION_PARTS_PER_MILLION, CONCENTRATION_PARTS_PER_MILLION,
14, 14,
), ),
@ -75,7 +75,7 @@ async def async_setup_platform(
"Power consumption", "Power consumption",
100, 100,
SensorDeviceClass.POWER, SensorDeviceClass.POWER,
STATE_CLASS_MEASUREMENT, SensorStateClass.MEASUREMENT,
POWER_WATT, POWER_WATT,
None, None,
), ),
@ -84,7 +84,7 @@ async def async_setup_platform(
"Today energy", "Today energy",
15, 15,
SensorDeviceClass.ENERGY, SensorDeviceClass.ENERGY,
STATE_CLASS_MEASUREMENT, SensorStateClass.MEASUREMENT,
ENERGY_KILO_WATT_HOUR, ENERGY_KILO_WATT_HOUR,
None, None,
), ),
@ -112,7 +112,7 @@ class DemoSensor(SensorEntity):
name: str, name: str,
state: StateType, state: StateType,
device_class: SensorDeviceClass, device_class: SensorDeviceClass,
state_class: str | None, state_class: SensorStateClass | None,
unit_of_measurement: str | None, unit_of_measurement: str | None,
battery: StateType, battery: StateType,
) -> None: ) -> None:

View file

@ -163,20 +163,29 @@ DEVICE_CLASSES_SCHEMA: Final = vol.All(vol.Lower, vol.Coerce(SensorDeviceClass))
# use the SensorDeviceClass enum instead. # use the SensorDeviceClass enum instead.
DEVICE_CLASSES: Final[list[str]] = [cls.value for cls in SensorDeviceClass] DEVICE_CLASSES: Final[list[str]] = [cls.value for cls in SensorDeviceClass]
# The state represents a measurement in present time
class SensorStateClass(StrEnum):
"""State class for sensors."""
# The state represents a measurement in present time
MEASUREMENT = "measurement"
# The state represents a total amount, e.g. net energy consumption
TOTAL = "total"
# The state represents a monotonically increasing total, e.g. an amount of consumed gas
TOTAL_INCREASING = "total_increasing"
STATE_CLASSES_SCHEMA: Final = vol.All(vol.Lower, vol.Coerce(SensorStateClass))
# STATE_CLASS* is deprecated as of 2021.12
# use the SensorStateClass enum instead.
STATE_CLASS_MEASUREMENT: Final = "measurement" STATE_CLASS_MEASUREMENT: Final = "measurement"
# The state represents a total amount, e.g. net energy consumption
STATE_CLASS_TOTAL: Final = "total" STATE_CLASS_TOTAL: Final = "total"
# The state represents a monotonically increasing total, e.g. an amount of consumed gas
STATE_CLASS_TOTAL_INCREASING: Final = "total_increasing" STATE_CLASS_TOTAL_INCREASING: Final = "total_increasing"
STATE_CLASSES: Final[list[str]] = [cls.value for cls in SensorStateClass]
STATE_CLASSES: Final[list[str]] = [
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL,
STATE_CLASS_TOTAL_INCREASING,
]
STATE_CLASSES_SCHEMA: Final = vol.All(vol.Lower, vol.In(STATE_CLASSES))
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
@ -208,7 +217,7 @@ class SensorEntityDescription(EntityDescription):
device_class: SensorDeviceClass | str | None = None device_class: SensorDeviceClass | str | None = None
last_reset: datetime | None = None # Deprecated, to be removed in 2021.11 last_reset: datetime | None = None # Deprecated, to be removed in 2021.11
native_unit_of_measurement: str | None = None native_unit_of_measurement: str | None = None
state_class: str | None = None state_class: SensorStateClass | str | None = None
unit_of_measurement: None = None # Type override, use native_unit_of_measurement unit_of_measurement: None = None # Type override, use native_unit_of_measurement
def __post_init__(self) -> None: def __post_init__(self) -> None:
@ -241,7 +250,7 @@ class SensorEntity(Entity):
_attr_last_reset: datetime | None # Deprecated, to be removed in 2021.11 _attr_last_reset: datetime | None # Deprecated, to be removed in 2021.11
_attr_native_unit_of_measurement: str | None _attr_native_unit_of_measurement: str | None
_attr_native_value: StateType | date | datetime = None _attr_native_value: StateType | date | datetime = None
_attr_state_class: str | None _attr_state_class: SensorStateClass | str | None
_attr_state: None = None # Subclasses of SensorEntity should not set this _attr_state: None = None # Subclasses of SensorEntity should not set this
_attr_unit_of_measurement: None = ( _attr_unit_of_measurement: None = (
None # Subclasses of SensorEntity should not set this None # Subclasses of SensorEntity should not set this
@ -262,8 +271,8 @@ class SensorEntity(Entity):
return None return None
@property @property
def state_class(self) -> str | None: def state_class(self) -> SensorStateClass | str | None:
"""Return the state class of this entity, from STATE_CLASSES, if any.""" """Return the state class of this entity, if any."""
if hasattr(self, "_attr_state_class"): if hasattr(self, "_attr_state_class"):
return self._attr_state_class return self._attr_state_class
if hasattr(self, "entity_description"): if hasattr(self, "entity_description"):
@ -293,7 +302,7 @@ class SensorEntity(Entity):
"""Return state attributes.""" """Return state attributes."""
if last_reset := self.last_reset: if last_reset := self.last_reset:
if ( if (
self.state_class == STATE_CLASS_MEASUREMENT self.state_class == SensorStateClass.MEASUREMENT
and not self._last_reset_reported and not self._last_reset_reported
): ):
self._last_reset_reported = True self._last_reset_reported = True