Add Select entity component platform (#51849)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
bc329cb602
commit
054ca1d7ec
16 changed files with 345 additions and 0 deletions
|
@ -20,6 +20,7 @@ COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM = [
|
|||
"lock",
|
||||
"media_player",
|
||||
"number",
|
||||
"select",
|
||||
"sensor",
|
||||
"switch",
|
||||
"vacuum",
|
||||
|
|
80
homeassistant/components/demo/select.py
Normal file
80
homeassistant/components/demo/select.py
Normal file
|
@ -0,0 +1,80 @@
|
|||
"""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."""
|
||||
if option not in self.options:
|
||||
raise ValueError(f"Invalid option for {self.entity_id}: {option}")
|
||||
|
||||
self._attr_current_option = option
|
||||
self.async_write_ha_state()
|
9
homeassistant/components/demo/strings.select.json
Normal file
9
homeassistant/components/demo/strings.select.json
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"state": {
|
||||
"demo__speed": {
|
||||
"light_speed": "Light Speed",
|
||||
"ludicrous_speed": "Ludicrous Speed",
|
||||
"ridiculous_speed": "Ridiculous Speed"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"state": {
|
||||
"demo__speed": {
|
||||
"light_speed": "Light Speed",
|
||||
"ludicrous_speed": "Ludicrous Speed",
|
||||
"ridiculous_speed": "Ridiculous Speed"
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue