Add zwave_js/subscribe_node_status WS API cmd (#73249)

* Add zwave_js/subscribe_node_status WS API cmd

* add ready to event
This commit is contained in:
Raman Gupta 2022-06-19 10:09:26 -04:00 committed by GitHub
parent 45142558ef
commit 7714183118
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 9 deletions

View File

@ -378,6 +378,7 @@ def node_status(node: Node) -> dict[str, Any]:
def async_register_api(hass: HomeAssistant) -> None:
"""Register all of our api endpoints."""
websocket_api.async_register_command(hass, websocket_network_status)
websocket_api.async_register_command(hass, websocket_subscribe_node_status)
websocket_api.async_register_command(hass, websocket_node_status)
websocket_api.async_register_command(hass, websocket_node_metadata)
websocket_api.async_register_command(hass, websocket_node_comments)
@ -422,7 +423,6 @@ def async_register_api(hass: HomeAssistant) -> None:
hass, websocket_subscribe_controller_statistics
)
websocket_api.async_register_command(hass, websocket_subscribe_node_statistics)
websocket_api.async_register_command(hass, websocket_node_ready)
hass.http.register_view(FirmwareUploadView())
@ -497,25 +497,28 @@ async def websocket_network_status(
@websocket_api.websocket_command(
{
vol.Required(TYPE): "zwave_js/node_ready",
vol.Required(TYPE): "zwave_js/subscribe_node_status",
vol.Required(DEVICE_ID): str,
}
)
@websocket_api.async_response
@async_get_node
async def websocket_node_ready(
async def websocket_subscribe_node_status(
hass: HomeAssistant,
connection: ActiveConnection,
msg: dict,
node: Node,
) -> None:
"""Subscribe to the node ready event of a Z-Wave JS node."""
"""Subscribe to node status update events of a Z-Wave JS node."""
@callback
def forward_event(event: dict) -> None:
"""Forward the event."""
connection.send_message(
websocket_api.event_message(msg[ID], {"event": event["event"]})
websocket_api.event_message(
msg[ID],
{"event": event["event"], "status": node.status, "ready": node.ready},
)
)
@callback
@ -525,7 +528,10 @@ async def websocket_node_ready(
unsub()
connection.subscriptions[msg["id"]] = async_cleanup
msg[DATA_UNSUBSCRIBE] = unsubs = [node.on("ready", forward_event)]
msg[DATA_UNSUBSCRIBE] = unsubs = [
node.on(evt, forward_event)
for evt in ("alive", "dead", "sleep", "wake up", "ready")
]
connection.send_result(msg[ID])

View File

@ -198,14 +198,14 @@ async def test_network_status(hass, multisensor_6, integration, hass_ws_client):
assert msg["error"]["code"] == ERR_INVALID_FORMAT
async def test_node_ready(
async def test_subscribe_node_status(
hass,
multisensor_6_state,
client,
integration,
hass_ws_client,
):
"""Test the node ready websocket command."""
"""Test the subscribe node status websocket command."""
entry = integration
ws_client = await hass_ws_client(hass)
node_data = deepcopy(multisensor_6_state) # Copy to allow modification in tests.
@ -222,7 +222,7 @@ async def test_node_ready(
await ws_client.send_json(
{
ID: 3,
TYPE: "zwave_js/node_ready",
TYPE: "zwave_js/subscribe_node_status",
DEVICE_ID: device.id,
}
)
@ -246,6 +246,25 @@ async def test_node_ready(
msg = await ws_client.receive_json()
assert msg["event"]["event"] == "ready"
assert msg["event"]["status"] == 1
assert msg["event"]["ready"]
event = Event(
"wake up",
{
"source": "node",
"event": "wake up",
"nodeId": node.node_id,
},
)
node.receive_event(event)
await hass.async_block_till_done()
msg = await ws_client.receive_json()
assert msg["event"]["event"] == "wake up"
assert msg["event"]["status"] == 2
assert msg["event"]["ready"]
async def test_node_status(hass, multisensor_6, integration, hass_ws_client):