Guard zwave_js missing nodes in websocket api (#47096)

pull/47120/head
Charles Garwood 2021-02-26 10:07:50 -05:00 committed by GitHub
parent 6e77ca70fc
commit d8633f94f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 2 deletions

View File

@ -106,7 +106,12 @@ def websocket_node_status(
entry_id = msg[ENTRY_ID]
client = hass.data[DOMAIN][entry_id][DATA_CLIENT]
node_id = msg[NODE_ID]
node = client.driver.controller.nodes[node_id]
node = client.driver.controller.nodes.get(node_id)
if node is None:
connection.send_error(msg[ID], ERR_NOT_FOUND, f"Node {node_id} not found")
return
data = {
"node_id": node.node_id,
"is_routing": node.is_routing,
@ -354,7 +359,12 @@ def websocket_get_config_parameters(
entry_id = msg[ENTRY_ID]
node_id = msg[NODE_ID]
client = hass.data[DOMAIN][entry_id][DATA_CLIENT]
node = client.driver.controller.nodes[node_id]
node = client.driver.controller.nodes.get(node_id)
if node is None:
connection.send_error(msg[ID], ERR_NOT_FOUND, f"Node {node_id} not found")
return
values = node.get_configuration_values()
result = {}
for value_id, zwave_value in values.items():

View File

@ -6,6 +6,7 @@ from zwave_js_server.const import LogLevel
from zwave_js_server.event import Event
from zwave_js_server.exceptions import InvalidNewValue, NotFoundError, SetValueFailed
from homeassistant.components.websocket_api.const import ERR_NOT_FOUND
from homeassistant.components.zwave_js.api import (
CONFIG,
ENABLED,
@ -76,6 +77,32 @@ async def test_websocket_api(hass, integration, multisensor_6, hass_ws_client):
assert result[key]["configuration_value_type"] == "enumerated"
assert result[key]["metadata"]["states"]
# Test getting non-existent node fails
await ws_client.send_json(
{
ID: 5,
TYPE: "zwave_js/node_status",
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 getting non-existent node config params fails
await ws_client.send_json(
{
ID: 6,
TYPE: "zwave_js/get_config_parameters",
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
async def test_add_node(
hass, integration, client, hass_ws_client, nortek_thermostat_added_event