2015-01-16 07:32:27 +00:00
|
|
|
"""
|
2016-03-07 19:20:07 +00:00
|
|
|
Offer time listening automation rules.
|
2015-10-13 19:08:51 +00:00
|
|
|
|
|
|
|
For more details about this automation rule, please refer to the documentation
|
2015-11-09 12:12:18 +00:00
|
|
|
at https://home-assistant.io/components/automation/#time-trigger
|
2015-01-16 07:32:27 +00:00
|
|
|
"""
|
2017-02-18 22:17:18 +00:00
|
|
|
import asyncio
|
2015-09-14 05:25:42 +00:00
|
|
|
import logging
|
|
|
|
|
2016-04-28 10:03:57 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
from homeassistant.core import callback
|
2016-05-03 05:05:09 +00:00
|
|
|
from homeassistant.const import CONF_AFTER, CONF_PLATFORM
|
2016-04-28 10:03:57 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
2016-10-01 08:22:13 +00:00
|
|
|
from homeassistant.helpers.event import async_track_time_change
|
2015-01-16 07:32:27 +00:00
|
|
|
|
2015-09-15 05:05:40 +00:00
|
|
|
CONF_HOURS = "hours"
|
|
|
|
CONF_MINUTES = "minutes"
|
|
|
|
CONF_SECONDS = "seconds"
|
2015-01-16 07:32:27 +00:00
|
|
|
|
2015-09-17 06:24:06 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-04-28 10:03:57 +00:00
|
|
|
TRIGGER_SCHEMA = vol.All(vol.Schema({
|
|
|
|
vol.Required(CONF_PLATFORM): 'time',
|
|
|
|
CONF_AFTER: cv.time,
|
|
|
|
CONF_HOURS: vol.Any(vol.Coerce(int), vol.Coerce(str)),
|
|
|
|
CONF_MINUTES: vol.Any(vol.Coerce(int), vol.Coerce(str)),
|
|
|
|
CONF_SECONDS: vol.Any(vol.Coerce(int), vol.Coerce(str)),
|
|
|
|
}), cv.has_at_least_one_key(CONF_HOURS, CONF_MINUTES,
|
|
|
|
CONF_SECONDS, CONF_AFTER))
|
|
|
|
|
2015-01-16 07:32:27 +00:00
|
|
|
|
2017-02-18 22:17:18 +00:00
|
|
|
@asyncio.coroutine
|
2016-10-01 08:22:13 +00:00
|
|
|
def async_trigger(hass, config, action):
|
2016-03-07 16:14:55 +00:00
|
|
|
"""Listen for state changes based on configuration."""
|
2015-09-15 15:56:06 +00:00
|
|
|
if CONF_AFTER in config:
|
2016-04-28 10:03:57 +00:00
|
|
|
after = config.get(CONF_AFTER)
|
2015-09-15 15:56:06 +00:00
|
|
|
hours, minutes, seconds = after.hour, after.minute, after.second
|
2016-04-28 10:03:57 +00:00
|
|
|
else:
|
2016-01-26 16:06:50 +00:00
|
|
|
hours = config.get(CONF_HOURS)
|
2016-01-26 09:28:31 +00:00
|
|
|
minutes = config.get(CONF_MINUTES)
|
|
|
|
seconds = config.get(CONF_SECONDS)
|
2015-09-15 15:56:06 +00:00
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
@callback
|
2015-01-16 07:32:27 +00:00
|
|
|
def time_automation_listener(now):
|
2016-03-07 19:20:07 +00:00
|
|
|
"""Listen for time changes and calls action."""
|
2016-10-05 03:44:32 +00:00
|
|
|
hass.async_run_job(action, {
|
2016-04-21 20:59:42 +00:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'time',
|
|
|
|
'now': now,
|
|
|
|
},
|
|
|
|
})
|
2015-01-16 07:32:27 +00:00
|
|
|
|
2016-10-01 08:22:13 +00:00
|
|
|
return async_track_time_change(hass, time_automation_listener,
|
|
|
|
hour=hours, minute=minutes, second=seconds)
|