2014-11-26 18:28:36 +00:00
|
|
|
"""
|
|
|
|
An event in the scheduler component that will call the service
|
|
|
|
every specified day at the time specified.
|
|
|
|
A time event need to have the type 'time', which service to call and at
|
|
|
|
which time.
|
2014-11-17 20:18:01 +00:00
|
|
|
|
2014-11-26 18:28:36 +00:00
|
|
|
{
|
|
|
|
"type": "time",
|
|
|
|
"service": "switch.turn_off",
|
2014-12-28 10:29:26 +00:00
|
|
|
"time": "22:00:00"
|
2014-11-26 18:28:36 +00:00
|
|
|
}
|
2014-11-17 20:18:01 +00:00
|
|
|
|
2014-11-26 18:28:36 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
import logging
|
|
|
|
|
2014-12-28 10:38:57 +00:00
|
|
|
from homeassistant.components.scheduler import ServiceEventListener
|
2014-12-03 19:29:15 +00:00
|
|
|
from homeassistant.components import ATTR_ENTITY_ID
|
2014-11-26 18:28:36 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2014-12-03 19:29:15 +00:00
|
|
|
def create(schedule, event_listener_data):
|
2014-11-26 18:28:36 +00:00
|
|
|
""" Create a TimeEvent based on the description """
|
|
|
|
|
2014-12-03 19:29:15 +00:00
|
|
|
service = event_listener_data['service']
|
2014-12-28 10:29:26 +00:00
|
|
|
(hour, minute, second) = [int(x) for x in
|
|
|
|
event_listener_data['time'].split(':')]
|
2014-11-26 18:28:36 +00:00
|
|
|
|
2014-12-28 10:29:26 +00:00
|
|
|
return TimeEventListener(schedule, service, hour, minute, second)
|
2014-11-26 18:28:36 +00:00
|
|
|
|
|
|
|
|
2014-12-28 10:38:57 +00:00
|
|
|
class TimeEventListener(ServiceEventListener):
|
2014-11-26 18:28:36 +00:00
|
|
|
""" The time event that the scheduler uses """
|
|
|
|
|
2014-12-28 10:29:26 +00:00
|
|
|
def __init__(self, schedule, service, hour, minute, second):
|
2014-12-28 10:38:57 +00:00
|
|
|
ServiceEventListener.__init__(self, schedule, service)
|
2014-11-26 18:28:36 +00:00
|
|
|
|
|
|
|
self._hour = hour
|
|
|
|
self._minute = minute
|
2014-12-28 10:29:26 +00:00
|
|
|
self._second = second
|
2014-11-26 18:28:36 +00:00
|
|
|
|
2014-12-03 19:29:15 +00:00
|
|
|
def schedule(self, hass):
|
2014-11-26 18:28:36 +00:00
|
|
|
""" Schedule this event so that it will be called """
|
|
|
|
|
|
|
|
next_time = datetime.now().replace(hour=self._hour,
|
|
|
|
minute=self._minute,
|
2014-12-28 10:29:26 +00:00
|
|
|
second=self._second,
|
|
|
|
microsecond=0)
|
2014-11-26 18:28:36 +00:00
|
|
|
|
|
|
|
# Calculate the next time the event should be executed.
|
|
|
|
# That is the next day that the schedule is configured to run
|
|
|
|
while next_time < datetime.now() or \
|
|
|
|
next_time.weekday() not in self._schedule.days:
|
|
|
|
|
|
|
|
next_time = next_time + timedelta(days=1)
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def execute(now):
|
|
|
|
""" Call the execute method """
|
2014-12-03 19:29:15 +00:00
|
|
|
self.execute(hass)
|
2014-11-26 18:28:36 +00:00
|
|
|
|
2014-12-03 19:29:15 +00:00
|
|
|
hass.track_point_in_time(execute, next_time)
|
2014-11-26 18:28:36 +00:00
|
|
|
|
2014-12-03 19:29:15 +00:00
|
|
|
_LOGGER.info(
|
|
|
|
'TimeEventListener scheduled for {}, will call service {}.{}'
|
|
|
|
.format(next_time, self._domain, self._service))
|