1
mirror of https://github.com/home-assistant/core synced 2024-08-02 23:40:32 +02:00
ha-core/homeassistant/components/script/trace.py
Erik Montnemery 961ee717ef
Store automation and script traces (#56894)
* Store automation and script traces

* Pylint

* Deduplicate code

* Fix issues when no stored traces are available

* Store serialized data for restored traces

* Update WS API

* Update test

* Restore context

* Improve tests

* Add new test files

* Rename restore_traces to async_restore_traces

* Refactor trace.websocket_api

* Defer loading stored traces

* Lint

* Revert refactoring which is no longer needed

* Correct order when restoring traces

* Apply suggestion from code review

* Improve test coverage

* Apply suggestions from code review
2021-10-19 10:23:23 +02:00

43 lines
1.1 KiB
Python

"""Trace support for script."""
from __future__ import annotations
from collections.abc import Iterator
from contextlib import contextmanager
from typing import Any
from homeassistant.components.trace import ActionTrace, async_store_trace
from homeassistant.components.trace.const import CONF_STORED_TRACES
from homeassistant.core import Context, HomeAssistant
from .const import DOMAIN
class ScriptTrace(ActionTrace):
"""Container for script trace."""
_domain = DOMAIN
@contextmanager
def trace_script(
hass: HomeAssistant,
item_id: str,
config: dict[str, Any],
blueprint_inputs: dict[str, Any],
context: Context,
trace_config: dict[str, Any],
) -> Iterator[ScriptTrace]:
"""Trace execution of a script."""
trace = ScriptTrace(item_id, config, blueprint_inputs, context)
async_store_trace(hass, trace, trace_config[CONF_STORED_TRACES])
try:
yield trace
except Exception as ex:
if item_id:
trace.set_error(ex)
raise ex
finally:
if item_id:
trace.finished()