Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Matter Button platform."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import TYPE_CHECKING, Any
8 
9 from chip.clusters import Objects as clusters
10 
12  ButtonDeviceClass,
13  ButtonEntity,
14  ButtonEntityDescription,
15 )
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import EntityCategory, Platform
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from .entity import MatterEntity, MatterEntityDescription
22 from .helpers import get_matter
23 from .models import MatterDiscoverySchema
24 
25 
27  hass: HomeAssistant,
28  config_entry: ConfigEntry,
29  async_add_entities: AddEntitiesCallback,
30 ) -> None:
31  """Set up Matter Button platform."""
32  matter = get_matter(hass)
33  matter.register_platform_handler(Platform.BUTTON, async_add_entities)
34 
35 
36 @dataclass(frozen=True)
38  """Describe Matter Button entities."""
39 
40  command: Callable[[], Any] | None = None
41 
42 
44  """Representation of a Matter Button entity."""
45 
46  entity_description: MatterButtonEntityDescription
47 
48  async def async_press(self) -> None:
49  """Handle the button press leveraging a Matter command."""
50  if TYPE_CHECKING:
51  assert self.entity_descriptionentity_description.command is not None
52  await self.matter_clientmatter_client.send_device_command(
53  node_id=self._endpoint_endpoint.node.node_id,
54  endpoint_id=self._endpoint_endpoint.endpoint_id,
55  command=self.entity_descriptionentity_description.command(),
56  )
57 
58 
59 # Discovery schema(s) to map Matter Attributes to HA entities
60 DISCOVERY_SCHEMAS = [
62  platform=Platform.BUTTON,
63  entity_description=MatterButtonEntityDescription(
64  key="IdentifyButton",
65  entity_category=EntityCategory.CONFIG,
66  device_class=ButtonDeviceClass.IDENTIFY,
67  command=lambda: clusters.Identify.Commands.Identify(identifyTime=15),
68  ),
69  entity_class=MatterCommandButton,
70  required_attributes=(clusters.Identify.Attributes.AcceptedCommandList,),
71  value_contains=clusters.Identify.Commands.Identify.command_id,
72  allow_multi=True,
73  ),
75  platform=Platform.BUTTON,
76  entity_description=MatterButtonEntityDescription(
77  key="OperationalStatePauseButton",
78  translation_key="pause",
79  command=clusters.OperationalState.Commands.Pause,
80  ),
81  entity_class=MatterCommandButton,
82  required_attributes=(clusters.OperationalState.Attributes.AcceptedCommandList,),
83  value_contains=clusters.OperationalState.Commands.Pause.command_id,
84  allow_multi=True,
85  ),
87  platform=Platform.BUTTON,
88  entity_description=MatterButtonEntityDescription(
89  key="OperationalStateResumeButton",
90  translation_key="resume",
91  command=clusters.OperationalState.Commands.Resume,
92  ),
93  entity_class=MatterCommandButton,
94  required_attributes=(clusters.OperationalState.Attributes.AcceptedCommandList,),
95  value_contains=clusters.OperationalState.Commands.Resume.command_id,
96  allow_multi=True,
97  ),
99  platform=Platform.BUTTON,
100  entity_description=MatterButtonEntityDescription(
101  key="OperationalStateStartButton",
102  translation_key="start",
103  command=clusters.OperationalState.Commands.Start,
104  ),
105  entity_class=MatterCommandButton,
106  required_attributes=(clusters.OperationalState.Attributes.AcceptedCommandList,),
107  value_contains=clusters.OperationalState.Commands.Start.command_id,
108  allow_multi=True,
109  ),
111  platform=Platform.BUTTON,
112  entity_description=MatterButtonEntityDescription(
113  key="OperationalStateStopButton",
114  translation_key="stop",
115  command=clusters.OperationalState.Commands.Stop,
116  ),
117  entity_class=MatterCommandButton,
118  required_attributes=(clusters.OperationalState.Attributes.AcceptedCommandList,),
119  value_contains=clusters.OperationalState.Commands.Stop.command_id,
120  allow_multi=True,
121  ),
123  platform=Platform.BUTTON,
124  entity_description=MatterButtonEntityDescription(
125  key="HepaFilterMonitoringResetButton",
126  translation_key="reset_filter_condition",
127  command=clusters.HepaFilterMonitoring.Commands.ResetCondition,
128  ),
129  entity_class=MatterCommandButton,
130  required_attributes=(
131  clusters.HepaFilterMonitoring.Attributes.AcceptedCommandList,
132  ),
133  value_contains=clusters.HepaFilterMonitoring.Commands.ResetCondition.command_id,
134  allow_multi=True,
135  ),
137  platform=Platform.BUTTON,
138  entity_description=MatterButtonEntityDescription(
139  key="ActivatedCarbonFilterMonitoringResetButton",
140  translation_key="reset_filter_condition",
141  command=clusters.ActivatedCarbonFilterMonitoring.Commands.ResetCondition,
142  ),
143  entity_class=MatterCommandButton,
144  required_attributes=(
145  clusters.ActivatedCarbonFilterMonitoring.Attributes.AcceptedCommandList,
146  ),
147  value_contains=clusters.ActivatedCarbonFilterMonitoring.Commands.ResetCondition.command_id,
148  allow_multi=True,
149  ),
150 ]
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: button.py:30
MatterAdapter get_matter(HomeAssistant hass)
Definition: helpers.py:35