mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 13:47:35 +00:00
Consolidate ESPHome icon-handling code into EsphomeEntity (#57744)
This commit is contained in:
parent
da7b67cc29
commit
a3b3c4ebad
@ -637,6 +637,9 @@ class EsphomeEnumMapper(Generic[_EnumT, _ValT]):
|
||||
return self._inverse[value]
|
||||
|
||||
|
||||
ICON_SCHEMA = vol.Schema(cv.icon)
|
||||
|
||||
|
||||
class EsphomeEntity(Entity, Generic[_InfoT, _StateT]):
|
||||
"""Define a base esphome entity."""
|
||||
|
||||
@ -761,6 +764,14 @@ class EsphomeEntity(Entity, Generic[_InfoT, _StateT]):
|
||||
"""Return the name of the entity."""
|
||||
return self._static_info.name
|
||||
|
||||
@property
|
||||
def icon(self) -> str | None:
|
||||
"""Return the icon."""
|
||||
if not self._static_info.icon:
|
||||
return None
|
||||
|
||||
return cast(str, ICON_SCHEMA(self._static_info.icon))
|
||||
|
||||
@property
|
||||
def should_poll(self) -> bool:
|
||||
"""Disable polling."""
|
||||
|
@ -2,21 +2,16 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from typing import cast
|
||||
|
||||
from aioesphomeapi import NumberInfo, NumberState
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.number import NumberEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry
|
||||
|
||||
ICON_SCHEMA = vol.Schema(cv.icon)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
@ -42,13 +37,6 @@ async def async_setup_entry(
|
||||
class EsphomeNumber(EsphomeEntity[NumberInfo, NumberState], NumberEntity):
|
||||
"""A number implementation for esphome."""
|
||||
|
||||
@property
|
||||
def icon(self) -> str | None:
|
||||
"""Return the icon."""
|
||||
if not self._static_info.icon:
|
||||
return None
|
||||
return cast(str, ICON_SCHEMA(self._static_info.icon))
|
||||
|
||||
@property
|
||||
def min_value(self) -> float:
|
||||
"""Return the minimum value."""
|
||||
|
@ -1,21 +1,15 @@
|
||||
"""Support for esphome selects."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import cast
|
||||
|
||||
from aioesphomeapi import SelectInfo, SelectState
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.select import SelectEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry
|
||||
|
||||
ICON_SCHEMA = vol.Schema(cv.icon)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
@ -41,13 +35,6 @@ async def async_setup_entry(
|
||||
class EsphomeSelect(EsphomeEntity[SelectInfo, SelectState], SelectEntity):
|
||||
"""A select implementation for esphome."""
|
||||
|
||||
@property
|
||||
def icon(self) -> str | None:
|
||||
"""Return the icon."""
|
||||
if not self._static_info.icon:
|
||||
return None
|
||||
return cast(str, ICON_SCHEMA(self._static_info.icon))
|
||||
|
||||
@property
|
||||
def options(self) -> list[str]:
|
||||
"""Return a set of selectable options."""
|
||||
|
@ -2,7 +2,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from typing import cast
|
||||
|
||||
from aioesphomeapi import (
|
||||
SensorInfo,
|
||||
@ -12,7 +11,6 @@ from aioesphomeapi import (
|
||||
TextSensorState,
|
||||
)
|
||||
from aioesphomeapi.model import LastResetType
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
DEVICE_CLASS_TIMESTAMP,
|
||||
@ -23,7 +21,6 @@ from homeassistant.components.sensor import (
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.util import dt
|
||||
|
||||
@ -34,8 +31,6 @@ from . import (
|
||||
platform_async_setup_entry,
|
||||
)
|
||||
|
||||
ICON_SCHEMA = vol.Schema(cv.icon)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
@ -77,13 +72,6 @@ _STATE_CLASSES: EsphomeEnumMapper[SensorStateClass, str | None] = EsphomeEnumMap
|
||||
class EsphomeSensor(EsphomeEntity[SensorInfo, SensorState], SensorEntity):
|
||||
"""A sensor implementation for esphome."""
|
||||
|
||||
@property
|
||||
def icon(self) -> str | None:
|
||||
"""Return the icon."""
|
||||
if not self._static_info.icon or self._static_info.device_class:
|
||||
return None
|
||||
return cast(str, ICON_SCHEMA(self._static_info.icon))
|
||||
|
||||
@property
|
||||
def force_update(self) -> bool:
|
||||
"""Return if this sensor should force a state update."""
|
||||
@ -133,11 +121,6 @@ class EsphomeSensor(EsphomeEntity[SensorInfo, SensorState], SensorEntity):
|
||||
class EsphomeTextSensor(EsphomeEntity[TextSensorInfo, TextSensorState], SensorEntity):
|
||||
"""A text sensor implementation for ESPHome."""
|
||||
|
||||
@property
|
||||
def icon(self) -> str:
|
||||
"""Return the icon."""
|
||||
return self._static_info.icon
|
||||
|
||||
@esphome_state_property
|
||||
def native_value(self) -> str | None:
|
||||
"""Return the state of the entity."""
|
||||
|
@ -35,11 +35,6 @@ async def async_setup_entry(
|
||||
class EsphomeSwitch(EsphomeEntity[SwitchInfo, SwitchState], SwitchEntity):
|
||||
"""A switch implementation for ESPHome."""
|
||||
|
||||
@property
|
||||
def icon(self) -> str:
|
||||
"""Return the icon."""
|
||||
return self._static_info.icon
|
||||
|
||||
@property
|
||||
def assumed_state(self) -> bool:
|
||||
"""Return true if we do optimistic updates."""
|
||||
|
Loading…
x
Reference in New Issue
Block a user