hass-core/homeassistant/components/automation/time.py

92 lines
2.6 KiB
Python
Raw Normal View History

2015-01-15 23:32:27 -08:00
"""
homeassistant.components.automation.time
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Offers time listening automation rules.
"""
2015-09-13 22:25:42 -07:00
import logging
2015-01-15 23:32:27 -08:00
from homeassistant.util import convert
2015-09-13 22:25:42 -07:00
import homeassistant.util.dt as dt_util
from homeassistant.helpers.event import track_time_change
2015-01-15 23:32:27 -08:00
2015-09-14 22:05:40 -07:00
CONF_HOURS = "hours"
CONF_MINUTES = "minutes"
CONF_SECONDS = "seconds"
2015-09-13 22:25:42 -07:00
CONF_BEFORE = "before"
CONF_AFTER = "after"
CONF_WEEKDAY = "weekday"
WEEKDAYS = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
2015-01-15 23:32:27 -08:00
2015-09-13 22:25:42 -07:00
def trigger(hass, config, action):
2015-01-15 23:32:27 -08:00
""" Listen for state changes based on `config`. """
2015-09-16 22:46:21 +02:00
hours = convert(config.get(CONF_HOURS), int)
minutes = convert(config.get(CONF_MINUTES), int)
seconds = convert(config.get(CONF_SECONDS), int)
2015-09-15 08:56:06 -07:00
if CONF_AFTER in config:
after = dt_util.parse_time_str(config[CONF_AFTER])
if after is None:
logging.getLogger(__name__).error(
'Received invalid after value: %s', config[CONF_AFTER])
return False
hours, minutes, seconds = after.hour, after.minute, after.second
2015-01-15 23:32:27 -08:00
def time_automation_listener(now):
""" Listens for time changes and calls action. """
action()
track_time_change(hass, time_automation_listener,
hour=hours, minute=minutes, second=seconds)
2015-01-15 23:32:27 -08:00
return True
2015-09-13 22:25:42 -07:00
def if_action(hass, config):
2015-09-13 22:25:42 -07:00
""" Wraps action method with time based condition. """
before = config.get(CONF_BEFORE)
after = config.get(CONF_AFTER)
weekday = config.get(CONF_WEEKDAY)
if before is None and after is None and weekday is None:
logging.getLogger(__name__).error(
"Missing if-condition configuration key %s, %s or %s",
CONF_BEFORE, CONF_AFTER, CONF_WEEKDAY)
return None
2015-09-13 22:25:42 -07:00
def time_if():
""" Validate time based if-condition """
now = dt_util.now()
if before is not None:
2015-09-15 08:56:06 -07:00
time = dt_util.parse_time_str(before)
if time is None:
return False
2015-09-13 22:25:42 -07:00
2015-09-15 08:56:06 -07:00
before_point = now.replace(hour=time.hour, minute=time.minute)
2015-09-13 22:25:42 -07:00
if now > before_point:
return False
2015-09-13 22:25:42 -07:00
if after is not None:
2015-09-15 08:56:06 -07:00
time = dt_util.parse_time_str(after)
if time is None:
return False
2015-09-13 22:25:42 -07:00
2015-09-15 08:56:06 -07:00
after_point = now.replace(hour=time.hour, minute=time.minute)
2015-09-13 22:25:42 -07:00
if now < after_point:
return False
2015-09-13 22:25:42 -07:00
if weekday is not None:
now_weekday = WEEKDAYS[now.weekday()]
if isinstance(weekday, str) and weekday != now_weekday or \
now_weekday not in weekday:
return False
2015-09-13 22:25:42 -07:00
return True
2015-09-13 22:25:42 -07:00
return time_if