mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 20:27:08 +00:00
Use try_parse_enum in integrations (#87085)
This commit is contained in:
parent
4a38b622b2
commit
bd6a4d10ea
@ -1,7 +1,6 @@
|
||||
"""Support for Abode Security System binary sensors."""
|
||||
from __future__ import annotations
|
||||
|
||||
from contextlib import suppress
|
||||
from typing import cast
|
||||
|
||||
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.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.util.enum import try_parse_enum
|
||||
|
||||
from . import AbodeDevice, AbodeSystem
|
||||
from .const import DOMAIN
|
||||
@ -54,6 +54,4 @@ class AbodeBinarySensor(AbodeDevice, BinarySensorEntity):
|
||||
"""Return the class of the binary sensor."""
|
||||
if self._device.get_value("is_window") == "1":
|
||||
return BinarySensorDeviceClass.WINDOW
|
||||
with suppress(ValueError):
|
||||
return BinarySensorDeviceClass(cast(str, self._device.generic_type))
|
||||
return None
|
||||
return try_parse_enum(BinarySensorDeviceClass, self._device.generic_type)
|
||||
|
@ -1,6 +1,5 @@
|
||||
"""Support for the Dynalite channels as covers."""
|
||||
|
||||
from contextlib import suppress
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.cover import (
|
||||
@ -11,6 +10,7 @@ from homeassistant.components.cover import (
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.util.enum import try_parse_enum
|
||||
|
||||
from .dynalitebase import DynaliteBase, async_setup_entry_base
|
||||
|
||||
@ -39,10 +39,8 @@ class DynaliteCover(DynaliteBase, CoverEntity):
|
||||
@property
|
||||
def device_class(self) -> CoverDeviceClass:
|
||||
"""Return the class of the device."""
|
||||
if device_class := self._device.device_class:
|
||||
with suppress(ValueError):
|
||||
return CoverDeviceClass(device_class)
|
||||
return CoverDeviceClass.SHUTTER
|
||||
device_class = try_parse_enum(CoverDeviceClass, self._device.device_class)
|
||||
return device_class or CoverDeviceClass.SHUTTER
|
||||
|
||||
@property
|
||||
def current_cover_position(self) -> int:
|
||||
|
@ -1,8 +1,6 @@
|
||||
"""Support for ESPHome binary sensors."""
|
||||
from __future__ import annotations
|
||||
|
||||
from contextlib import suppress
|
||||
|
||||
from aioesphomeapi import BinarySensorInfo, BinarySensorState
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
@ -12,6 +10,7 @@ from homeassistant.components.binary_sensor import (
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.util.enum import try_parse_enum
|
||||
|
||||
from . import EsphomeEntity, platform_async_setup_entry
|
||||
|
||||
@ -52,9 +51,7 @@ class EsphomeBinarySensor(
|
||||
@property
|
||||
def device_class(self) -> BinarySensorDeviceClass | None:
|
||||
"""Return the class of this device, from component DEVICE_CLASSES."""
|
||||
with suppress(ValueError):
|
||||
return BinarySensorDeviceClass(self._static_info.device_class)
|
||||
return None
|
||||
return try_parse_enum(BinarySensorDeviceClass, self._static_info.device_class)
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
|
@ -1,14 +1,13 @@
|
||||
"""Support for ESPHome buttons."""
|
||||
from __future__ import annotations
|
||||
|
||||
from contextlib import suppress
|
||||
|
||||
from aioesphomeapi import ButtonInfo, EntityState
|
||||
|
||||
from homeassistant.components.button import ButtonDeviceClass, ButtonEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.util.enum import try_parse_enum
|
||||
|
||||
from . import EsphomeEntity, platform_async_setup_entry
|
||||
|
||||
@ -34,9 +33,7 @@ class EsphomeButton(EsphomeEntity[ButtonInfo, EntityState], ButtonEntity):
|
||||
@property
|
||||
def device_class(self) -> ButtonDeviceClass | None:
|
||||
"""Return the class of this entity."""
|
||||
with suppress(ValueError):
|
||||
return ButtonDeviceClass(self._static_info.device_class)
|
||||
return None
|
||||
return try_parse_enum(ButtonDeviceClass, self._static_info.device_class)
|
||||
|
||||
@callback
|
||||
def _on_device_update(self) -> None:
|
||||
|
@ -1,7 +1,6 @@
|
||||
"""Support for ESPHome covers."""
|
||||
from __future__ import annotations
|
||||
|
||||
from contextlib import suppress
|
||||
from typing import Any
|
||||
|
||||
from aioesphomeapi import CoverInfo, CoverOperation, CoverState
|
||||
@ -16,6 +15,7 @@ from homeassistant.components.cover import (
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
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
|
||||
|
||||
@ -57,9 +57,7 @@ class EsphomeCover(EsphomeEntity[CoverInfo, CoverState], CoverEntity):
|
||||
@property
|
||||
def device_class(self) -> CoverDeviceClass | None:
|
||||
"""Return the class of this device, from component DEVICE_CLASSES."""
|
||||
with suppress(ValueError):
|
||||
return CoverDeviceClass(self._static_info.device_class)
|
||||
return None
|
||||
return try_parse_enum(CoverDeviceClass, self._static_info.device_class)
|
||||
|
||||
@property
|
||||
def assumed_state(self) -> bool:
|
||||
|
@ -1,7 +1,6 @@
|
||||
"""Support for esphome numbers."""
|
||||
from __future__ import annotations
|
||||
|
||||
from contextlib import suppress
|
||||
import math
|
||||
|
||||
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.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.util.enum import try_parse_enum
|
||||
|
||||
from . import (
|
||||
EsphomeEntity,
|
||||
@ -51,9 +51,7 @@ class EsphomeNumber(EsphomeEntity[NumberInfo, NumberState], NumberEntity):
|
||||
@property
|
||||
def device_class(self) -> NumberDeviceClass | None:
|
||||
"""Return the class of this entity."""
|
||||
with suppress(ValueError):
|
||||
return NumberDeviceClass(self._static_info.device_class)
|
||||
return None
|
||||
return try_parse_enum(NumberDeviceClass, self._static_info.device_class)
|
||||
|
||||
@property
|
||||
def native_min_value(self) -> float:
|
||||
|
@ -1,7 +1,6 @@
|
||||
"""Support for esphome sensors."""
|
||||
from __future__ import annotations
|
||||
|
||||
from contextlib import suppress
|
||||
from datetime import datetime
|
||||
import math
|
||||
|
||||
@ -23,6 +22,7 @@ from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.util import dt
|
||||
from homeassistant.util.enum import try_parse_enum
|
||||
|
||||
from . import (
|
||||
EsphomeEntity,
|
||||
@ -98,9 +98,7 @@ class EsphomeSensor(EsphomeEntity[SensorInfo, SensorState], SensorEntity):
|
||||
@property
|
||||
def device_class(self) -> SensorDeviceClass | None:
|
||||
"""Return the class of this device, from component DEVICE_CLASSES."""
|
||||
with suppress(ValueError):
|
||||
return SensorDeviceClass(self._static_info.device_class)
|
||||
return None
|
||||
return try_parse_enum(SensorDeviceClass, self._static_info.device_class)
|
||||
|
||||
@property
|
||||
def state_class(self) -> SensorStateClass | None:
|
||||
|
@ -1,7 +1,6 @@
|
||||
"""Support for ESPHome switches."""
|
||||
from __future__ import annotations
|
||||
|
||||
from contextlib import suppress
|
||||
from typing import Any
|
||||
|
||||
from aioesphomeapi import SwitchInfo, SwitchState
|
||||
@ -10,6 +9,7 @@ from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
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
|
||||
|
||||
@ -46,9 +46,7 @@ class EsphomeSwitch(EsphomeEntity[SwitchInfo, SwitchState], SwitchEntity):
|
||||
@property
|
||||
def device_class(self) -> SwitchDeviceClass | None:
|
||||
"""Return the class of this device."""
|
||||
with suppress(ValueError):
|
||||
return SwitchDeviceClass(self._static_info.device_class)
|
||||
return None
|
||||
return try_parse_enum(SwitchDeviceClass, self._static_info.device_class)
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn the entity on."""
|
||||
|
@ -1,7 +1,6 @@
|
||||
"""Support for KNX/IP sensors."""
|
||||
from __future__ import annotations
|
||||
|
||||
from contextlib import suppress
|
||||
from typing import Any
|
||||
|
||||
from xknx import XKNX
|
||||
@ -23,6 +22,7 @@ from homeassistant.const import (
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
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 .knx_entity import KnxEntity
|
||||
@ -64,10 +64,10 @@ class KNXSensor(KnxEntity, SensorEntity):
|
||||
if device_class := config.get(CONF_DEVICE_CLASS):
|
||||
self._attr_device_class = device_class
|
||||
else:
|
||||
with suppress(ValueError):
|
||||
self._attr_device_class = SensorDeviceClass(
|
||||
str(self._device.ha_device_class())
|
||||
)
|
||||
self._attr_device_class = try_parse_enum(
|
||||
SensorDeviceClass, self._device.ha_device_class()
|
||||
)
|
||||
|
||||
self._attr_force_update = self._device.always_callback
|
||||
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
|
||||
self._attr_unique_id = str(self._device.sensor_value.group_address_state)
|
||||
|
@ -1,8 +1,6 @@
|
||||
"""Support for ONVIF binary sensors."""
|
||||
from __future__ import annotations
|
||||
|
||||
from contextlib import suppress
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorDeviceClass,
|
||||
BinarySensorEntity,
|
||||
@ -13,6 +11,7 @@ from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
from homeassistant.util.enum import try_parse_enum
|
||||
|
||||
from .base import ONVIFBaseEntity
|
||||
from .const import DOMAIN
|
||||
@ -66,21 +65,17 @@ class ONVIFBinarySensor(ONVIFBaseEntity, RestoreEntity, BinarySensorEntity):
|
||||
"""Initialize the ONVIF binary sensor."""
|
||||
self._attr_unique_id = uid
|
||||
if entry is not None:
|
||||
if entry.original_device_class:
|
||||
with suppress(ValueError):
|
||||
self._attr_device_class = BinarySensorDeviceClass(
|
||||
entry.original_device_class
|
||||
)
|
||||
self._attr_device_class = try_parse_enum(
|
||||
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
|
||||
if event.device_class:
|
||||
with suppress(ValueError):
|
||||
self._attr_device_class = BinarySensorDeviceClass(
|
||||
event.device_class
|
||||
)
|
||||
self._attr_device_class = try_parse_enum(
|
||||
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}"
|
||||
|
@ -1,7 +1,6 @@
|
||||
"""Support for ONVIF binary sensors."""
|
||||
from __future__ import annotations
|
||||
|
||||
from contextlib import suppress
|
||||
from datetime import date, datetime
|
||||
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.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
from homeassistant.util.enum import try_parse_enum
|
||||
|
||||
from .base import ONVIFBaseEntity
|
||||
from .const import DOMAIN
|
||||
@ -59,20 +59,18 @@ class ONVIFSensor(ONVIFBaseEntity, RestoreSensor):
|
||||
"""Initialize the ONVIF binary sensor."""
|
||||
self._attr_unique_id = uid
|
||||
if entry is not None:
|
||||
if entry.original_device_class:
|
||||
with suppress(ValueError):
|
||||
self._attr_device_class = SensorDeviceClass(
|
||||
entry.original_device_class
|
||||
)
|
||||
self._attr_device_class = try_parse_enum(
|
||||
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
|
||||
if event.device_class:
|
||||
with suppress(ValueError):
|
||||
self._attr_device_class = SensorDeviceClass(event.device_class)
|
||||
self._attr_device_class = try_parse_enum(
|
||||
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}"
|
||||
|
Loading…
x
Reference in New Issue
Block a user