Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Demo platform that offers a fake button entity."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.components import persistent_notification
6 from homeassistant.components.button import ButtonEntity
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.core import HomeAssistant
9 from homeassistant.helpers.device_registry import DeviceInfo
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from . import DOMAIN
13 
14 
16  hass: HomeAssistant,
17  config_entry: ConfigEntry,
18  async_add_entities: AddEntitiesCallback,
19 ) -> None:
20  """Set up the demo button platform."""
22  [
23  DemoButton(
24  unique_id="2_ch_power_strip",
25  device_name=None,
26  device_translation_key="n_ch_power_strip",
27  device_translation_placeholders={"number_of_sockets": "2"},
28  entity_name="Restart",
29  ),
30  ]
31  )
32 
33 
35  """Representation of a demo button entity."""
36 
37  _attr_has_entity_name = True
38  _attr_should_poll = False
39 
40  def __init__(
41  self,
42  unique_id: str,
43  device_name: str | None,
44  device_translation_key: str | None,
45  device_translation_placeholders: dict[str, str] | None,
46  entity_name: str | None,
47  ) -> None:
48  """Initialize the Demo button entity."""
49  self._attr_unique_id_attr_unique_id = unique_id
50  self._attr_device_info_attr_device_info = DeviceInfo(
51  identifiers={(DOMAIN, unique_id)},
52  name=device_name,
53  translation_key=device_translation_key,
54  translation_placeholders=device_translation_placeholders,
55  )
56  self._attr_name_attr_name = entity_name
57 
58  async def async_press(self) -> None:
59  """Send out a persistent notification."""
60  persistent_notification.async_create(
61  self.hasshass, "Button pressed", title="Button"
62  )
63  self.hasshass.bus.async_fire("demo_button_pressed")
None __init__(self, str unique_id, str|None device_name, str|None device_translation_key, dict[str, str]|None device_translation_placeholders, str|None entity_name)
Definition: button.py:47
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: button.py:19