"""
homeassistant.components.automation.time
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Offers time listening automation rules.
"""
import logging

from homeassistant.util import convert
import homeassistant.util.dt as dt_util
from homeassistant.helpers.event import track_time_change

CONF_HOURS = "hours"
CONF_MINUTES = "minutes"
CONF_SECONDS = "seconds"
CONF_BEFORE = "before"
CONF_AFTER = "after"
CONF_WEEKDAY = "weekday"

WEEKDAYS = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']


def trigger(hass, config, action):
    """ Listen for state changes based on `config`. """
    hours = convert(config.get(CONF_HOURS), int)
    minutes = convert(config.get(CONF_MINUTES), int)
    seconds = convert(config.get(CONF_SECONDS), int)

    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

    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)

    return True


def if_action(hass, config):
    """ 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

    def time_if():
        """ Validate time based if-condition """
        now = dt_util.now()
        if before is not None:
            time = dt_util.parse_time_str(before)
            if time is None:
                return False

            before_point = now.replace(hour=time.hour, minute=time.minute)

            if now > before_point:
                return False

        if after is not None:
            time = dt_util.parse_time_str(after)
            if time is None:
                return False

            after_point = now.replace(hour=time.hour, minute=time.minute)

            if now < after_point:
                return False

        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

        return True

    return time_if

@@ -481,16 +481,55 @@
 'sun'%5D%0A%0A
+_LOGGER = logging.getLogger(__name__)%0A%0A
 %0Adef tri
@@ -616,164 +616,8 @@
 %22%22%22%0A
-    hours = convert(config.get(CONF_HOURS), int)%0A    minutes = convert(config.get(CONF_MINUTES), int)%0A    seconds = convert(config.get(CONF_SECONDS), int)%0A%0A
     
@@ -742,35 +742,15 @@
     
-logging.getLogger(__name__)
+_LOGGER
 .err
@@ -850,16 +850,16 @@
 n False%0A
-
         
@@ -922,16 +922,443 @@
 r.second
+%0A    elif CONF_HOURS in config or CONF_MINUTES in config %5C%0A         or CONF_SECONDS in config:%0A        hours = convert(config.get(CONF_HOURS), int)%0A        minutes = convert(config.get(CONF_MINUTES), int)%0A        seconds = convert(config.get(CONF_SECONDS), int)%0A    else:%0A        _LOGGER.error('One of %25s, %25s, %25s OR %25s needs to be specified',%0A            CONF_HOURS, CONF_MINUTES, CONF_SECONDS, CONF_AFTER)%0A        return False
 %0A%0A    de

return and output error if none of the 4 keys provided only parse hour/minute/second if after is not available