Fix bug for zwave_js diagnostics (#64568)

pull/64585/head
Raman Gupta 2022-01-20 12:38:34 -05:00 committed by GitHub
parent 6803219133
commit f622bf4ff2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 115 deletions

View File

@ -301,7 +301,6 @@ 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_node_status)
websocket_api.async_register_command(hass, websocket_node_state)
websocket_api.async_register_command(hass, websocket_node_metadata)
websocket_api.async_register_command(hass, websocket_ping_node)
websocket_api.async_register_command(hass, websocket_add_node)
@ -468,29 +467,6 @@ async def websocket_node_status(
)
@websocket_api.require_admin
@websocket_api.websocket_command(
{
vol.Required(TYPE): "zwave_js/node_state",
vol.Required(ENTRY_ID): str,
vol.Required(NODE_ID): int,
}
)
@websocket_api.async_response
@async_get_node
async def websocket_node_state(
hass: HomeAssistant,
connection: ActiveConnection,
msg: dict,
node: Node,
) -> None:
"""Get the state data of a Z-Wave JS node."""
connection.send_result(
msg[ID],
{**node.data, "values": [value.data for value in node.values.values()]},
)
@websocket_api.websocket_command(
{
vol.Required(TYPE): "zwave_js/node_metadata",

View File

@ -34,4 +34,5 @@ async def async_get_device_diagnostics(
node_id = identifiers[1] if identifiers else None
if node_id is None or node_id not in client.driver.controller.nodes:
raise ValueError(f"Node for device {device.id} can't be found")
return client.driver.controller.nodes[node_id].data
node = client.driver.controller.nodes[node_id]
return {**node.data, "values": [value.data for value in node.values.values()]}

View File

@ -6,7 +6,6 @@ from unittest.mock import patch
import pytest
from zwave_js_server.const import (
CommandClass,
InclusionStrategy,
LogLevel,
Protocols,
@ -27,7 +26,6 @@ from zwave_js_server.model.controller import (
QRProvisioningInformation,
)
from zwave_js_server.model.node import Node
from zwave_js_server.model.value import _get_value_id_from_dict, get_value_id
from homeassistant.components.websocket_api.const import ERR_NOT_FOUND
from homeassistant.components.zwave_js.api import (
@ -72,8 +70,6 @@ from homeassistant.components.zwave_js.const import (
)
from homeassistant.helpers import device_registry as dr
from .common import PROPERTY_ULTRAVIOLET
async def test_network_status(hass, integration, hass_ws_client):
"""Test the network status websocket command."""
@ -203,86 +199,6 @@ async def test_node_status(hass, multisensor_6, integration, hass_ws_client):
assert msg["error"]["code"] == ERR_NOT_LOADED
async def test_node_state(hass, multisensor_6, integration, hass_ws_client):
"""Test the node_state websocket command."""
entry = integration
ws_client = await hass_ws_client(hass)
node = multisensor_6
# Update a value and ensure it is reflected in the node state
value_id = get_value_id(node, CommandClass.SENSOR_MULTILEVEL, PROPERTY_ULTRAVIOLET)
event = Event(
type="value updated",
data={
"source": "node",
"event": "value updated",
"nodeId": node.node_id,
"args": {
"commandClassName": "Multilevel Sensor",
"commandClass": 49,
"endpoint": 0,
"property": PROPERTY_ULTRAVIOLET,
"newValue": 1,
"prevValue": 0,
"propertyName": PROPERTY_ULTRAVIOLET,
},
},
)
node.receive_event(event)
await ws_client.send_json(
{
ID: 3,
TYPE: "zwave_js/node_state",
ENTRY_ID: entry.entry_id,
NODE_ID: node.node_id,
}
)
msg = await ws_client.receive_json()
# Assert that the data returned doesn't match the stale node state data
assert msg["result"] != node.data
# Replace data for the value we updated and assert the new node data is the same
# as what's returned
updated_node_data = node.data.copy()
for n, value in enumerate(updated_node_data["values"]):
if _get_value_id_from_dict(node, value) == value_id:
updated_node_data["values"][n] = node.values[value_id].data.copy()
assert msg["result"] == updated_node_data
# Test getting non-existent node fails
await ws_client.send_json(
{
ID: 4,
TYPE: "zwave_js/node_state",
ENTRY_ID: entry.entry_id,
NODE_ID: 99999,
}
)
msg = await ws_client.receive_json()
assert not msg["success"]
assert msg["error"]["code"] == ERR_NOT_FOUND
# 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: 5,
TYPE: "zwave_js/node_state",
ENTRY_ID: entry.entry_id,
NODE_ID: node.node_id,
}
)
msg = await ws_client.receive_json()
assert not msg["success"]
assert msg["error"]["code"] == ERR_NOT_LOADED
async def test_node_metadata(hass, wallmote_central_scene, integration, hass_ws_client):
"""Test the node metadata websocket command."""
entry = integration

View File

@ -2,11 +2,16 @@
from unittest.mock import patch
import pytest
from zwave_js_server.const import CommandClass
from zwave_js_server.event import Event
from zwave_js_server.model.value import _get_value_id_from_dict, get_value_id
from homeassistant.components.zwave_js.diagnostics import async_get_device_diagnostics
from homeassistant.components.zwave_js.helpers import get_device_id
from homeassistant.helpers.device_registry import async_get
from .common import PROPERTY_ULTRAVIOLET
from tests.components.diagnostics import (
get_diagnostics_for_config_entry,
get_diagnostics_for_device,
@ -27,19 +32,54 @@ async def test_config_entry_diagnostics(hass, hass_client, integration):
async def test_device_diagnostics(
hass,
client,
aeon_smart_switch_6,
aeon_smart_switch_6_state,
multisensor_6,
integration,
hass_client,
):
"""Test the device level diagnostics data dump."""
dev_reg = async_get(hass)
device = dev_reg.async_get_device({get_device_id(client, aeon_smart_switch_6)})
device = dev_reg.async_get_device({get_device_id(client, multisensor_6)})
assert device
assert (
await get_diagnostics_for_device(hass, hass_client, integration, device)
== aeon_smart_switch_6_state
# Update a value and ensure it is reflected in the node state
value_id = get_value_id(
multisensor_6, CommandClass.SENSOR_MULTILEVEL, PROPERTY_ULTRAVIOLET
)
event = Event(
type="value updated",
data={
"source": "node",
"event": "value updated",
"nodeId": multisensor_6.node_id,
"args": {
"commandClassName": "Multilevel Sensor",
"commandClass": 49,
"endpoint": 0,
"property": PROPERTY_ULTRAVIOLET,
"newValue": 1,
"prevValue": 0,
"propertyName": PROPERTY_ULTRAVIOLET,
},
},
)
multisensor_6.receive_event(event)
diagnostics_data = await get_diagnostics_for_device(
hass, hass_client, integration, device
)
# Assert that the data returned doesn't match the stale node state data
assert diagnostics_data != multisensor_6.data
# Replace data for the value we updated and assert the new node data is the same
# as what's returned
updated_node_data = multisensor_6.data.copy()
for idx, value in enumerate(updated_node_data["values"]):
if _get_value_id_from_dict(multisensor_6, value) == value_id:
updated_node_data["values"][idx] = multisensor_6.values[
value_id
].data.copy()
assert diagnostics_data == updated_node_data
async def test_device_diagnostics_error(hass, integration):