Add support for multiple time triggers in automations (#37975)
* Add support for multiple time triggers in automations * Attach with single callback * Patch time in tests * Improve test coverage * Adjusting my facepalm moment
This commit is contained in:
parent
99b624a676
commit
fa9866db96
2 changed files with 104 additions and 7 deletions
|
@ -13,20 +13,37 @@ from homeassistant.helpers.event import async_track_time_change
|
|||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
TRIGGER_SCHEMA = vol.Schema(
|
||||
{vol.Required(CONF_PLATFORM): "time", vol.Required(CONF_AT): cv.time}
|
||||
{
|
||||
vol.Required(CONF_PLATFORM): "time",
|
||||
vol.Required(CONF_AT): vol.All(cv.ensure_list, [cv.time]),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def async_attach_trigger(hass, config, action, automation_info):
|
||||
"""Listen for state changes based on configuration."""
|
||||
at_time = config.get(CONF_AT)
|
||||
hours, minutes, seconds = at_time.hour, at_time.minute, at_time.second
|
||||
at_times = config[CONF_AT]
|
||||
|
||||
@callback
|
||||
def time_automation_listener(now):
|
||||
"""Listen for time changes and calls action."""
|
||||
hass.async_run_job(action, {"trigger": {"platform": "time", "now": now}})
|
||||
|
||||
return async_track_time_change(
|
||||
hass, time_automation_listener, hour=hours, minute=minutes, second=seconds
|
||||
)
|
||||
removes = [
|
||||
async_track_time_change(
|
||||
hass,
|
||||
time_automation_listener,
|
||||
hour=at_time.hour,
|
||||
minute=at_time.minute,
|
||||
second=at_time.second,
|
||||
)
|
||||
for at_time in at_times
|
||||
]
|
||||
|
||||
@callback
|
||||
def remove_track_time_changes():
|
||||
"""Remove tracked time changes."""
|
||||
for remove in removes:
|
||||
remove()
|
||||
|
||||
return remove_track_time_changes
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue