1
mirror of https://github.com/home-assistant/core synced 2024-08-02 23:40:32 +02:00
ha-core/homeassistant/components/demo/button.py
2022-01-13 08:42:50 +01:00

67 lines
1.9 KiB
Python

"""Demo platform that offers a fake button entity."""
from __future__ import annotations
from homeassistant.components import persistent_notification
from homeassistant.components.button import ButtonEntity
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,
) -> None:
"""Set up the demo Button entity."""
async_add_entities(
[
DemoButton(
unique_id="push",
name="Push",
icon="mdi:gesture-tap-button",
),
]
)
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 DemoButton(ButtonEntity):
"""Representation of a demo button entity."""
_attr_should_poll = False
def __init__(
self,
unique_id: str,
name: str,
icon: str,
) -> None:
"""Initialize the Demo button entity."""
self._attr_unique_id = unique_id
self._attr_name = name or DEVICE_DEFAULT_NAME
self._attr_icon = icon
self._attr_device_info = {
"identifiers": {(DOMAIN, unique_id)},
"name": name,
}
async def async_press(self) -> None:
"""Send out a persistent notification."""
persistent_notification.async_create(
self.hass, "Button pressed", title="Button"
)