1
mirror of https://github.com/home-assistant/core synced 2024-08-02 23:40:32 +02:00
ha-core/homeassistant/components/demo/select.py
epenet 29d58e427b
Add setup type hints (part 2) (#63960)
* Add setup type hints to demo

* Add setup type hints to group

Co-authored-by: epenet <epenet@users.noreply.github.com>
2022-01-12 09:28:14 +01:00

79 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 import DeviceInfo
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,
) -> 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 = DeviceInfo(
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()