Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZwaveJS: Handle S2 inclusion via Inclusion Controller #132073

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions homeassistant/components/zwave_js/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ def async_register_api(hass: HomeAssistant) -> None:
websocket_api.async_register_command(hass, websocket_node_metadata)
websocket_api.async_register_command(hass, websocket_node_alerts)
websocket_api.async_register_command(hass, websocket_add_node)
websocket_api.async_register_command(hass, websocket_subscribe_s2_inclusion)
websocket_api.async_register_command(hass, websocket_grant_security_classes)
websocket_api.async_register_command(hass, websocket_validate_dsk_and_enter_pin)
websocket_api.async_register_command(hass, websocket_provision_smart_start_node)
Expand Down Expand Up @@ -839,6 +840,40 @@ def device_registered(device: dr.DeviceEntry) -> None:
)


@websocket_api.require_admin
@websocket_api.websocket_command(
{
vol.Required(TYPE): "zwave_js/subscribe_s2_inclusion",
vol.Required(ENTRY_ID): str,
}
)
@websocket_api.async_response
@async_handle_failed_command
@async_get_entry
async def websocket_subscribe_s2_inclusion(
hass: HomeAssistant,
connection: ActiveConnection,
msg: dict[str, Any],
entry: ConfigEntry,
client: Client,
driver: Driver,
) -> None:
"""Subscribe to S2 inclusion initiated by the controller."""

@callback
def forward_dsk(event: dict) -> None:
connection.send_message(
websocket_api.event_message(
msg[ID], {"event": event["event"], "dsk": event["dsk"]}
)
)

unsub = driver.controller.on("validate dsk and enter pin", forward_dsk)
connection.subscriptions[msg["id"]] = unsub
msg[DATA_UNSUBSCRIBE] = [unsub]
connection.send_result(msg[ID])


@websocket_api.require_admin
@websocket_api.websocket_command(
{
Expand Down
53 changes: 53 additions & 0 deletions tests/components/zwave_js/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5195,3 +5195,56 @@ async def test_get_integration_settings(
assert msg["result"] == {
CONF_INSTALLER_MODE: installer_mode,
}


async def test_subscribe_s2_inclusion(
hass: HomeAssistant, integration, client, hass_ws_client: WebSocketGenerator
) -> None:
"""Test the subscribe_s2_inclusion websocket command."""
entry = integration
ws_client = await hass_ws_client(hass)

await ws_client.send_json(
{
ID: 1,
TYPE: "zwave_js/subscribe_s2_inclusion",
ENTRY_ID: entry.entry_id,
}
)

msg = await ws_client.receive_json()
assert msg["success"]
assert msg["result"] is None

# Test receiving DSK request event
event = Event(
type="validate dsk and enter pin",
data={
"source": "controller",
"event": "validate dsk and enter pin",
"dsk": "test_dsk",
},
)
client.driver.receive_event(event)

msg = await ws_client.receive_json()
assert msg["event"] == {
"event": "validate dsk and enter pin",
"dsk": "test_dsk",
}

# Test sending command with not loaded entry fails
await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()

await ws_client.send_json(
{
ID: 2,
TYPE: "zwave_js/subscribe_s2_inclusion",
ENTRY_ID: entry.entry_id,
}
)
msg = await ws_client.receive_json()

assert not msg["success"]
assert msg["error"]["code"] == ERR_NOT_LOADED
Loading