From fbc91d3d3d0fcb8f213928f14914218e1bb367ad Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 6 Feb 2026 12:01:23 +0100 Subject: [PATCH] Remove str from sensor state class (#161686) --- homeassistant/components/sensor/__init__.py | 8 ++++---- homeassistant/components/sensor/const.py | 2 +- pylint/plugins/hass_enforce_type_hints.py | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/sensor/__init__.py b/homeassistant/components/sensor/__init__.py index d6829d35329..3148b0d13c2 100644 --- a/homeassistant/components/sensor/__init__.py +++ b/homeassistant/components/sensor/__init__.py @@ -112,7 +112,7 @@ class SensorEntityDescription(EntityDescription, frozen_or_thawed=True): last_reset: datetime | None = None native_unit_of_measurement: str | None = None options: list[str] | None = None - state_class: SensorStateClass | str | None = None + state_class: SensorStateClass | None = None suggested_display_precision: int | None = None suggested_unit_of_measurement: str | None = None unit_of_measurement: None = None # Type override, use native_unit_of_measurement @@ -120,7 +120,7 @@ class SensorEntityDescription(EntityDescription, frozen_or_thawed=True): def _numeric_state_expected( device_class: SensorDeviceClass | None, - state_class: SensorStateClass | str | None, + state_class: SensorStateClass | None, native_unit_of_measurement: str | None, suggested_display_precision: int | None, ) -> bool: @@ -188,7 +188,7 @@ class SensorEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_): _attr_native_unit_of_measurement: str | None _attr_native_value: StateType | date | datetime | Decimal = None _attr_options: list[str] | None - _attr_state_class: SensorStateClass | str | None + _attr_state_class: SensorStateClass | None _attr_state: None = None # Subclasses of SensorEntity should not set this _attr_suggested_display_precision: int | None _attr_suggested_unit_of_measurement: str | None @@ -329,7 +329,7 @@ class SensorEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_): return None @cached_property - def state_class(self) -> SensorStateClass | str | None: + def state_class(self) -> SensorStateClass | None: """Return the state class of this entity, if any.""" if hasattr(self, "_attr_state_class"): return self._attr_state_class diff --git a/homeassistant/components/sensor/const.py b/homeassistant/components/sensor/const.py index 108ed318ccb..88fde1569c0 100644 --- a/homeassistant/components/sensor/const.py +++ b/homeassistant/components/sensor/const.py @@ -835,7 +835,7 @@ DEVICE_CLASS_STATE_CLASSES: dict[SensorDeviceClass, set[SensorStateClass]] = { } -STATE_CLASS_UNITS: dict[SensorStateClass | str, set[type[StrEnum] | str | None]] = { +STATE_CLASS_UNITS: dict[SensorStateClass, set[type[StrEnum] | str | None]] = { SensorStateClass.MEASUREMENT_ANGLE: {DEGREE}, } diff --git a/pylint/plugins/hass_enforce_type_hints.py b/pylint/plugins/hass_enforce_type_hints.py index b09bd55e43a..47416da20cc 100644 --- a/pylint/plugins/hass_enforce_type_hints.py +++ b/pylint/plugins/hass_enforce_type_hints.py @@ -2526,7 +2526,7 @@ _INHERITANCE_MATCH: dict[str, list[ClassTypeHintMatch]] = { ), TypeHintMatch( function_name="state_class", - return_type=["SensorStateClass", "str", None], + return_type=["SensorStateClass", None], mandatory=True, ), TypeHintMatch(