forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter.py
254 lines (227 loc) · 10 KB
/
adapter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"""Matter to Home Assistant adapter."""
from __future__ import annotations
from typing import TYPE_CHECKING, cast
from chip.clusters import Objects as clusters
from matter_server.client.models.device_types import BridgedNode
from matter_server.common.models import EventType, ServerInfoMessage
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN, ID_TYPE_DEVICE_ID, ID_TYPE_SERIAL, LOGGER
from .discovery import async_discover_entities
from .helpers import get_device_id
if TYPE_CHECKING:
from matter_server.client import MatterClient
from matter_server.client.models.node import MatterEndpoint, MatterNode
def get_clean_name(name: str | None) -> str | None:
"""Strip spaces and null char from the name."""
if name is None:
return name
name = name.replace("\x00", "")
return name.strip() or None
class MatterAdapter:
"""Connect Matter into Home Assistant."""
def __init__(
self,
hass: HomeAssistant,
matter_client: MatterClient,
config_entry: ConfigEntry,
) -> None:
"""Initialize the adapter."""
self.matter_client = matter_client
self.hass = hass
self.config_entry = config_entry
self.platform_handlers: dict[Platform, AddEntitiesCallback] = {}
self.discovered_entities: set[str] = set()
def register_platform_handler(
self, platform: Platform, add_entities: AddEntitiesCallback
) -> None:
"""Register a platform handler."""
self.platform_handlers[platform] = add_entities
async def setup_nodes(self) -> None:
"""Set up all existing nodes and subscribe to new nodes."""
for node in self.matter_client.get_nodes():
self._setup_node(node)
def node_added_callback(event: EventType, node: MatterNode) -> None:
"""Handle node added event."""
self._setup_node(node)
def node_updated_callback(event: EventType, node: MatterNode) -> None:
"""Handle node updated event."""
if not node.available:
return
# We always run the discovery logic again,
# because the firmware version could have been changed or features added.
self._setup_node(node)
def endpoint_added_callback(event: EventType, data: dict[str, int]) -> None:
"""Handle endpoint added event."""
node = self.matter_client.get_node(data["node_id"])
self._setup_endpoint(node.endpoints[data["endpoint_id"]])
def endpoint_removed_callback(event: EventType, data: dict[str, int]) -> None:
"""Handle endpoint removed event."""
server_info = cast(ServerInfoMessage, self.matter_client.server_info)
try:
node = self.matter_client.get_node(data["node_id"])
except KeyError:
return # race condition
device_registry = dr.async_get(self.hass)
endpoint = node.endpoints.get(data["endpoint_id"])
if not endpoint:
return # race condition
node_device_id = get_device_id(
server_info,
node.endpoints[data["endpoint_id"]],
)
identifier = (DOMAIN, f"{ID_TYPE_DEVICE_ID}_{node_device_id}")
if device := device_registry.async_get_device(identifiers={identifier}):
device_registry.async_remove_device(device.id)
def node_removed_callback(event: EventType, node_id: int) -> None:
"""Handle node removed event."""
try:
node = self.matter_client.get_node(node_id)
except KeyError:
return # race condition
for endpoint_id in node.endpoints:
endpoint_removed_callback(
EventType.ENDPOINT_REMOVED,
{"node_id": node_id, "endpoint_id": endpoint_id},
)
self.config_entry.async_on_unload(
self.matter_client.subscribe_events(
callback=endpoint_added_callback, event_filter=EventType.ENDPOINT_ADDED
)
)
self.config_entry.async_on_unload(
self.matter_client.subscribe_events(
callback=endpoint_removed_callback,
event_filter=EventType.ENDPOINT_REMOVED,
)
)
self.config_entry.async_on_unload(
self.matter_client.subscribe_events(
callback=node_removed_callback, event_filter=EventType.NODE_REMOVED
)
)
self.config_entry.async_on_unload(
self.matter_client.subscribe_events(
callback=node_added_callback, event_filter=EventType.NODE_ADDED
)
)
self.config_entry.async_on_unload(
self.matter_client.subscribe_events(
callback=node_updated_callback, event_filter=EventType.NODE_UPDATED
)
)
def _setup_node(self, node: MatterNode) -> None:
"""Set up an node."""
LOGGER.debug("Setting up entities for node %s", node.node_id)
try:
for endpoint in node.endpoints.values():
# Node endpoints are translated into HA devices
self._setup_endpoint(endpoint)
except Exception as err: # noqa: BLE001
# We don't want to crash the whole setup when a single node fails to setup
# for whatever reason, so we catch all exceptions here.
LOGGER.exception(
"Error setting up node %s: %s",
node.node_id,
err,
)
def _create_device_registry(
self,
endpoint: MatterEndpoint,
) -> None:
"""Create a device registry entry for a MatterNode."""
server_info = cast(ServerInfoMessage, self.matter_client.server_info)
basic_info = endpoint.device_info
# use (first) DeviceType of the endpoint as fallback product name
device_type = next(
(
x
for x in endpoint.device_types
if x.device_type != BridgedNode.device_type
),
None,
)
name = (
get_clean_name(basic_info.nodeLabel)
or get_clean_name(basic_info.productLabel)
or get_clean_name(basic_info.productName)
or (device_type.__name__ if device_type else None)
)
# handle bridged devices
bridge_device_id = None
if endpoint.is_bridged_device and endpoint.node.endpoints[0] != endpoint:
bridge_device_id = get_device_id(
server_info,
endpoint.node.endpoints[0],
)
bridge_device_id = f"{ID_TYPE_DEVICE_ID}_{bridge_device_id}"
node_device_id = get_device_id(
server_info,
endpoint,
)
identifiers = {(DOMAIN, f"{ID_TYPE_DEVICE_ID}_{node_device_id}")}
serial_number: str | None = None
# if available, we also add the serialnumber as identifier
if (
basic_info_serial_number := basic_info.serialNumber
) and "test" not in basic_info_serial_number.lower():
# prefix identifier with 'serial_' to be able to filter it
identifiers.add((DOMAIN, f"{ID_TYPE_SERIAL}_{basic_info_serial_number}"))
serial_number = basic_info_serial_number
# Model name is the human readable name of the model/product name
model_name = (
# productLabel is optional but preferred (e.g. Hue Bloom)
get_clean_name(basic_info.productLabel)
# alternative is the productName (e.g. LCT001)
or get_clean_name(basic_info.productName)
# if no product name, use the device type name
or device_type.__name__
if device_type
else None
)
# Model ID is the non-human readable product ID
# we prefer the matter product ID so we can look it up in Matter DCL
if isinstance(basic_info, clusters.BridgedDeviceBasicInformation):
# On bridged devices, the productID is not available
model_id = None
else:
model_id = str(product_id) if (product_id := basic_info.productID) else None
dr.async_get(self.hass).async_get_or_create(
name=name,
config_entry_id=self.config_entry.entry_id,
identifiers=identifiers,
hw_version=basic_info.hardwareVersionString,
sw_version=basic_info.softwareVersionString,
manufacturer=basic_info.vendorName or endpoint.node.device_info.vendorName,
model=model_name,
model_id=model_id,
serial_number=serial_number,
via_device=(DOMAIN, bridge_device_id) if bridge_device_id else None,
)
def _setup_endpoint(self, endpoint: MatterEndpoint) -> None:
"""Set up a MatterEndpoint as HA Device."""
# pre-create device registry entry
self._create_device_registry(endpoint)
# run platform discovery from device type instances
for entity_info in async_discover_entities(endpoint):
discovery_key = (
f"{entity_info.platform}_{endpoint.node.node_id}_{endpoint.endpoint_id}_"
f"{entity_info.primary_attribute.cluster_id}_"
f"{entity_info.primary_attribute.attribute_id}_"
f"{entity_info.entity_description.key}"
)
if discovery_key in self.discovered_entities:
continue
LOGGER.debug(
"Creating %s entity for %s",
entity_info.platform,
entity_info.primary_attribute,
)
self.discovered_entities.add(discovery_key)
new_entity = entity_info.entity_class(
self.matter_client, endpoint, entity_info
)
self.platform_handlers[entity_info.platform]([new_entity])