hass-core/homeassistant/components/automation/trace.py

27 lines
731 B
Python
Raw Normal View History

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
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
def trace_automation(hass, item_id, config, context):
"""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:
yield trace
2021-03-10 23:42:13 +01:00
except Exception as ex: # pylint: disable=broad-except
if item_id:
trace.set_error(ex)
2021-03-10 23:42:13 +01:00
raise ex
finally:
if item_id:
trace.finished()