1 """Support for buttons which integrates with other components."""
3 from __future__
import annotations
8 import voluptuous
as vol
26 from .const
import CONF_PRESS, DOMAIN
27 from .template_entity
import (
28 TEMPLATE_ENTITY_AVAILABILITY_SCHEMA,
29 TEMPLATE_ENTITY_ICON_SCHEMA,
33 _LOGGER = logging.getLogger(__name__)
35 DEFAULT_NAME =
"Template Button"
36 DEFAULT_OPTIMISTIC =
False
41 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.template,
42 vol.Required(CONF_PRESS): cv.SCRIPT_SCHEMA,
43 vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
44 vol.Optional(CONF_UNIQUE_ID): cv.string,
47 .extend(TEMPLATE_ENTITY_AVAILABILITY_SCHEMA.schema)
48 .extend(TEMPLATE_ENTITY_ICON_SCHEMA.schema)
51 CONFIG_BUTTON_SCHEMA = vol.Schema(
53 vol.Optional(CONF_NAME): cv.template,
54 vol.Optional(CONF_PRESS): cv.SCRIPT_SCHEMA,
55 vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
56 vol.Optional(CONF_DEVICE_ID): selector.DeviceSelector(),
62 hass: HomeAssistant, definitions: list[dict[str, Any]], unique_id_prefix: str |
None
63 ) -> list[TemplateButtonEntity]:
64 """Create the Template button."""
66 for definition
in definitions:
67 unique_id = definition.get(CONF_UNIQUE_ID)
68 if unique_id
and unique_id_prefix:
69 unique_id = f
"{unique_id_prefix}-{unique_id}"
77 async_add_entities: AddEntitiesCallback,
78 discovery_info: DiscoveryInfoType |
None =
None,
80 """Set up the template button."""
81 if not discovery_info
or "coordinator" in discovery_info:
83 "The template button platform doesn't support trigger entities"
88 hass, discovery_info[
"entities"], discovery_info[
"unique_id"]
95 config_entry: ConfigEntry,
96 async_add_entities: AddEntitiesCallback,
98 """Initialize config entry."""
99 _options =
dict(config_entry.options)
100 _options.pop(
"template_type")
108 """Representation of a template button."""
110 _attr_should_poll =
False
116 unique_id: str |
None,
118 """Initialize the button."""
119 super().
__init__(hass, config=config, unique_id=unique_id)
123 if config.get(CONF_PRESS,
None)
is not None
130 config.get(CONF_DEVICE_ID),
134 """Press the button."""
None async_run_script(self, Script script, *_VarsType|None run_variables=None, Context|None context=None)
dr.DeviceInfo|None async_device_info_to_link_from_device_id(HomeAssistant hass, str|None device_id)