1 """Support to keep track of user controlled buttons which can be used in automations."""
3 from __future__
import annotations
6 from typing
import Self, cast
8 import voluptuous
as vol
27 DOMAIN =
"input_button"
29 _LOGGER = logging.getLogger(__name__)
31 STORAGE_FIELDS: VolDictType = {
32 vol.Required(CONF_NAME): vol.All(str, vol.Length(min=1)),
33 vol.Optional(CONF_ICON): cv.icon,
36 CONFIG_SCHEMA = vol.Schema(
38 DOMAIN: cv.schema_with_slug_keys(
41 vol.Optional(CONF_NAME): cv.string,
42 vol.Optional(CONF_ICON): cv.icon,
48 extra=vol.ALLOW_EXTRA,
51 RELOAD_SERVICE_SCHEMA = vol.Schema({})
57 """Input button collection stored in storage."""
59 CREATE_UPDATE_SCHEMA = vol.Schema(STORAGE_FIELDS)
62 """Validate the config is valid."""
67 """Suggest an ID based on the config."""
68 return cast(str, info[CONF_NAME])
70 async
def _update_data(self, item: dict, update_data: dict) -> dict:
71 """Return a new updated data object."""
73 return {CONF_ID: item[CONF_ID]} | update_data
76 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
77 """Set up an input button."""
78 component = EntityComponent[InputButton](_LOGGER, DOMAIN, hass)
80 id_manager = collection.IDManager()
82 yaml_collection = collection.YamlCollection(
83 logging.getLogger(f
"{__name__}.yaml_collection"), id_manager
85 collection.sync_entity_lifecycle(
86 hass, DOMAIN, DOMAIN, component, yaml_collection, InputButton
90 Store(hass, STORAGE_VERSION, STORAGE_KEY),
93 collection.sync_entity_lifecycle(
94 hass, DOMAIN, DOMAIN, component, storage_collection, InputButton
97 await yaml_collection.async_load(
98 [{CONF_ID: id_, **(conf
or {})}
for id_, conf
in config.get(DOMAIN, {}).items()]
100 await storage_collection.async_load()
102 collection.DictStorageCollectionWebsocket(
103 storage_collection, DOMAIN, DOMAIN, STORAGE_FIELDS, STORAGE_FIELDS
106 async
def reload_service_handler(service_call: ServiceCall) ->
None:
107 """Remove all input buttons and load new ones from config."""
108 conf = await component.async_prepare_reload(skip_reset=
True)
111 await yaml_collection.async_load(
113 {CONF_ID: id_, **(conf
or {})}
114 for id_, conf
in conf.get(DOMAIN, {}).items()
122 reload_service_handler,
123 schema=RELOAD_SERVICE_SCHEMA,
126 component.async_register_entity_service(SERVICE_PRESS,
None,
"_async_press_action")
133 """Representation of a button."""
135 _unrecorded_attributes = frozenset({ATTR_EDITABLE})
137 _attr_should_poll =
False
141 """Initialize a button."""
147 """Return entity instance initialized from storage."""
149 button.editable =
True
154 """Return entity instance initialized from yaml."""
156 button.entity_id = f
"{DOMAIN}.{config[CONF_ID]}"
157 button.editable =
False
162 """Return name of the button."""
167 """Return the icon to be used for this entity."""
172 """Return the state attributes of the entity."""
173 return {ATTR_EDITABLE: self.editable}
178 Left empty intentionally.
179 The input button itself doesn't trigger anything.
183 """Handle when the config is updated."""
184 self._config = config
185 self.async_write_ha_state()
web.Response get(self, web.Request request, str config_key)
None async_register_admin_service(HomeAssistant hass, str domain, str service, Callable[[ServiceCall], Awaitable[None]|None] service_func, VolSchemaType schema=vol.Schema({}, extra=vol.PREVENT_EXTRA))