Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Platform allowing several button entities to be grouped into one single button."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import voluptuous as vol
8 
10  DOMAIN as BUTTON_DOMAIN,
11  PLATFORM_SCHEMA as BUTTON_PLATFORM_SCHEMA,
12  SERVICE_PRESS,
13  ButtonEntity,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import (
17  ATTR_ENTITY_ID,
18  CONF_ENTITIES,
19  CONF_NAME,
20  CONF_UNIQUE_ID,
21  STATE_UNAVAILABLE,
22 )
23 from homeassistant.core import HomeAssistant, callback
24 from homeassistant.helpers import config_validation as cv, entity_registry as er
25 from homeassistant.helpers.entity_platform import AddEntitiesCallback
26 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
27 
28 from .entity import GroupEntity
29 
30 DEFAULT_NAME = "Button group"
31 
32 # No limit on parallel updates to enable a group calling another group
33 PARALLEL_UPDATES = 0
34 
35 PLATFORM_SCHEMA = BUTTON_PLATFORM_SCHEMA.extend(
36  {
37  vol.Required(CONF_ENTITIES): cv.entities_domain(BUTTON_DOMAIN),
38  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
39  vol.Optional(CONF_UNIQUE_ID): cv.string,
40  }
41 )
42 
43 
45  _: HomeAssistant,
46  config: ConfigType,
47  async_add_entities: AddEntitiesCallback,
48  __: DiscoveryInfoType | None = None,
49 ) -> None:
50  """Set up the button group platform."""
52  [
54  config.get(CONF_UNIQUE_ID),
55  config[CONF_NAME],
56  config[CONF_ENTITIES],
57  )
58  ]
59  )
60 
61 
63  hass: HomeAssistant,
64  config_entry: ConfigEntry,
65  async_add_entities: AddEntitiesCallback,
66 ) -> None:
67  """Initialize button group config entry."""
68  registry = er.async_get(hass)
69  entities = er.async_validate_entity_ids(
70  registry, config_entry.options[CONF_ENTITIES]
71  )
73  [
75  config_entry.entry_id,
76  config_entry.title,
77  entities,
78  )
79  ]
80  )
81 
82 
83 @callback
85  hass: HomeAssistant, name: str, validated_config: dict[str, Any]
86 ) -> ButtonGroup:
87  """Create a preview button."""
88  return ButtonGroup(
89  None,
90  name,
91  validated_config[CONF_ENTITIES],
92  )
93 
94 
96  """Representation of an button group."""
97 
98  _attr_available = False
99  _attr_should_poll = False
100 
101  def __init__(
102  self,
103  unique_id: str | None,
104  name: str,
105  entity_ids: list[str],
106  ) -> None:
107  """Initialize a button group."""
108  self._entity_ids_entity_ids = entity_ids
109  self._attr_name_attr_name = name
110  self._attr_extra_state_attributes_attr_extra_state_attributes = {ATTR_ENTITY_ID: entity_ids}
111  self._attr_unique_id_attr_unique_id = unique_id
112 
113  async def async_press(self) -> None:
114  """Forward the press to all buttons in the group."""
115  await self.hasshass.services.async_call(
116  BUTTON_DOMAIN,
117  SERVICE_PRESS,
118  {ATTR_ENTITY_ID: self._entity_ids_entity_ids},
119  blocking=True,
120  context=self._context_context,
121  )
122 
123  @callback
124  def async_update_group_state(self) -> None:
125  """Query all members and determine the button group state."""
126  # Set group as unavailable if all members are unavailable or missing
127  self._attr_available_attr_available_attr_available = any(
128  state.state != STATE_UNAVAILABLE
129  for entity_id in self._entity_ids_entity_ids
130  if (state := self.hasshass.states.get(entity_id)) is not None
131  )
None __init__(self, str|None unique_id, str name, list[str] entity_ids)
Definition: button.py:106
ButtonGroup async_create_preview_button(HomeAssistant hass, str name, dict[str, Any] validated_config)
Definition: button.py:86
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: button.py:66
None async_setup_platform(HomeAssistant _, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None __=None)
Definition: button.py:49