From cb69364ad2f36b68fa5404400b88948e8bdd7173 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Tue, 6 Dec 2022 14:52:26 +0100 Subject: [PATCH] Remove str from binary sensor device class (#83393) --- .../components/binary_sensor/__init__.py | 6 +++--- .../components/onvif/binary_sensor.py | 19 ++++++++++++++++--- pylint/plugins/hass_enforce_type_hints.py | 2 +- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/binary_sensor/__init__.py b/homeassistant/components/binary_sensor/__init__.py index 46107938ddf..d99f569ed59 100644 --- a/homeassistant/components/binary_sensor/__init__.py +++ b/homeassistant/components/binary_sensor/__init__.py @@ -179,19 +179,19 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: class BinarySensorEntityDescription(EntityDescription): """A class that describes binary sensor entities.""" - device_class: BinarySensorDeviceClass | str | None = None + device_class: BinarySensorDeviceClass | None = None class BinarySensorEntity(Entity): """Represent a binary sensor.""" entity_description: BinarySensorEntityDescription - _attr_device_class: BinarySensorDeviceClass | str | None + _attr_device_class: BinarySensorDeviceClass | None _attr_is_on: bool | None = None _attr_state: None = None @property - def device_class(self) -> BinarySensorDeviceClass | str | None: + def device_class(self) -> BinarySensorDeviceClass | None: """Return the class of this entity.""" if hasattr(self, "_attr_device_class"): return self._attr_device_class diff --git a/homeassistant/components/onvif/binary_sensor.py b/homeassistant/components/onvif/binary_sensor.py index 2a7f23c61ee..93298c0cc63 100644 --- a/homeassistant/components/onvif/binary_sensor.py +++ b/homeassistant/components/onvif/binary_sensor.py @@ -1,7 +1,12 @@ """Support for ONVIF binary sensors.""" 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.const import STATE_ON from homeassistant.core import HomeAssistant, callback @@ -61,13 +66,21 @@ class ONVIFBinarySensor(ONVIFBaseEntity, RestoreEntity, BinarySensorEntity): """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 = BinarySensorDeviceClass( + entry.original_device_class + ) self._attr_entity_category = entry.entity_category self._attr_name = entry.name 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 = BinarySensorDeviceClass( + 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/pylint/plugins/hass_enforce_type_hints.py b/pylint/plugins/hass_enforce_type_hints.py index 7cfd5d32914..a418399b450 100644 --- a/pylint/plugins/hass_enforce_type_hints.py +++ b/pylint/plugins/hass_enforce_type_hints.py @@ -732,7 +732,7 @@ _INHERITANCE_MATCH: dict[str, list[ClassTypeHintMatch]] = { matches=[ TypeHintMatch( function_name="device_class", - return_type=["BinarySensorDeviceClass", "str", None], + return_type=["BinarySensorDeviceClass", None], ), TypeHintMatch( function_name="is_on",