1
mirror of https://github.com/home-assistant/core synced 2024-07-30 21:18:57 +02:00
ha-core/homeassistant/components/demo/select.py
Jan Bouwhuis 8299d0a7c3
Validate Select option before calling entity method (#52352)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
Co-authored-by: Franck Nijhof <git@frenck.dev>
2021-08-04 11:12:42 +02:00

78 lines
2.3 KiB
Python

"""Demo platform that offers a fake select entity."""
from __future__ import annotations
from homeassistant.components.select import SelectEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DEVICE_DEFAULT_NAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DOMAIN
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType = None,
) -> None:
"""Set up the demo Select entity."""
async_add_entities(
[
DemoSelect(
unique_id="speed",
name="Speed",
icon="mdi:speedometer",
device_class="demo__speed",
current_option="ridiculous_speed",
options=[
"light_speed",
"ridiculous_speed",
"ludicrous_speed",
],
),
]
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Demo config entry."""
await async_setup_platform(hass, {}, async_add_entities)
class DemoSelect(SelectEntity):
"""Representation of a demo select entity."""
_attr_should_poll = False
def __init__(
self,
unique_id: str,
name: str,
icon: str,
device_class: str | None,
current_option: str | None,
options: list[str],
) -> None:
"""Initialize the Demo select entity."""
self._attr_unique_id = unique_id
self._attr_name = name or DEVICE_DEFAULT_NAME
self._attr_current_option = current_option
self._attr_icon = icon
self._attr_device_class = device_class
self._attr_options = options
self._attr_device_info = {
"identifiers": {(DOMAIN, unique_id)},
"name": name,
}
async def async_select_option(self, option: str) -> None:
"""Update the current selected option."""
self._attr_current_option = option
self.async_write_ha_state()