diff --git a/homeassistant/components/canary/model.py b/homeassistant/components/canary/model.py index 12fb8209108..acf8f9c59a8 100644 --- a/homeassistant/components/canary/model.py +++ b/homeassistant/components/canary/model.py @@ -2,7 +2,7 @@ from __future__ import annotations from collections.abc import ValuesView -from typing import Optional, TypedDict +from typing import TypedDict from canary.model import Location @@ -12,6 +12,3 @@ class CanaryData(TypedDict): locations: dict[str, Location] readings: dict[str, ValuesView] - - -SensorTypeItem = tuple[str, Optional[str], Optional[str], Optional[str], list[str]] diff --git a/homeassistant/components/canary/sensor.py b/homeassistant/components/canary/sensor.py index c80c178fbb5..95204550af7 100644 --- a/homeassistant/components/canary/sensor.py +++ b/homeassistant/components/canary/sensor.py @@ -1,7 +1,7 @@ """Support for Canary sensors.""" from __future__ import annotations -from typing import Final +from typing import Final, Optional from canary.model import Device, Location, SensorType @@ -19,7 +19,10 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import DATA_COORDINATOR, DOMAIN, MANUFACTURER from .coordinator import CanaryDataUpdateCoordinator -from .model import SensorTypeItem + +SensorTypeItem = tuple[ + str, Optional[str], Optional[str], Optional[SensorDeviceClass], list[str] +] SENSOR_VALUE_PRECISION: Final = 2 ATTR_AIR_QUALITY: Final = "air_quality" diff --git a/homeassistant/components/demo/sensor.py b/homeassistant/components/demo/sensor.py index a7391a055fa..67a7b346a3e 100644 --- a/homeassistant/components/demo/sensor.py +++ b/homeassistant/components/demo/sensor.py @@ -158,7 +158,7 @@ class DemoSensor(SensorEntity): unique_id: str, name: str, state: StateType, - device_class: SensorDeviceClass | str, + device_class: SensorDeviceClass, state_class: SensorStateClass | None, unit_of_measurement: str | None, battery: StateType, diff --git a/homeassistant/components/onvif/sensor.py b/homeassistant/components/onvif/sensor.py index 4ad4c11f175..0a2932a8094 100644 --- a/homeassistant/components/onvif/sensor.py +++ b/homeassistant/components/onvif/sensor.py @@ -1,10 +1,11 @@ """Support for ONVIF binary sensors.""" from __future__ import annotations +from contextlib import suppress from datetime import date, datetime from decimal import Decimal -from homeassistant.components.sensor import RestoreSensor +from homeassistant.components.sensor import RestoreSensor, SensorDeviceClass from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import entity_registry as er @@ -58,14 +59,20 @@ class ONVIFSensor(ONVIFBaseEntity, RestoreSensor): """Initialize the ONVIF binary sensor.""" self._attr_unique_id = uid if entry is not None: - self._attr_device_class = entry.original_device_class + if entry.original_device_class: + with suppress(ValueError): + self._attr_device_class = SensorDeviceClass( + entry.original_device_class + ) self._attr_entity_category = entry.entity_category self._attr_name = entry.name self._attr_native_unit_of_measurement = entry.unit_of_measurement else: event = device.events.get_uid(uid) assert event - self._attr_device_class = event.device_class + if event.device_class: + with suppress(ValueError): + self._attr_device_class = SensorDeviceClass(event.device_class) self._attr_entity_category = event.entity_category self._attr_entity_registry_enabled_default = event.entity_enabled self._attr_name = f"{device.name} {event.name}" diff --git a/homeassistant/components/sensor/__init__.py b/homeassistant/components/sensor/__init__.py index a3cbc8a93ae..e96a4a190f7 100644 --- a/homeassistant/components/sensor/__init__.py +++ b/homeassistant/components/sensor/__init__.py @@ -470,7 +470,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: class SensorEntityDescription(EntityDescription): """A class that describes sensor entities.""" - device_class: SensorDeviceClass | str | None = None + device_class: SensorDeviceClass | None = None suggested_unit_of_measurement: str | None = None last_reset: datetime | None = None native_unit_of_measurement: str | None = None @@ -483,7 +483,7 @@ class SensorEntity(Entity): """Base class for sensor entities.""" entity_description: SensorEntityDescription - _attr_device_class: SensorDeviceClass | str | None + _attr_device_class: SensorDeviceClass | None _attr_last_reset: datetime | None _attr_native_unit_of_measurement: str | None _attr_native_value: StateType | date | datetime | Decimal = None @@ -567,7 +567,7 @@ class SensorEntity(Entity): self.async_registry_entry_updated() @property - def device_class(self) -> SensorDeviceClass | str | None: + def device_class(self) -> SensorDeviceClass | None: """Return the class of this entity.""" if hasattr(self, "_attr_device_class"): return self._attr_device_class diff --git a/homeassistant/components/tasmota/sensor.py b/homeassistant/components/tasmota/sensor.py index 09b44eca1ee..1db2eb4dba6 100644 --- a/homeassistant/components/tasmota/sensor.py +++ b/homeassistant/components/tasmota/sensor.py @@ -294,7 +294,7 @@ class TasmotaSensor(TasmotaAvailability, TasmotaDiscoveryUpdate, SensorEntity): self.async_write_ha_state() @property - def device_class(self) -> str | None: + def device_class(self) -> SensorDeviceClass | None: """Return the device class of the sensor.""" class_or_icon = SENSOR_DEVICE_CLASS_ICON_MAP.get( self._tasmota_entity.quantity, {} diff --git a/pylint/plugins/hass_enforce_type_hints.py b/pylint/plugins/hass_enforce_type_hints.py index a9740a3f899..1e3ab900793 100644 --- a/pylint/plugins/hass_enforce_type_hints.py +++ b/pylint/plugins/hass_enforce_type_hints.py @@ -2157,7 +2157,7 @@ _INHERITANCE_MATCH: dict[str, list[ClassTypeHintMatch]] = { matches=[ TypeHintMatch( function_name="device_class", - return_type=["SensorDeviceClass", "str", None], + return_type=["SensorDeviceClass", None], ), TypeHintMatch( function_name="state_class",