@@ -167,12 +167,45 @@ async def set_thread(call: ServiceCall) -> None:
167
167
vol .Schema ({"thread_operation_dataset" : str }),
168
168
)
169
169
170
+ @callback
171
+ def _node_id_from_ha_device_id (ha_device_id : str ) -> str | None :
172
+ """Get node id from ha device id."""
173
+ dev_reg = dr .async_get (hass )
174
+ device = dev_reg .async_get (ha_device_id )
175
+
176
+ if device is None :
177
+ return None
178
+
179
+ matter_iden = [iden for iden in device .identifiers if iden [0 ] == DOMAIN ]
180
+
181
+ if not matter_iden :
182
+ return None
183
+
184
+ unique_id = matter_iden [0 ][1 ]
185
+
186
+ matter : Matter = list (hass .data [DOMAIN ].values ())[0 ]
187
+
188
+ # This could be more efficient
189
+ for node in matter .get_nodes ():
190
+ if node .unique_id == unique_id :
191
+ return node .node_id
192
+
193
+ return None
194
+
170
195
async def open_commissioning_window (call : ServiceCall ) -> None :
171
196
"""Open commissioning window on specific node."""
197
+ node_id = _node_id_from_ha_device_id (call .data ["device_id" ])
198
+
199
+ if node_id is None :
200
+ raise HomeAssistantError ("This is not a Matter device" )
201
+
172
202
matter : Matter = list (hass .data [DOMAIN ].values ())[0 ]
203
+
204
+ # We are sending device ID .
205
+
173
206
try :
174
207
await matter .client .driver .device_controller .open_commissioning_window (
175
- call . data [ " node_id" ]
208
+ node_id
176
209
)
177
210
except FailedCommand as err :
178
211
raise HomeAssistantError (str (err )) from err
@@ -182,5 +215,5 @@ async def open_commissioning_window(call: ServiceCall) -> None:
182
215
DOMAIN ,
183
216
"open_commissioning_window" ,
184
217
open_commissioning_window ,
185
- vol .Schema ({"node_id " : int }),
218
+ vol .Schema ({"device_id " : str }),
186
219
)
0 commit comments