1 """Offer time listening automation rules."""
3 from __future__
import annotations
5 from datetime
import datetime
8 import voluptuous
as vol
18 CONF_MINUTES =
"minutes"
19 CONF_SECONDS =
"seconds"
23 """Validate a time pattern value.
25 :raises Invalid: If the value has a wrong format or is outside the range.
29 """Initialize time pattern."""
38 if isinstance(value, str)
and value.startswith(
"/"):
39 number =
int(value[1:])
41 value = number =
int(value)
43 if not (0 <= number <= self.
maximummaximum):
44 raise vol.Invalid(f
"must be a value between 0 and {self.maximum}")
45 except ValueError
as err:
46 raise vol.Invalid(
"invalid time_pattern value")
from err
51 TRIGGER_SCHEMA = vol.All(
52 cv.TRIGGER_BASE_SCHEMA.extend(
54 vol.Required(CONF_PLATFORM):
"time_pattern",
60 cv.has_at_least_one_key(CONF_HOURS, CONF_MINUTES, CONF_SECONDS),
67 action: TriggerActionType,
68 trigger_info: TriggerInfo,
70 """Listen for state changes based on configuration."""
71 trigger_data = trigger_info[
"trigger_data"]
72 hours = config.get(CONF_HOURS)
73 minutes = config.get(CONF_MINUTES)
74 seconds = config.get(CONF_SECONDS)
75 job =
HassJob(action, f
"time pattern trigger {trigger_info}")
78 if minutes
is None and hours
is not None:
80 if seconds
is None and minutes
is not None:
84 def time_automation_listener(now: datetime) ->
None:
85 """Listen for time changes and calls action."""
86 hass.async_run_hass_job(
91 "platform":
"time_pattern",
93 "description":
"time pattern",
99 hass, time_automation_listener, hour=hours, minute=minutes, second=seconds
str|int __call__(self, Any value)
None __init__(self, int maximum)
CALLBACK_TYPE async_attach_trigger(HomeAssistant hass, ConfigType config, TriggerActionType action, TriggerInfo trigger_info)
CALLBACK_TYPE async_track_time_change(HomeAssistant hass, Callable[[datetime], Coroutine[Any, Any, None]|None] action, Any|None hour=None, Any|None minute=None, Any|None second=None)