2021-03-10 23:42:13 +01:00
|
|
|
"""Trace support for automation."""
|
2021-03-17 23:34:25 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-03-10 23:42:13 +01:00
|
|
|
from contextlib import contextmanager
|
|
|
|
|
2021-03-24 17:56:22 +01:00
|
|
|
from homeassistant.components.trace import AutomationTrace, async_store_trace
|
2021-03-10 23:42:13 +01:00
|
|
|
|
|
|
|
# mypy: allow-untyped-calls, allow-untyped-defs
|
|
|
|
# mypy: no-check-untyped-defs, no-warn-return-any
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
2021-03-23 22:53:38 +01:00
|
|
|
def trace_automation(hass, item_id, config, context):
|
2021-03-24 17:56:22 +01:00
|
|
|
"""Trace action execution of automation with item_id."""
|
|
|
|
trace = AutomationTrace(item_id, config, context)
|
|
|
|
async_store_trace(hass, trace)
|
2021-03-10 23:42:13 +01:00
|
|
|
|
|
|
|
try:
|
2021-03-23 22:53:38 +01:00
|
|
|
yield trace
|
2021-03-10 23:42:13 +01:00
|
|
|
except Exception as ex: # pylint: disable=broad-except
|
2021-03-24 17:56:22 +01:00
|
|
|
if item_id:
|
2021-03-23 22:53:38 +01:00
|
|
|
trace.set_error(ex)
|
2021-03-10 23:42:13 +01:00
|
|
|
raise ex
|
|
|
|
finally:
|
2021-03-24 17:56:22 +01:00
|
|
|
if item_id:
|
2021-03-23 22:53:38 +01:00
|
|
|
trace.finished()
|