Skip to content
Merged
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
1 change: 1 addition & 0 deletions homeassistant/components/websocket_api/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
ERR_ID_REUSE = "id_reuse"
ERR_INVALID_FORMAT = "invalid_format"
ERR_NOT_FOUND = "not_found"
ERR_NOT_LOADED = "not_loaded"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to make this generic since it's only used inside the zwave_js integration. This list is not exclusive, you can make any error code.

Copy link
Copy Markdown
Member Author

@MartinHjelmare MartinHjelmare May 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. So far it seemed no custom codes were used, but I'll move it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved it: #50188

ERR_NOT_SUPPORTED = "not_supported"
ERR_HOME_ASSISTANT_ERROR = "home_assistant_error"
ERR_UNKNOWN_COMMAND = "unknown_command"
Expand Down
62 changes: 30 additions & 32 deletions homeassistant/components/zwave_js/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
from homeassistant.components.websocket_api.connection import ActiveConnection
from homeassistant.components.websocket_api.const import (
ERR_NOT_FOUND,
ERR_NOT_LOADED,
ERR_NOT_SUPPORTED,
ERR_UNKNOWN_ERROR,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.config_entries import ENTRY_STATE_LOADED, ConfigEntry
from homeassistant.const import CONF_URL
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import config_validation as cv
Expand Down Expand Up @@ -83,6 +84,13 @@ async def async_get_entry_func(
msg[ID], ERR_NOT_FOUND, f"Config entry {entry_id} not found"
)
return

if entry.state != ENTRY_STATE_LOADED:
connection.send_error(
msg[ID], ERR_NOT_LOADED, f"Config entry {entry_id} not loaded"
)
return

client = hass.data[DOMAIN][entry_id][DATA_CLIENT]
await orig_func(hass, connection, msg, entry, client)

Expand Down Expand Up @@ -137,17 +145,20 @@ def async_register_api(hass: HomeAssistant) -> None:
hass.http.register_view(DumpView) # type: ignore


@websocket_api.require_admin
@websocket_api.require_admin # type: ignore
@websocket_api.async_response
@websocket_api.websocket_command(
{vol.Required(TYPE): "zwave_js/network_status", vol.Required(ENTRY_ID): str}
)
@callback
def websocket_network_status(
hass: HomeAssistant, connection: ActiveConnection, msg: dict
@async_get_entry
async def websocket_network_status(
hass: HomeAssistant,
connection: ActiveConnection,
msg: dict,
entry: ConfigEntry,
client: Client,
) -> None:
"""Get the status of the Z-Wave JS network."""
entry_id = msg[ENTRY_ID]
client = hass.data[DOMAIN][entry_id][DATA_CLIENT]
data = {
"client": {
"ws_server_url": client.ws_server_url,
Expand All @@ -166,27 +177,22 @@ def websocket_network_status(
)


@websocket_api.async_response # type: ignore
@websocket_api.websocket_command(
{
vol.Required(TYPE): "zwave_js/node_status",
vol.Required(ENTRY_ID): str,
vol.Required(NODE_ID): int,
}
)
@callback
def websocket_node_status(
hass: HomeAssistant, connection: ActiveConnection, msg: dict
@async_get_node
async def websocket_node_status(
hass: HomeAssistant,
connection: ActiveConnection,
msg: dict,
node: Node,
) -> None:
"""Get the status of a Z-Wave JS node."""
entry_id = msg[ENTRY_ID]
client = hass.data[DOMAIN][entry_id][DATA_CLIENT]
node_id = msg[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,
Expand Down Expand Up @@ -537,28 +543,20 @@ async def websocket_set_config_parameter(
)


@websocket_api.require_admin
@websocket_api.require_admin # type: ignore
@websocket_api.async_response
@websocket_api.websocket_command(
{
vol.Required(TYPE): "zwave_js/get_config_parameters",
vol.Required(ENTRY_ID): str,
vol.Required(NODE_ID): int,
}
)
@callback
def websocket_get_config_parameters(
hass: HomeAssistant, connection: ActiveConnection, msg: dict
@async_get_node
async def websocket_get_config_parameters(
hass: HomeAssistant, connection: ActiveConnection, msg: dict, node: Node
) -> None:
"""Get a list of configuration parameters for a Z-Wave node."""
entry_id = msg[ENTRY_ID]
node_id = msg[NODE_ID]
client = hass.data[DOMAIN][entry_id][DATA_CLIENT]
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():
Expand Down
Loading