Consolidate ESPHome icon-handling code into EsphomeEntity (#57744)

This commit is contained in:
Paul Monigatti 2021-10-23 05:21:41 +13:00 committed by GitHub
parent da7b67cc29
commit a3b3c4ebad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 11 additions and 47 deletions

View File

@ -637,6 +637,9 @@ class EsphomeEnumMapper(Generic[_EnumT, _ValT]):
return self._inverse[value] return self._inverse[value]
ICON_SCHEMA = vol.Schema(cv.icon)
class EsphomeEntity(Entity, Generic[_InfoT, _StateT]): class EsphomeEntity(Entity, Generic[_InfoT, _StateT]):
"""Define a base esphome entity.""" """Define a base esphome entity."""
@ -761,6 +764,14 @@ class EsphomeEntity(Entity, Generic[_InfoT, _StateT]):
"""Return the name of the entity.""" """Return the name of the entity."""
return self._static_info.name 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 @property
def should_poll(self) -> bool: def should_poll(self) -> bool:
"""Disable polling.""" """Disable polling."""

View File

@ -2,21 +2,16 @@
from __future__ import annotations from __future__ import annotations
import math import math
from typing import cast
from aioesphomeapi import NumberInfo, NumberState from aioesphomeapi import NumberInfo, NumberState
import voluptuous as vol
from homeassistant.components.number import NumberEntity from homeassistant.components.number import NumberEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry
ICON_SCHEMA = vol.Schema(cv.icon)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
@ -42,13 +37,6 @@ async def async_setup_entry(
class EsphomeNumber(EsphomeEntity[NumberInfo, NumberState], NumberEntity): class EsphomeNumber(EsphomeEntity[NumberInfo, NumberState], NumberEntity):
"""A number implementation for esphome.""" """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 @property
def min_value(self) -> float: def min_value(self) -> float:
"""Return the minimum value.""" """Return the minimum value."""

View File

@ -1,21 +1,15 @@
"""Support for esphome selects.""" """Support for esphome selects."""
from __future__ import annotations from __future__ import annotations
from typing import cast
from aioesphomeapi import SelectInfo, SelectState from aioesphomeapi import SelectInfo, SelectState
import voluptuous as vol
from homeassistant.components.select import SelectEntity from homeassistant.components.select import SelectEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry
ICON_SCHEMA = vol.Schema(cv.icon)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
@ -41,13 +35,6 @@ async def async_setup_entry(
class EsphomeSelect(EsphomeEntity[SelectInfo, SelectState], SelectEntity): class EsphomeSelect(EsphomeEntity[SelectInfo, SelectState], SelectEntity):
"""A select implementation for esphome.""" """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 @property
def options(self) -> list[str]: def options(self) -> list[str]:
"""Return a set of selectable options.""" """Return a set of selectable options."""

View File

@ -2,7 +2,6 @@
from __future__ import annotations from __future__ import annotations
import math import math
from typing import cast
from aioesphomeapi import ( from aioesphomeapi import (
SensorInfo, SensorInfo,
@ -12,7 +11,6 @@ from aioesphomeapi import (
TextSensorState, TextSensorState,
) )
from aioesphomeapi.model import LastResetType from aioesphomeapi.model import LastResetType
import voluptuous as vol
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
DEVICE_CLASS_TIMESTAMP, DEVICE_CLASS_TIMESTAMP,
@ -23,7 +21,6 @@ from homeassistant.components.sensor import (
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import dt from homeassistant.util import dt
@ -34,8 +31,6 @@ from . import (
platform_async_setup_entry, platform_async_setup_entry,
) )
ICON_SCHEMA = vol.Schema(cv.icon)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback 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): class EsphomeSensor(EsphomeEntity[SensorInfo, SensorState], SensorEntity):
"""A sensor implementation for esphome.""" """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 @property
def force_update(self) -> bool: def force_update(self) -> bool:
"""Return if this sensor should force a state update.""" """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): class EsphomeTextSensor(EsphomeEntity[TextSensorInfo, TextSensorState], SensorEntity):
"""A text sensor implementation for ESPHome.""" """A text sensor implementation for ESPHome."""
@property
def icon(self) -> str:
"""Return the icon."""
return self._static_info.icon
@esphome_state_property @esphome_state_property
def native_value(self) -> str | None: def native_value(self) -> str | None:
"""Return the state of the entity.""" """Return the state of the entity."""

View File

@ -35,11 +35,6 @@ async def async_setup_entry(
class EsphomeSwitch(EsphomeEntity[SwitchInfo, SwitchState], SwitchEntity): class EsphomeSwitch(EsphomeEntity[SwitchInfo, SwitchState], SwitchEntity):
"""A switch implementation for ESPHome.""" """A switch implementation for ESPHome."""
@property
def icon(self) -> str:
"""Return the icon."""
return self._static_info.icon
@property @property
def assumed_state(self) -> bool: def assumed_state(self) -> bool:
"""Return true if we do optimistic updates.""" """Return true if we do optimistic updates."""