Add select entities to ESPHome (#53526)

Co-authored-by: Otto Winter <otto@otto-winter.com>
This commit is contained in:
Jesse Hills 2021-07-27 21:45:04 +12:00 committed by GitHub
parent a6b34924be
commit 18bf0762b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 71 additions and 3 deletions

View file

@ -279,6 +279,7 @@ omit =
homeassistant/components/esphome/fan.py
homeassistant/components/esphome/light.py
homeassistant/components/esphome/number.py
homeassistant/components/esphome/select.py
homeassistant/components/esphome/sensor.py
homeassistant/components/esphome/switch.py
homeassistant/components/essent/sensor.py

View file

@ -19,6 +19,7 @@ from aioesphomeapi import (
FanInfo,
LightInfo,
NumberInfo,
SelectInfo,
SensorInfo,
SwitchInfo,
TextSensorInfo,
@ -41,6 +42,7 @@ INFO_TYPE_TO_PLATFORM: dict[type[EntityInfo], str] = {
FanInfo: "fan",
LightInfo: "light",
NumberInfo: "number",
SelectInfo: "select",
SensorInfo: "sensor",
SwitchInfo: "switch",
TextSensorInfo: "sensor",

View file

@ -3,7 +3,7 @@
"name": "ESPHome",
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/esphome",
"requirements": ["aioesphomeapi==5.0.1"],
"requirements": ["aioesphomeapi==5.1.1"],
"zeroconf": ["_esphomelib._tcp.local."],
"codeowners": ["@OttoWinter", "@jesserockz"],
"after_dependencies": ["zeroconf", "tag"],

View file

@ -0,0 +1,65 @@
"""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,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up esphome selects based on a config entry."""
await platform_async_setup_entry(
hass,
entry,
async_add_entities,
component_key="select",
info_type=SelectInfo,
entity_type=EsphomeSelect,
state_type=SelectState,
)
# https://github.com/PyCQA/pylint/issues/3150 for all @esphome_state_property
# pylint: disable=invalid-overridden-method
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."""
return self._static_info.options
@esphome_state_property
def current_option(self) -> str | None:
"""Return the state of the entity."""
if self._state.missing_state:
return None
return self._state.state
async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
await self._client.select_command(self._static_info.key, option)

View file

@ -163,7 +163,7 @@ aioeafm==0.1.2
aioemonitor==1.0.5
# homeassistant.components.esphome
aioesphomeapi==5.0.1
aioesphomeapi==5.1.1
# homeassistant.components.flo
aioflo==0.4.1

View file

@ -103,7 +103,7 @@ aioeafm==0.1.2
aioemonitor==1.0.5
# homeassistant.components.esphome
aioesphomeapi==5.0.1
aioesphomeapi==5.1.1
# homeassistant.components.flo
aioflo==0.4.1