mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Improve device class handling in ESPHome (#82923)
This commit is contained in:
parent
9805d8a6cc
commit
412c12b992
@ -1,9 +1,14 @@
|
|||||||
"""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 DEVICE_CLASSES, BinarySensorEntity
|
from homeassistant.components.binary_sensor import (
|
||||||
|
BinarySensorDeviceClass,
|
||||||
|
BinarySensorEntity,
|
||||||
|
)
|
||||||
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
|
||||||
@ -45,11 +50,11 @@ class EsphomeBinarySensor(
|
|||||||
return self._state.state
|
return self._state.state
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_class(self) -> str | 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."""
|
||||||
if self._static_info.device_class not in DEVICE_CLASSES:
|
with suppress(ValueError):
|
||||||
return None
|
return BinarySensorDeviceClass(self._static_info.device_class)
|
||||||
return self._static_info.device_class
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"""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
|
||||||
|
|
||||||
@ -14,7 +15,6 @@ from aioesphomeapi import (
|
|||||||
from aioesphomeapi.model import LastResetType
|
from aioesphomeapi.model import LastResetType
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
DEVICE_CLASSES,
|
|
||||||
SensorDeviceClass,
|
SensorDeviceClass,
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
@ -96,11 +96,11 @@ class EsphomeSensor(EsphomeEntity[SensorInfo, SensorState], SensorEntity):
|
|||||||
return self._static_info.unit_of_measurement
|
return self._static_info.unit_of_measurement
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_class(self) -> str | 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."""
|
||||||
if self._static_info.device_class not in DEVICE_CLASSES:
|
with suppress(ValueError):
|
||||||
return None
|
return SensorDeviceClass(self._static_info.device_class)
|
||||||
return self._static_info.device_class
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state_class(self) -> SensorStateClass | None:
|
def state_class(self) -> SensorStateClass | None:
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
"""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
|
||||||
|
|
||||||
from homeassistant.components.switch import DEVICE_CLASSES, SwitchEntity
|
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
|
||||||
@ -43,11 +44,11 @@ class EsphomeSwitch(EsphomeEntity[SwitchInfo, SwitchState], SwitchEntity):
|
|||||||
return self._state.state
|
return self._state.state
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_class(self) -> str | None:
|
def device_class(self) -> SwitchDeviceClass | None:
|
||||||
"""Return the class of this device."""
|
"""Return the class of this device."""
|
||||||
if self._static_info.device_class not in DEVICE_CLASSES:
|
with suppress(ValueError):
|
||||||
return None
|
return SwitchDeviceClass(self._static_info.device_class)
|
||||||
return 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."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user