1 """Support for script and automation tracing and debugging."""
3 from __future__
import annotations
7 import voluptuous
as vol
17 from .
import websocket_api
22 DEFAULT_STORED_TRACES,
24 from .models
import ActionTrace
25 from .util
import async_store_trace
27 _LOGGER = logging.getLogger(__name__)
31 STORAGE_KEY =
"trace.saved_traces"
34 TRACE_CONFIG_SCHEMA = {
35 vol.Optional(CONF_STORED_TRACES, default=DEFAULT_STORED_TRACES): cv.positive_int
38 CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
42 "TRACE_CONFIG_SCHEMA",
48 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
49 """Initialize the trace integration."""
50 hass.data[DATA_TRACE] = {}
51 websocket_api.async_setup(hass)
52 store = Store[dict[str, list]](
53 hass, STORAGE_VERSION, STORAGE_KEY, encoder=ExtendedJSONEncoder
55 hass.data[DATA_TRACE_STORE] = store
57 async
def _async_store_traces_at_stop(_: Event) ->
None:
58 """Save traces to storage."""
59 _LOGGER.debug(
"Storing traces")
61 await store.async_save(
63 key:
list(traces.values())
64 for key, traces
in hass.data[DATA_TRACE].items()
67 except HomeAssistantError
as exc:
68 _LOGGER.error(
"Error storing traces", exc_info=exc)
71 hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_store_traces_at_stop)
bool async_setup(HomeAssistant hass, ConfigType config)