Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Matter switches."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from chip.clusters import Objects as clusters
8 from matter_server.client.models import device_types
9 
11  SwitchDeviceClass,
12  SwitchEntity,
13  SwitchEntityDescription,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import Platform
17 from homeassistant.core import HomeAssistant, callback
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from .entity import MatterEntity
21 from .helpers import get_matter
22 from .models import MatterDiscoverySchema
23 
24 
26  hass: HomeAssistant,
27  config_entry: ConfigEntry,
28  async_add_entities: AddEntitiesCallback,
29 ) -> None:
30  """Set up Matter switches from Config Entry."""
31  matter = get_matter(hass)
32  matter.register_platform_handler(Platform.SWITCH, async_add_entities)
33 
34 
36  """Representation of a Matter switch."""
37 
38  _platform_translation_key = "switch"
39 
40  async def async_turn_on(self, **kwargs: Any) -> None:
41  """Turn switch on."""
42  await self.matter_clientmatter_client.send_device_command(
43  node_id=self._endpoint_endpoint.node.node_id,
44  endpoint_id=self._endpoint_endpoint.endpoint_id,
45  command=clusters.OnOff.Commands.On(),
46  )
47 
48  async def async_turn_off(self, **kwargs: Any) -> None:
49  """Turn switch off."""
50  await self.matter_clientmatter_client.send_device_command(
51  node_id=self._endpoint_endpoint.node.node_id,
52  endpoint_id=self._endpoint_endpoint.endpoint_id,
53  command=clusters.OnOff.Commands.Off(),
54  )
55 
56  @callback
57  def _update_from_device(self) -> None:
58  """Update from device."""
59  self._attr_is_on_attr_is_on = self.get_matter_attribute_valueget_matter_attribute_value(
60  self._entity_info_entity_info.primary_attribute
61  )
62 
63 
64 # Discovery schema(s) to map Matter Attributes to HA entities
65 DISCOVERY_SCHEMAS = [
67  platform=Platform.SWITCH,
68  entity_description=SwitchEntityDescription(
69  key="MatterPlug",
70  device_class=SwitchDeviceClass.OUTLET,
71  name=None,
72  ),
73  entity_class=MatterSwitch,
74  required_attributes=(clusters.OnOff.Attributes.OnOff,),
75  device_type=(device_types.OnOffPlugInUnit,),
76  ),
78  platform=Platform.SWITCH,
79  entity_description=SwitchEntityDescription(
80  key="MatterPowerToggle",
81  device_class=SwitchDeviceClass.SWITCH,
82  translation_key="power",
83  ),
84  entity_class=MatterSwitch,
85  required_attributes=(clusters.OnOff.Attributes.OnOff,),
86  device_type=(
87  device_types.AirPurifier,
88  device_types.BasicVideoPlayer,
89  device_types.CastingVideoPlayer,
90  device_types.CookSurface,
91  device_types.Cooktop,
92  device_types.Dishwasher,
93  device_types.ExtractorHood,
94  device_types.HeatingCoolingUnit,
95  device_types.LaundryDryer,
96  device_types.LaundryWasher,
97  device_types.Oven,
98  device_types.Pump,
99  device_types.PumpController,
100  device_types.Refrigerator,
101  device_types.RoboticVacuumCleaner,
102  device_types.RoomAirConditioner,
103  device_types.Speaker,
104  ),
105  ),
107  platform=Platform.SWITCH,
108  entity_description=SwitchEntityDescription(
109  key="MatterSwitch",
110  device_class=SwitchDeviceClass.OUTLET,
111  name=None,
112  ),
113  entity_class=MatterSwitch,
114  required_attributes=(clusters.OnOff.Attributes.OnOff,),
115  not_device_type=(
116  device_types.ColorTemperatureLight,
117  device_types.DimmableLight,
118  device_types.ExtendedColorLight,
119  device_types.DimmerSwitch,
120  device_types.ColorDimmerSwitch,
121  device_types.OnOffLight,
122  device_types.AirPurifier,
123  device_types.BasicVideoPlayer,
124  device_types.CastingVideoPlayer,
125  device_types.CookSurface,
126  device_types.Cooktop,
127  device_types.Dishwasher,
128  device_types.ExtractorHood,
129  device_types.Fan,
130  device_types.HeatingCoolingUnit,
131  device_types.LaundryDryer,
132  device_types.LaundryWasher,
133  device_types.Oven,
134  device_types.Pump,
135  device_types.PumpController,
136  device_types.Refrigerator,
137  device_types.RoboticVacuumCleaner,
138  device_types.RoomAirConditioner,
139  device_types.Speaker,
140  ),
141  ),
142 ]
Any get_matter_attribute_value(self, type[ClusterAttributeDescriptor] attribute, bool null_as_none=True)
Definition: entity.py:206
MatterAdapter get_matter(HomeAssistant hass)
Definition: helpers.py:35
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:29