Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Representation of ISY/IoX buttons."""
2 
3 from __future__ import annotations
4 
5 from pyisy import ISY
6 from pyisy.constants import (
7  ATTR_ACTION,
8  NC_NODE_ENABLED,
9  PROTO_INSTEON,
10  TAG_ADDRESS,
11  TAG_ENABLED,
12 )
13 from pyisy.helpers import EventListener, NodeProperty
14 from pyisy.networking import NetworkCommand
15 from pyisy.nodes import Node
16 
17 from homeassistant.components.button import ButtonEntity
18 from homeassistant.config_entries import ConfigEntry
19 from homeassistant.const import EntityCategory, Platform
20 from homeassistant.core import HomeAssistant, callback
21 from homeassistant.helpers.device_registry import DeviceInfo
22 from homeassistant.helpers.entity_platform import AddEntitiesCallback
23 
24 from .const import CONF_NETWORK, DOMAIN
25 from .models import IsyData
26 
27 
29  hass: HomeAssistant,
30  config_entry: ConfigEntry,
31  async_add_entities: AddEntitiesCallback,
32 ) -> None:
33  """Set up ISY/IoX button from config entry."""
34  isy_data: IsyData = hass.data[DOMAIN][config_entry.entry_id]
35  isy: ISY = isy_data.root
36  device_info = isy_data.devices
37  entities: list[
38  ISYNodeQueryButtonEntity
39  | ISYNodeBeepButtonEntity
40  | ISYNetworkResourceButtonEntity
41  ] = [
43  node=node,
44  name=node.name,
45  unique_id=isy_data.uid_base(node),
46  device_info=device_info[CONF_NETWORK],
47  )
48  for node in isy_data.net_resources
49  ]
50 
51  for node in isy_data.root_nodes[Platform.BUTTON]:
52  entities.append(
54  node=node,
55  name="Query",
56  unique_id=f"{isy_data.uid_base(node)}_query",
57  entity_category=EntityCategory.DIAGNOSTIC,
58  device_info=device_info[node.address],
59  )
60  )
61  if node.protocol == PROTO_INSTEON:
62  entities.append(
64  node=node,
65  name="Beep",
66  unique_id=f"{isy_data.uid_base(node)}_beep",
67  entity_category=EntityCategory.DIAGNOSTIC,
68  device_info=device_info[node.address],
69  )
70  )
71 
72  # Add entity to query full system
73  entities.append(
75  node=isy,
76  name="Query",
77  unique_id=f"{isy.uuid}_query",
78  device_info=DeviceInfo(identifiers={(DOMAIN, isy.uuid)}),
79  entity_category=EntityCategory.DIAGNOSTIC,
80  )
81  )
82 
83  async_add_entities(entities)
84 
85 
87  """Representation of an ISY/IoX device button entity."""
88 
89  _attr_should_poll = False
90  _attr_has_entity_name = True
91 
92  def __init__(
93  self,
94  node: Node | ISY | NetworkCommand,
95  name: str,
96  unique_id: str,
97  device_info: DeviceInfo,
98  entity_category: EntityCategory | None = None,
99  ) -> None:
100  """Initialize a query ISY device button entity."""
101  self._node_node = node
102 
103  # Entity class attributes
104  self._attr_name_attr_name = name
105  self._attr_entity_category_attr_entity_category = entity_category
106  self._attr_unique_id_attr_unique_id = unique_id
107  self._attr_device_info_attr_device_info = device_info
108  self._node_enabled_node_enabled = getattr(node, TAG_ENABLED, True)
109  self._availability_handler_availability_handler: EventListener | None = None
110 
111  @property
112  def available(self) -> bool:
113  """Return entity availability."""
114  return self._node_enabled_node_enabled
115 
116  async def async_added_to_hass(self) -> None:
117  """Subscribe to the node change events."""
118  # No status for NetworkResources or ISY Query buttons
119  if not hasattr(self._node_node, "status_events") or not hasattr(self._node_node, "isy"):
120  return
121  self._availability_handler_availability_handler = self._node_node.isy.nodes.status_events.subscribe(
122  self.async_on_updateasync_on_update,
123  event_filter={
124  TAG_ADDRESS: self._node_node.address,
125  ATTR_ACTION: NC_NODE_ENABLED,
126  },
127  key=self.unique_idunique_id,
128  )
129 
130  @callback
131  def async_on_update(self, event: NodeProperty, key: str) -> None:
132  """Handle the update event from the ISY Node."""
133  # Watch for node availability/enabled changes only
134  self._node_enabled_node_enabled = getattr(self._node_node, TAG_ENABLED, True)
135  self.async_write_ha_stateasync_write_ha_state()
136 
137 
139  """Representation of a device query button entity."""
140 
141  async def async_press(self) -> None:
142  """Press the button."""
143  await self._node_node.query()
144 
145 
147  """Representation of a device beep button entity."""
148 
149  async def async_press(self) -> None:
150  """Press the button."""
151  await self._node_node.beep()
152 
153 
155  """Representation of an ISY/IoX Network Resource button entity."""
156 
157  _attr_has_entity_name = False
158 
159  async def async_press(self) -> None:
160  """Press the button."""
161  await self._node_node.run()
None __init__(self, Node|ISY|NetworkCommand node, str name, str unique_id, DeviceInfo device_info, EntityCategory|None entity_category=None)
Definition: button.py:99
None async_on_update(self, NodeProperty event, str key)
Definition: button.py:131
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: button.py:32
int run(RuntimeConfig runtime_config)
Definition: runner.py:146