Remove str from binary sensor device class (#83393)

This commit is contained in:
Franck Nijhof 2022-12-06 14:52:26 +01:00 committed by GitHub
parent 46caefca7b
commit cb69364ad2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 7 deletions

View File

@ -179,19 +179,19 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
class BinarySensorEntityDescription(EntityDescription): class BinarySensorEntityDescription(EntityDescription):
"""A class that describes binary sensor entities.""" """A class that describes binary sensor entities."""
device_class: BinarySensorDeviceClass | str | None = None device_class: BinarySensorDeviceClass | None = None
class BinarySensorEntity(Entity): class BinarySensorEntity(Entity):
"""Represent a binary sensor.""" """Represent a binary sensor."""
entity_description: BinarySensorEntityDescription entity_description: BinarySensorEntityDescription
_attr_device_class: BinarySensorDeviceClass | str | None _attr_device_class: BinarySensorDeviceClass | None
_attr_is_on: bool | None = None _attr_is_on: bool | None = None
_attr_state: None = None _attr_state: None = None
@property @property
def device_class(self) -> BinarySensorDeviceClass | str | None: def device_class(self) -> BinarySensorDeviceClass | 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

@ -1,7 +1,12 @@
"""Support for ONVIF binary sensors.""" """Support for ONVIF binary sensors."""
from __future__ import annotations from __future__ import annotations
from homeassistant.components.binary_sensor import BinarySensorEntity from contextlib import suppress
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_ON from homeassistant.const import STATE_ON
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
@ -61,13 +66,21 @@ class ONVIFBinarySensor(ONVIFBaseEntity, RestoreEntity, BinarySensorEntity):
"""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 = BinarySensorDeviceClass(
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
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 = BinarySensorDeviceClass(
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

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