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 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]]

View file

@ -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"

View file

@ -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,

View file

@ -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}"

View file

@ -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

View file

@ -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, {}

View file

@ -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",