Remove str from sensor device class (#83391)

This commit is contained in:
Franck Nijhof 2022-12-08 19:04:58 +01:00 committed by GitHub
parent 3ba264c318
commit 9864d9e0d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 22 additions and 15 deletions

View File

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import ValuesView from collections.abc import ValuesView
from typing import Optional, TypedDict from typing import TypedDict
from canary.model import Location from canary.model import Location
@ -12,6 +12,3 @@ class CanaryData(TypedDict):
locations: dict[str, Location] locations: dict[str, Location]
readings: dict[str, ValuesView] readings: dict[str, ValuesView]
SensorTypeItem = tuple[str, Optional[str], Optional[str], Optional[str], list[str]]

View File

@ -1,7 +1,7 @@
"""Support for Canary sensors.""" """Support for Canary sensors."""
from __future__ import annotations from __future__ import annotations
from typing import Final from typing import Final, Optional
from canary.model import Device, Location, SensorType 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 .const import DATA_COORDINATOR, DOMAIN, MANUFACTURER
from .coordinator import CanaryDataUpdateCoordinator from .coordinator import CanaryDataUpdateCoordinator
from .model import SensorTypeItem
SensorTypeItem = tuple[
str, Optional[str], Optional[str], Optional[SensorDeviceClass], list[str]
]
SENSOR_VALUE_PRECISION: Final = 2 SENSOR_VALUE_PRECISION: Final = 2
ATTR_AIR_QUALITY: Final = "air_quality" ATTR_AIR_QUALITY: Final = "air_quality"

View File

@ -158,7 +158,7 @@ class DemoSensor(SensorEntity):
unique_id: str, unique_id: str,
name: str, name: str,
state: StateType, state: StateType,
device_class: SensorDeviceClass | str, device_class: SensorDeviceClass,
state_class: SensorStateClass | None, state_class: SensorStateClass | None,
unit_of_measurement: str | None, unit_of_measurement: str | None,
battery: StateType, battery: StateType,

View File

@ -1,10 +1,11 @@
"""Support for ONVIF binary sensors.""" """Support for ONVIF binary sensors."""
from __future__ import annotations from __future__ import annotations
from contextlib import suppress
from datetime import date, datetime from datetime import date, datetime
from decimal import Decimal 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.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
@ -58,14 +59,20 @@ class ONVIFSensor(ONVIFBaseEntity, RestoreSensor):
"""Initialize the ONVIF binary sensor.""" """Initialize the ONVIF binary sensor."""
self._attr_unique_id = uid self._attr_unique_id = uid
if entry is not None: 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_entity_category = entry.entity_category
self._attr_name = entry.name self._attr_name = entry.name
self._attr_native_unit_of_measurement = entry.unit_of_measurement self._attr_native_unit_of_measurement = entry.unit_of_measurement
else: else:
event = device.events.get_uid(uid) event = device.events.get_uid(uid)
assert event 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_category = event.entity_category
self._attr_entity_registry_enabled_default = event.entity_enabled self._attr_entity_registry_enabled_default = event.entity_enabled
self._attr_name = f"{device.name} {event.name}" self._attr_name = f"{device.name} {event.name}"

View File

@ -470,7 +470,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
class SensorEntityDescription(EntityDescription): class SensorEntityDescription(EntityDescription):
"""A class that describes sensor entities.""" """A class that describes sensor entities."""
device_class: SensorDeviceClass | str | None = None device_class: SensorDeviceClass | None = None
suggested_unit_of_measurement: str | None = None suggested_unit_of_measurement: str | None = None
last_reset: datetime | None = None last_reset: datetime | None = None
native_unit_of_measurement: str | None = None native_unit_of_measurement: str | None = None
@ -483,7 +483,7 @@ class SensorEntity(Entity):
"""Base class for sensor entities.""" """Base class for sensor entities."""
entity_description: SensorEntityDescription entity_description: SensorEntityDescription
_attr_device_class: SensorDeviceClass | str | None _attr_device_class: SensorDeviceClass | None
_attr_last_reset: datetime | None _attr_last_reset: datetime | None
_attr_native_unit_of_measurement: str | None _attr_native_unit_of_measurement: str | None
_attr_native_value: StateType | date | datetime | Decimal = None _attr_native_value: StateType | date | datetime | Decimal = None
@ -567,7 +567,7 @@ class SensorEntity(Entity):
self.async_registry_entry_updated() self.async_registry_entry_updated()
@property @property
def device_class(self) -> SensorDeviceClass | str | None: def device_class(self) -> SensorDeviceClass | None:
"""Return the class of this entity.""" """Return the class of this entity."""
if hasattr(self, "_attr_device_class"): if hasattr(self, "_attr_device_class"):
return self._attr_device_class return self._attr_device_class

View File

@ -294,7 +294,7 @@ class TasmotaSensor(TasmotaAvailability, TasmotaDiscoveryUpdate, SensorEntity):
self.async_write_ha_state() self.async_write_ha_state()
@property @property
def device_class(self) -> str | None: def device_class(self) -> SensorDeviceClass | None:
"""Return the device class of the sensor.""" """Return the device class of the sensor."""
class_or_icon = SENSOR_DEVICE_CLASS_ICON_MAP.get( class_or_icon = SENSOR_DEVICE_CLASS_ICON_MAP.get(
self._tasmota_entity.quantity, {} self._tasmota_entity.quantity, {}

View File

@ -2157,7 +2157,7 @@ _INHERITANCE_MATCH: dict[str, list[ClassTypeHintMatch]] = {
matches=[ matches=[
TypeHintMatch( TypeHintMatch(
function_name="device_class", function_name="device_class",
return_type=["SensorDeviceClass", "str", None], return_type=["SensorDeviceClass", None],
), ),
TypeHintMatch( TypeHintMatch(
function_name="state_class", function_name="state_class",