2021-03-10 22:42:13 +00:00
|
|
|
"""Trace support for automation."""
|
2021-03-17 22:34:25 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-09-14 11:04:09 +00:00
|
|
|
from collections.abc import Generator
|
2021-03-10 22:42:13 +00:00
|
|
|
from contextlib import contextmanager
|
2021-03-29 21:06:49 +00:00
|
|
|
from typing import Any
|
2021-03-10 22:42:13 +00:00
|
|
|
|
2022-09-12 16:06:03 +00:00
|
|
|
from homeassistant.components.trace import (
|
|
|
|
CONF_STORED_TRACES,
|
|
|
|
ActionTrace,
|
|
|
|
async_store_trace,
|
|
|
|
)
|
2022-09-14 11:04:09 +00:00
|
|
|
from homeassistant.core import Context, HomeAssistant
|
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2021-03-10 22:42:13 +00:00
|
|
|
|
2021-10-19 08:23:23 +00:00
|
|
|
from .const import DOMAIN
|
|
|
|
|
2021-03-10 22:42:13 +00:00
|
|
|
|
2021-03-29 06:09:14 +00:00
|
|
|
class AutomationTrace(ActionTrace):
|
|
|
|
"""Container for automation trace."""
|
|
|
|
|
2021-10-19 08:23:23 +00:00
|
|
|
_domain = DOMAIN
|
|
|
|
|
2021-03-29 06:09:14 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2022-09-14 11:04:09 +00:00
|
|
|
item_id: str | None,
|
|
|
|
config: ConfigType | None,
|
|
|
|
blueprint_inputs: ConfigType | None,
|
2021-03-29 06:09:14 +00:00
|
|
|
context: Context,
|
2021-05-20 15:51:39 +00:00
|
|
|
) -> None:
|
2021-03-29 06:09:14 +00:00
|
|
|
"""Container for automation trace."""
|
2021-10-19 08:23:23 +00:00
|
|
|
super().__init__(item_id, config, blueprint_inputs, context)
|
2021-03-29 21:06:49 +00:00
|
|
|
self._trigger_description: str | None = None
|
2021-03-29 06:09:14 +00:00
|
|
|
|
2021-03-29 21:06:49 +00:00
|
|
|
def set_trigger_description(self, trigger: str) -> None:
|
|
|
|
"""Set trigger description."""
|
|
|
|
self._trigger_description = trigger
|
2021-03-29 06:09:14 +00:00
|
|
|
|
|
|
|
def as_short_dict(self) -> dict[str, Any]:
|
|
|
|
"""Return a brief dictionary version of this AutomationTrace."""
|
2021-10-19 08:23:23 +00:00
|
|
|
if self._short_dict:
|
|
|
|
return self._short_dict
|
|
|
|
|
2021-03-29 06:09:14 +00:00
|
|
|
result = super().as_short_dict()
|
2021-03-29 21:06:49 +00:00
|
|
|
result["trigger"] = self._trigger_description
|
2021-03-29 06:09:14 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2021-03-10 22:42:13 +00:00
|
|
|
@contextmanager
|
2021-04-27 17:27:12 +00:00
|
|
|
def trace_automation(
|
2022-09-14 11:04:09 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
automation_id: str | None,
|
|
|
|
config: ConfigType | None,
|
|
|
|
blueprint_inputs: ConfigType | None,
|
|
|
|
context: Context,
|
|
|
|
trace_config: ConfigType,
|
|
|
|
) -> Generator[AutomationTrace, None, None]:
|
2021-03-29 06:09:14 +00:00
|
|
|
"""Trace action execution of automation with automation_id."""
|
2021-04-01 20:34:47 +00:00
|
|
|
trace = AutomationTrace(automation_id, config, blueprint_inputs, context)
|
2021-04-27 17:27:12 +00:00
|
|
|
async_store_trace(hass, trace, trace_config[CONF_STORED_TRACES])
|
2021-03-10 22:42:13 +00:00
|
|
|
|
|
|
|
try:
|
2021-03-23 21:53:38 +00:00
|
|
|
yield trace
|
2021-03-30 04:02:56 +00:00
|
|
|
except Exception as ex:
|
2021-03-29 06:09:14 +00:00
|
|
|
if automation_id:
|
2021-03-23 21:53:38 +00:00
|
|
|
trace.set_error(ex)
|
2021-03-10 22:42:13 +00:00
|
|
|
raise ex
|
|
|
|
finally:
|
2021-03-29 06:09:14 +00:00
|
|
|
if automation_id:
|
2021-03-23 21:53:38 +00:00
|
|
|
trace.finished()
|