2021-03-10 22:42:13 +00:00
|
|
|
"""Websocket API for automation."""
|
2021-03-16 13:21:05 +00:00
|
|
|
import json
|
|
|
|
|
2021-03-10 22:42:13 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components import websocket_api
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
|
|
from homeassistant.helpers.dispatcher import (
|
|
|
|
DATA_DISPATCHER,
|
|
|
|
async_dispatcher_connect,
|
|
|
|
async_dispatcher_send,
|
|
|
|
)
|
2021-04-10 03:47:10 +00:00
|
|
|
from homeassistant.helpers.json import ExtendedJSONEncoder
|
2021-03-10 22:42:13 +00:00
|
|
|
from homeassistant.helpers.script import (
|
|
|
|
SCRIPT_BREAKPOINT_HIT,
|
|
|
|
SCRIPT_DEBUG_CONTINUE_ALL,
|
|
|
|
breakpoint_clear,
|
|
|
|
breakpoint_clear_all,
|
|
|
|
breakpoint_list,
|
|
|
|
breakpoint_set,
|
|
|
|
debug_continue,
|
|
|
|
debug_step,
|
|
|
|
debug_stop,
|
|
|
|
)
|
|
|
|
|
2021-03-29 06:09:14 +00:00
|
|
|
from .const import DATA_TRACE
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
# mypy: allow-untyped-calls, allow-untyped-defs
|
|
|
|
|
2021-03-29 06:09:14 +00:00
|
|
|
TRACE_DOMAINS = ("automation", "script")
|
2021-03-23 21:53:38 +00:00
|
|
|
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_setup(hass: HomeAssistant) -> None:
|
|
|
|
"""Set up the websocket API."""
|
2021-03-23 21:53:38 +00:00
|
|
|
websocket_api.async_register_command(hass, websocket_trace_get)
|
|
|
|
websocket_api.async_register_command(hass, websocket_trace_list)
|
|
|
|
websocket_api.async_register_command(hass, websocket_trace_contexts)
|
|
|
|
websocket_api.async_register_command(hass, websocket_breakpoint_clear)
|
|
|
|
websocket_api.async_register_command(hass, websocket_breakpoint_list)
|
|
|
|
websocket_api.async_register_command(hass, websocket_breakpoint_set)
|
|
|
|
websocket_api.async_register_command(hass, websocket_debug_continue)
|
|
|
|
websocket_api.async_register_command(hass, websocket_debug_step)
|
|
|
|
websocket_api.async_register_command(hass, websocket_debug_stop)
|
2021-03-10 22:42:13 +00:00
|
|
|
websocket_api.async_register_command(hass, websocket_subscribe_breakpoint_events)
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
@websocket_api.require_admin
|
|
|
|
@websocket_api.websocket_command(
|
|
|
|
{
|
2021-03-23 21:53:38 +00:00
|
|
|
vol.Required("type"): "trace/get",
|
|
|
|
vol.Required("domain"): vol.In(TRACE_DOMAINS),
|
|
|
|
vol.Required("item_id"): str,
|
2021-03-10 22:42:13 +00:00
|
|
|
vol.Required("run_id"): str,
|
|
|
|
}
|
|
|
|
)
|
2021-03-23 21:53:38 +00:00
|
|
|
def websocket_trace_get(hass, connection, msg):
|
2021-04-27 17:27:12 +00:00
|
|
|
"""Get a script or automation trace."""
|
2021-03-23 21:53:38 +00:00
|
|
|
key = (msg["domain"], msg["item_id"])
|
2021-03-10 22:42:13 +00:00
|
|
|
run_id = msg["run_id"]
|
|
|
|
|
2021-03-31 13:58:36 +00:00
|
|
|
try:
|
|
|
|
trace = hass.data[DATA_TRACE][key][run_id]
|
|
|
|
except KeyError:
|
|
|
|
connection.send_error(
|
|
|
|
msg["id"], websocket_api.ERR_NOT_FOUND, "The trace could not be found"
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
2021-03-16 13:21:05 +00:00
|
|
|
message = websocket_api.messages.result_message(msg["id"], trace)
|
2021-03-10 22:42:13 +00:00
|
|
|
|
2021-04-10 03:47:10 +00:00
|
|
|
connection.send_message(
|
|
|
|
json.dumps(message, cls=ExtendedJSONEncoder, allow_nan=False)
|
|
|
|
)
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
|
2021-03-29 06:09:14 +00:00
|
|
|
def get_debug_traces(hass, key):
|
2021-04-27 17:27:12 +00:00
|
|
|
"""Return a serializable list of debug traces for a script or automation."""
|
2021-03-29 06:09:14 +00:00
|
|
|
traces = []
|
|
|
|
|
|
|
|
for trace in hass.data[DATA_TRACE].get(key, {}).values():
|
|
|
|
traces.append(trace.as_short_dict())
|
|
|
|
|
|
|
|
return traces
|
|
|
|
|
|
|
|
|
2021-03-10 22:42:13 +00:00
|
|
|
@callback
|
|
|
|
@websocket_api.require_admin
|
2021-03-15 23:51:04 +00:00
|
|
|
@websocket_api.websocket_command(
|
2021-03-23 21:53:38 +00:00
|
|
|
{
|
|
|
|
vol.Required("type"): "trace/list",
|
2021-03-29 06:09:14 +00:00
|
|
|
vol.Required("domain", "id"): vol.In(TRACE_DOMAINS),
|
|
|
|
vol.Optional("item_id", "id"): str,
|
2021-03-23 21:53:38 +00:00
|
|
|
}
|
2021-03-15 23:51:04 +00:00
|
|
|
)
|
2021-03-23 21:53:38 +00:00
|
|
|
def websocket_trace_list(hass, connection, msg):
|
2021-03-29 06:09:14 +00:00
|
|
|
"""Summarize script and automation traces."""
|
|
|
|
domain = msg["domain"]
|
|
|
|
key = (domain, msg["item_id"]) if "item_id" in msg else None
|
2021-03-15 23:51:04 +00:00
|
|
|
|
2021-03-23 21:53:38 +00:00
|
|
|
if not key:
|
2021-03-29 06:09:14 +00:00
|
|
|
traces = []
|
|
|
|
for key in hass.data[DATA_TRACE]:
|
|
|
|
if key[0] == domain:
|
|
|
|
traces.extend(get_debug_traces(hass, key))
|
2021-03-15 23:51:04 +00:00
|
|
|
else:
|
2021-03-29 06:09:14 +00:00
|
|
|
traces = get_debug_traces(hass, key)
|
2021-03-10 22:42:13 +00:00
|
|
|
|
2021-03-23 21:53:38 +00:00
|
|
|
connection.send_result(msg["id"], traces)
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
|
2021-03-16 21:37:26 +00:00
|
|
|
@callback
|
|
|
|
@websocket_api.require_admin
|
|
|
|
@websocket_api.websocket_command(
|
|
|
|
{
|
2021-03-23 21:53:38 +00:00
|
|
|
vol.Required("type"): "trace/contexts",
|
|
|
|
vol.Inclusive("domain", "id"): vol.In(TRACE_DOMAINS),
|
|
|
|
vol.Inclusive("item_id", "id"): str,
|
2021-03-16 21:37:26 +00:00
|
|
|
}
|
|
|
|
)
|
2021-03-23 21:53:38 +00:00
|
|
|
def websocket_trace_contexts(hass, connection, msg):
|
2021-03-16 21:37:26 +00:00
|
|
|
"""Retrieve contexts we have traces for."""
|
2021-03-23 21:53:38 +00:00
|
|
|
key = (msg["domain"], msg["item_id"]) if "item_id" in msg else None
|
2021-03-16 21:37:26 +00:00
|
|
|
|
2021-03-23 21:53:38 +00:00
|
|
|
if key is not None:
|
|
|
|
values = {key: hass.data[DATA_TRACE].get(key, {})}
|
2021-03-16 21:37:26 +00:00
|
|
|
else:
|
2021-03-22 18:19:38 +00:00
|
|
|
values = hass.data[DATA_TRACE]
|
2021-03-16 21:37:26 +00:00
|
|
|
|
|
|
|
contexts = {
|
2021-03-23 21:53:38 +00:00
|
|
|
trace.context.id: {"run_id": trace.run_id, "domain": key[0], "item_id": key[1]}
|
|
|
|
for key, traces in values.items()
|
2021-03-16 21:37:26 +00:00
|
|
|
for trace in traces.values()
|
|
|
|
}
|
|
|
|
|
|
|
|
connection.send_result(msg["id"], contexts)
|
|
|
|
|
|
|
|
|
2021-03-10 22:42:13 +00:00
|
|
|
@callback
|
|
|
|
@websocket_api.require_admin
|
|
|
|
@websocket_api.websocket_command(
|
|
|
|
{
|
2021-03-23 21:53:38 +00:00
|
|
|
vol.Required("type"): "trace/debug/breakpoint/set",
|
|
|
|
vol.Required("domain"): vol.In(TRACE_DOMAINS),
|
|
|
|
vol.Required("item_id"): str,
|
2021-03-10 22:42:13 +00:00
|
|
|
vol.Required("node"): str,
|
|
|
|
vol.Optional("run_id"): str,
|
|
|
|
}
|
|
|
|
)
|
2021-03-23 21:53:38 +00:00
|
|
|
def websocket_breakpoint_set(hass, connection, msg):
|
2021-03-10 22:42:13 +00:00
|
|
|
"""Set breakpoint."""
|
2021-03-23 21:53:38 +00:00
|
|
|
key = (msg["domain"], msg["item_id"])
|
2021-03-10 22:42:13 +00:00
|
|
|
node = msg["node"]
|
|
|
|
run_id = msg.get("run_id")
|
|
|
|
|
|
|
|
if (
|
|
|
|
SCRIPT_BREAKPOINT_HIT not in hass.data.get(DATA_DISPATCHER, {})
|
|
|
|
or not hass.data[DATA_DISPATCHER][SCRIPT_BREAKPOINT_HIT]
|
|
|
|
):
|
|
|
|
raise HomeAssistantError("No breakpoint subscription")
|
|
|
|
|
2021-03-23 21:53:38 +00:00
|
|
|
result = breakpoint_set(hass, key, run_id, node)
|
2021-03-10 22:42:13 +00:00
|
|
|
connection.send_result(msg["id"], result)
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
@websocket_api.require_admin
|
|
|
|
@websocket_api.websocket_command(
|
|
|
|
{
|
2021-03-23 21:53:38 +00:00
|
|
|
vol.Required("type"): "trace/debug/breakpoint/clear",
|
|
|
|
vol.Required("domain"): vol.In(TRACE_DOMAINS),
|
|
|
|
vol.Required("item_id"): str,
|
2021-03-10 22:42:13 +00:00
|
|
|
vol.Required("node"): str,
|
|
|
|
vol.Optional("run_id"): str,
|
|
|
|
}
|
|
|
|
)
|
2021-03-23 21:53:38 +00:00
|
|
|
def websocket_breakpoint_clear(hass, connection, msg):
|
2021-03-10 22:42:13 +00:00
|
|
|
"""Clear breakpoint."""
|
2021-03-23 21:53:38 +00:00
|
|
|
key = (msg["domain"], msg["item_id"])
|
2021-03-10 22:42:13 +00:00
|
|
|
node = msg["node"]
|
|
|
|
run_id = msg.get("run_id")
|
|
|
|
|
2021-03-23 21:53:38 +00:00
|
|
|
result = breakpoint_clear(hass, key, run_id, node)
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
connection.send_result(msg["id"], result)
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
@websocket_api.require_admin
|
2021-03-23 21:53:38 +00:00
|
|
|
@websocket_api.websocket_command({vol.Required("type"): "trace/debug/breakpoint/list"})
|
|
|
|
def websocket_breakpoint_list(hass, connection, msg):
|
2021-03-10 22:42:13 +00:00
|
|
|
"""List breakpoints."""
|
|
|
|
breakpoints = breakpoint_list(hass)
|
|
|
|
for _breakpoint in breakpoints:
|
2021-03-23 21:53:38 +00:00
|
|
|
_breakpoint["domain"], _breakpoint["item_id"] = _breakpoint.pop("key")
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
connection.send_result(msg["id"], breakpoints)
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
@websocket_api.require_admin
|
|
|
|
@websocket_api.websocket_command(
|
2021-03-23 21:53:38 +00:00
|
|
|
{vol.Required("type"): "trace/debug/breakpoint/subscribe"}
|
2021-03-10 22:42:13 +00:00
|
|
|
)
|
|
|
|
def websocket_subscribe_breakpoint_events(hass, connection, msg):
|
|
|
|
"""Subscribe to breakpoint events."""
|
|
|
|
|
|
|
|
@callback
|
2021-03-23 21:53:38 +00:00
|
|
|
def breakpoint_hit(key, run_id, node):
|
2021-03-10 22:42:13 +00:00
|
|
|
"""Forward events to websocket."""
|
|
|
|
connection.send_message(
|
|
|
|
websocket_api.event_message(
|
|
|
|
msg["id"],
|
|
|
|
{
|
2021-03-23 21:53:38 +00:00
|
|
|
"domain": key[0],
|
|
|
|
"item_id": key[1],
|
2021-03-10 22:42:13 +00:00
|
|
|
"run_id": run_id,
|
|
|
|
"node": node,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
remove_signal = async_dispatcher_connect(
|
|
|
|
hass, SCRIPT_BREAKPOINT_HIT, breakpoint_hit
|
|
|
|
)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def unsub():
|
|
|
|
"""Unsubscribe from breakpoint events."""
|
|
|
|
remove_signal()
|
|
|
|
if (
|
|
|
|
SCRIPT_BREAKPOINT_HIT not in hass.data.get(DATA_DISPATCHER, {})
|
|
|
|
or not hass.data[DATA_DISPATCHER][SCRIPT_BREAKPOINT_HIT]
|
|
|
|
):
|
|
|
|
breakpoint_clear_all(hass)
|
|
|
|
async_dispatcher_send(hass, SCRIPT_DEBUG_CONTINUE_ALL)
|
|
|
|
|
|
|
|
connection.subscriptions[msg["id"]] = unsub
|
|
|
|
|
|
|
|
connection.send_message(websocket_api.result_message(msg["id"]))
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
@websocket_api.require_admin
|
|
|
|
@websocket_api.websocket_command(
|
|
|
|
{
|
2021-03-23 21:53:38 +00:00
|
|
|
vol.Required("type"): "trace/debug/continue",
|
|
|
|
vol.Required("domain"): vol.In(TRACE_DOMAINS),
|
|
|
|
vol.Required("item_id"): str,
|
2021-03-10 22:42:13 +00:00
|
|
|
vol.Required("run_id"): str,
|
|
|
|
}
|
|
|
|
)
|
2021-03-23 21:53:38 +00:00
|
|
|
def websocket_debug_continue(hass, connection, msg):
|
2021-03-29 06:09:14 +00:00
|
|
|
"""Resume execution of halted script or automation."""
|
2021-03-23 21:53:38 +00:00
|
|
|
key = (msg["domain"], msg["item_id"])
|
2021-03-10 22:42:13 +00:00
|
|
|
run_id = msg["run_id"]
|
|
|
|
|
2021-03-23 21:53:38 +00:00
|
|
|
result = debug_continue(hass, key, run_id)
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
connection.send_result(msg["id"], result)
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
@websocket_api.require_admin
|
|
|
|
@websocket_api.websocket_command(
|
|
|
|
{
|
2021-03-23 21:53:38 +00:00
|
|
|
vol.Required("type"): "trace/debug/step",
|
|
|
|
vol.Required("domain"): vol.In(TRACE_DOMAINS),
|
|
|
|
vol.Required("item_id"): str,
|
2021-03-10 22:42:13 +00:00
|
|
|
vol.Required("run_id"): str,
|
|
|
|
}
|
|
|
|
)
|
2021-03-23 21:53:38 +00:00
|
|
|
def websocket_debug_step(hass, connection, msg):
|
2021-03-29 06:09:14 +00:00
|
|
|
"""Single step a halted script or automation."""
|
2021-03-23 21:53:38 +00:00
|
|
|
key = (msg["domain"], msg["item_id"])
|
2021-03-10 22:42:13 +00:00
|
|
|
run_id = msg["run_id"]
|
|
|
|
|
2021-03-23 21:53:38 +00:00
|
|
|
result = debug_step(hass, key, run_id)
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
connection.send_result(msg["id"], result)
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
@websocket_api.require_admin
|
|
|
|
@websocket_api.websocket_command(
|
|
|
|
{
|
2021-03-23 21:53:38 +00:00
|
|
|
vol.Required("type"): "trace/debug/stop",
|
|
|
|
vol.Required("domain"): vol.In(TRACE_DOMAINS),
|
|
|
|
vol.Required("item_id"): str,
|
2021-03-10 22:42:13 +00:00
|
|
|
vol.Required("run_id"): str,
|
|
|
|
}
|
|
|
|
)
|
2021-03-23 21:53:38 +00:00
|
|
|
def websocket_debug_stop(hass, connection, msg):
|
2021-03-29 06:09:14 +00:00
|
|
|
"""Stop a halted script or automation."""
|
2021-03-23 21:53:38 +00:00
|
|
|
key = (msg["domain"], msg["item_id"])
|
2021-03-10 22:42:13 +00:00
|
|
|
run_id = msg["run_id"]
|
|
|
|
|
2021-03-23 21:53:38 +00:00
|
|
|
result = debug_stop(hass, key, run_id)
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
connection.send_result(msg["id"], result)
|