Use try_parse_enum in integrations (#87085)

This commit is contained in:
epenet 2023-02-02 12:34:01 +01:00 committed by GitHub
parent 4a38b622b2
commit bd6a4d10ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 36 additions and 61 deletions

View file

@ -1,7 +1,6 @@
"""Support for Abode Security System binary sensors.""" """Support for Abode Security System binary sensors."""
from __future__ import annotations from __future__ import annotations
from contextlib import suppress
from typing import cast from typing import cast
from jaraco.abode.devices.sensor import BinarySensor as ABBinarySensor from jaraco.abode.devices.sensor import BinarySensor as ABBinarySensor
@ -14,6 +13,7 @@ from homeassistant.components.binary_sensor import (
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.enum import try_parse_enum
from . import AbodeDevice, AbodeSystem from . import AbodeDevice, AbodeSystem
from .const import DOMAIN from .const import DOMAIN
@ -54,6 +54,4 @@ class AbodeBinarySensor(AbodeDevice, BinarySensorEntity):
"""Return the class of the binary sensor.""" """Return the class of the binary sensor."""
if self._device.get_value("is_window") == "1": if self._device.get_value("is_window") == "1":
return BinarySensorDeviceClass.WINDOW return BinarySensorDeviceClass.WINDOW
with suppress(ValueError): return try_parse_enum(BinarySensorDeviceClass, self._device.generic_type)
return BinarySensorDeviceClass(cast(str, self._device.generic_type))
return None

View file

@ -1,6 +1,5 @@
"""Support for the Dynalite channels as covers.""" """Support for the Dynalite channels as covers."""
from contextlib import suppress
from typing import Any from typing import Any
from homeassistant.components.cover import ( from homeassistant.components.cover import (
@ -11,6 +10,7 @@ from homeassistant.components.cover import (
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.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.enum import try_parse_enum
from .dynalitebase import DynaliteBase, async_setup_entry_base from .dynalitebase import DynaliteBase, async_setup_entry_base
@ -39,10 +39,8 @@ class DynaliteCover(DynaliteBase, CoverEntity):
@property @property
def device_class(self) -> CoverDeviceClass: def device_class(self) -> CoverDeviceClass:
"""Return the class of the device.""" """Return the class of the device."""
if device_class := self._device.device_class: device_class = try_parse_enum(CoverDeviceClass, self._device.device_class)
with suppress(ValueError): return device_class or CoverDeviceClass.SHUTTER
return CoverDeviceClass(device_class)
return CoverDeviceClass.SHUTTER
@property @property
def current_cover_position(self) -> int: def current_cover_position(self) -> int:

View file

@ -1,8 +1,6 @@
"""Support for ESPHome binary sensors.""" """Support for ESPHome binary sensors."""
from __future__ import annotations from __future__ import annotations
from contextlib import suppress
from aioesphomeapi import BinarySensorInfo, BinarySensorState from aioesphomeapi import BinarySensorInfo, BinarySensorState
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
@ -12,6 +10,7 @@ from homeassistant.components.binary_sensor import (
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.enum import try_parse_enum
from . import EsphomeEntity, platform_async_setup_entry from . import EsphomeEntity, platform_async_setup_entry
@ -52,9 +51,7 @@ class EsphomeBinarySensor(
@property @property
def device_class(self) -> BinarySensorDeviceClass | None: def device_class(self) -> BinarySensorDeviceClass | None:
"""Return the class of this device, from component DEVICE_CLASSES.""" """Return the class of this device, from component DEVICE_CLASSES."""
with suppress(ValueError): return try_parse_enum(BinarySensorDeviceClass, self._static_info.device_class)
return BinarySensorDeviceClass(self._static_info.device_class)
return None
@property @property
def available(self) -> bool: def available(self) -> bool:

View file

@ -1,14 +1,13 @@
"""Support for ESPHome buttons.""" """Support for ESPHome buttons."""
from __future__ import annotations from __future__ import annotations
from contextlib import suppress
from aioesphomeapi import ButtonInfo, EntityState from aioesphomeapi import ButtonInfo, EntityState
from homeassistant.components.button import ButtonDeviceClass, ButtonEntity from homeassistant.components.button import ButtonDeviceClass, ButtonEntity
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.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.enum import try_parse_enum
from . import EsphomeEntity, platform_async_setup_entry from . import EsphomeEntity, platform_async_setup_entry
@ -34,9 +33,7 @@ class EsphomeButton(EsphomeEntity[ButtonInfo, EntityState], ButtonEntity):
@property @property
def device_class(self) -> ButtonDeviceClass | None: def device_class(self) -> ButtonDeviceClass | None:
"""Return the class of this entity.""" """Return the class of this entity."""
with suppress(ValueError): return try_parse_enum(ButtonDeviceClass, self._static_info.device_class)
return ButtonDeviceClass(self._static_info.device_class)
return None
@callback @callback
def _on_device_update(self) -> None: def _on_device_update(self) -> None:

View file

@ -1,7 +1,6 @@
"""Support for ESPHome covers.""" """Support for ESPHome covers."""
from __future__ import annotations from __future__ import annotations
from contextlib import suppress
from typing import Any from typing import Any
from aioesphomeapi import CoverInfo, CoverOperation, CoverState from aioesphomeapi import CoverInfo, CoverOperation, CoverState
@ -16,6 +15,7 @@ from homeassistant.components.cover import (
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.enum import try_parse_enum
from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry
@ -57,9 +57,7 @@ class EsphomeCover(EsphomeEntity[CoverInfo, CoverState], CoverEntity):
@property @property
def device_class(self) -> CoverDeviceClass | None: def device_class(self) -> CoverDeviceClass | None:
"""Return the class of this device, from component DEVICE_CLASSES.""" """Return the class of this device, from component DEVICE_CLASSES."""
with suppress(ValueError): return try_parse_enum(CoverDeviceClass, self._static_info.device_class)
return CoverDeviceClass(self._static_info.device_class)
return None
@property @property
def assumed_state(self) -> bool: def assumed_state(self) -> bool:

View file

@ -1,7 +1,6 @@
"""Support for esphome numbers.""" """Support for esphome numbers."""
from __future__ import annotations from __future__ import annotations
from contextlib import suppress
import math import math
from aioesphomeapi import NumberInfo, NumberMode as EsphomeNumberMode, NumberState from aioesphomeapi import NumberInfo, NumberMode as EsphomeNumberMode, NumberState
@ -10,6 +9,7 @@ from homeassistant.components.number import NumberDeviceClass, NumberEntity, Num
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.enum import try_parse_enum
from . import ( from . import (
EsphomeEntity, EsphomeEntity,
@ -51,9 +51,7 @@ class EsphomeNumber(EsphomeEntity[NumberInfo, NumberState], NumberEntity):
@property @property
def device_class(self) -> NumberDeviceClass | None: def device_class(self) -> NumberDeviceClass | None:
"""Return the class of this entity.""" """Return the class of this entity."""
with suppress(ValueError): return try_parse_enum(NumberDeviceClass, self._static_info.device_class)
return NumberDeviceClass(self._static_info.device_class)
return None
@property @property
def native_min_value(self) -> float: def native_min_value(self) -> float:

View file

@ -1,7 +1,6 @@
"""Support for esphome sensors.""" """Support for esphome sensors."""
from __future__ import annotations from __future__ import annotations
from contextlib import suppress
from datetime import datetime from datetime import datetime
import math import math
@ -23,6 +22,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import dt from homeassistant.util import dt
from homeassistant.util.enum import try_parse_enum
from . import ( from . import (
EsphomeEntity, EsphomeEntity,
@ -98,9 +98,7 @@ class EsphomeSensor(EsphomeEntity[SensorInfo, SensorState], SensorEntity):
@property @property
def device_class(self) -> SensorDeviceClass | None: def device_class(self) -> SensorDeviceClass | None:
"""Return the class of this device, from component DEVICE_CLASSES.""" """Return the class of this device, from component DEVICE_CLASSES."""
with suppress(ValueError): return try_parse_enum(SensorDeviceClass, self._static_info.device_class)
return SensorDeviceClass(self._static_info.device_class)
return None
@property @property
def state_class(self) -> SensorStateClass | None: def state_class(self) -> SensorStateClass | None:

View file

@ -1,7 +1,6 @@
"""Support for ESPHome switches.""" """Support for ESPHome switches."""
from __future__ import annotations from __future__ import annotations
from contextlib import suppress
from typing import Any from typing import Any
from aioesphomeapi import SwitchInfo, SwitchState from aioesphomeapi import SwitchInfo, SwitchState
@ -10,6 +9,7 @@ from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.enum import try_parse_enum
from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry
@ -46,9 +46,7 @@ class EsphomeSwitch(EsphomeEntity[SwitchInfo, SwitchState], SwitchEntity):
@property @property
def device_class(self) -> SwitchDeviceClass | None: def device_class(self) -> SwitchDeviceClass | None:
"""Return the class of this device.""" """Return the class of this device."""
with suppress(ValueError): return try_parse_enum(SwitchDeviceClass, self._static_info.device_class)
return SwitchDeviceClass(self._static_info.device_class)
return None
async def async_turn_on(self, **kwargs: Any) -> None: async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the entity on.""" """Turn the entity on."""

View file

@ -1,7 +1,6 @@
"""Support for KNX/IP sensors.""" """Support for KNX/IP sensors."""
from __future__ import annotations from __future__ import annotations
from contextlib import suppress
from typing import Any from typing import Any
from xknx import XKNX from xknx import XKNX
@ -23,6 +22,7 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, StateType from homeassistant.helpers.typing import ConfigType, StateType
from homeassistant.util.enum import try_parse_enum
from .const import ATTR_SOURCE, DATA_KNX_CONFIG, DOMAIN from .const import ATTR_SOURCE, DATA_KNX_CONFIG, DOMAIN
from .knx_entity import KnxEntity from .knx_entity import KnxEntity
@ -64,10 +64,10 @@ class KNXSensor(KnxEntity, SensorEntity):
if device_class := config.get(CONF_DEVICE_CLASS): if device_class := config.get(CONF_DEVICE_CLASS):
self._attr_device_class = device_class self._attr_device_class = device_class
else: else:
with suppress(ValueError): self._attr_device_class = try_parse_enum(
self._attr_device_class = SensorDeviceClass( SensorDeviceClass, self._device.ha_device_class()
str(self._device.ha_device_class()) )
)
self._attr_force_update = self._device.always_callback self._attr_force_update = self._device.always_callback
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY) self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
self._attr_unique_id = str(self._device.sensor_value.group_address_state) self._attr_unique_id = str(self._device.sensor_value.group_address_state)

View file

@ -1,8 +1,6 @@
"""Support for ONVIF binary sensors.""" """Support for ONVIF binary sensors."""
from __future__ import annotations from __future__ import annotations
from contextlib import suppress
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass, BinarySensorDeviceClass,
BinarySensorEntity, BinarySensorEntity,
@ -13,6 +11,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.util.enum import try_parse_enum
from .base import ONVIFBaseEntity from .base import ONVIFBaseEntity
from .const import DOMAIN from .const import DOMAIN
@ -66,21 +65,17 @@ 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:
if entry.original_device_class: self._attr_device_class = try_parse_enum(
with suppress(ValueError): BinarySensorDeviceClass, entry.original_device_class
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
if event.device_class: self._attr_device_class = try_parse_enum(
with suppress(ValueError): BinarySensorDeviceClass, event.device_class
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

@ -1,7 +1,6 @@
"""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
@ -11,6 +10,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType from homeassistant.helpers.typing import StateType
from homeassistant.util.enum import try_parse_enum
from .base import ONVIFBaseEntity from .base import ONVIFBaseEntity
from .const import DOMAIN from .const import DOMAIN
@ -59,20 +59,18 @@ 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:
if entry.original_device_class: self._attr_device_class = try_parse_enum(
with suppress(ValueError): SensorDeviceClass, entry.original_device_class
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
if event.device_class: self._attr_device_class = try_parse_enum(
with suppress(ValueError): SensorDeviceClass, event.device_class
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}"