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:
Franck Nijhof 2020-07-30 02:51:30 +02:00 committed by GitHub
parent 99b624a676
commit fa9866db96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 104 additions and 7 deletions

View file

@ -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