Skip to content

Commit 25c94fa

Browse files
authored
Use device picker for commission window service call (#5)
1 parent 66da56c commit 25c94fa

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

custom_components/matter_experimental/__init__.py

+35-2
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,45 @@ async def set_thread(call: ServiceCall) -> None:
167167
vol.Schema({"thread_operation_dataset": str}),
168168
)
169169

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+
170195
async def open_commissioning_window(call: ServiceCall) -> None:
171196
"""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+
172202
matter: Matter = list(hass.data[DOMAIN].values())[0]
203+
204+
# We are sending device ID .
205+
173206
try:
174207
await matter.client.driver.device_controller.open_commissioning_window(
175-
call.data["node_id"]
208+
node_id
176209
)
177210
except FailedCommand as err:
178211
raise HomeAssistantError(str(err)) from err
@@ -182,5 +215,5 @@ async def open_commissioning_window(call: ServiceCall) -> None:
182215
DOMAIN,
183216
"open_commissioning_window",
184217
open_commissioning_window,
185-
vol.Schema({"node_id": int}),
218+
vol.Schema({"device_id": str}),
186219
)

custom_components/matter_experimental/services.yaml

+5-7
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,11 @@ set_thread:
3737
open_commissioning_window:
3838
name: Open Commissioning Window
3939
description: >
40-
Open Commissioning Window of a Matter node for 60s.
40+
Open Commissioning Window of a Matter device for 60s.
4141
fields:
42-
node_id:
43-
name: Matter Node ID
42+
device_id:
43+
name: Device
4444
required: true
4545
selector:
46-
number:
47-
min: 0
48-
max: 18446744004990074879
49-
mode: box
46+
device:
47+
integration: matter_experimental

0 commit comments

Comments
 (0)