SunriseEventListener and SunsetEventListner
parent
c436b33da9
commit
83320681f0
|
@ -28,7 +28,7 @@ DOMAIN = 'scheduler'
|
||||||
# List of component names (string) your component depends upon
|
# List of component names (string) your component depends upon
|
||||||
# If you are setting up a group but not using a group for anything,
|
# If you are setting up a group but not using a group for anything,
|
||||||
# don't depend on group
|
# don't depend on group
|
||||||
DEPENDENCIES = []
|
DEPENDENCIES = ['sun']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -93,7 +93,7 @@ class Schedule(object):
|
||||||
|
|
||||||
|
|
||||||
class EventListener(object):
|
class EventListener(object):
|
||||||
""" The base Event class that the schedule uses """
|
""" The base EventListner class that the schedule uses """
|
||||||
def __init__(self, schedule):
|
def __init__(self, schedule):
|
||||||
self._schedule = schedule
|
self._schedule = schedule
|
||||||
|
|
||||||
|
@ -104,3 +104,19 @@ class EventListener(object):
|
||||||
def execute(self, hass):
|
def execute(self, hass):
|
||||||
""" execute the event """
|
""" execute the event """
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class ServiceEventListener(EventListener):
|
||||||
|
""" A EventListner that calls a service when executed """
|
||||||
|
|
||||||
|
def __init__(self, schdule, service):
|
||||||
|
EventListener.__init__(self, schdule)
|
||||||
|
|
||||||
|
(self._domain, self._service) = service.split('.')
|
||||||
|
|
||||||
|
def execute(self, hass):
|
||||||
|
""" Call the service """
|
||||||
|
data = {ATTR_ENTITY_ID: self._schedule.entity_ids}
|
||||||
|
hass.call_service(self._domain, self._service, data)
|
||||||
|
|
||||||
|
# Reschedule for next day
|
||||||
|
self.schedule(hass)
|
||||||
|
|
|
@ -1,7 +1,83 @@
|
||||||
"""asd"""
|
"""asd"""
|
||||||
|
|
||||||
from homeassistant.components.scheduler import Event
|
from datetime import datetime, timedelta
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from homeassistant.components.scheduler import ServiceEventListener
|
||||||
|
import homeassistant.components.sun as sun
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def create(schedule, description):
|
def create(schedule, event_listener_data):
|
||||||
print('creating sun')
|
negative_offset = False
|
||||||
|
service = event_listener_data['service']
|
||||||
|
offset_str = event_listener_data['offset']
|
||||||
|
event = event_listener_data['event']
|
||||||
|
|
||||||
|
if (offset_str.startswith('-')):
|
||||||
|
negative_offset = True
|
||||||
|
offset_str = offset_str[1:]
|
||||||
|
|
||||||
|
(hour, minute, second) = [int(x) for x in offset_str.split(':')]
|
||||||
|
|
||||||
|
offset = timedelta(hours=hour, minutes=minute, seconds=second)
|
||||||
|
|
||||||
|
if event == 'sunset':
|
||||||
|
return SunsetEventListener(schedule, service, offset, negative_offset)
|
||||||
|
|
||||||
|
return SunriseEventListener(schedule, service, offset, negative_offset)
|
||||||
|
|
||||||
|
|
||||||
|
class SunEventListener(ServiceEventListener):
|
||||||
|
def __init__(self, schedule, service, offset, negative_offset):
|
||||||
|
ServiceEventListener.__init__(self, schedule, service)
|
||||||
|
|
||||||
|
self._offset = offset
|
||||||
|
self._negative_offset = negative_offset
|
||||||
|
|
||||||
|
def __get_next_time(self, next_event):
|
||||||
|
if self._negative_offset:
|
||||||
|
next_time = next_event - self._offset
|
||||||
|
else:
|
||||||
|
next_time = next_event + self._offset
|
||||||
|
|
||||||
|
while next_time < datetime.now() or \
|
||||||
|
next_time.weekday() not in self._schedule.days:
|
||||||
|
next_time = next_time + timedelta(days=1)
|
||||||
|
|
||||||
|
return next_time
|
||||||
|
|
||||||
|
def schedule_next_event(self, hass, next_event):
|
||||||
|
next_time = self.__get_next_time(next_event)
|
||||||
|
|
||||||
|
# pylint: disable=unused-argument
|
||||||
|
def execute(now):
|
||||||
|
""" Call the execute method """
|
||||||
|
self.execute(hass)
|
||||||
|
|
||||||
|
hass.track_point_in_time(execute, next_time)
|
||||||
|
|
||||||
|
return next_time
|
||||||
|
|
||||||
|
|
||||||
|
class SunsetEventListener(SunEventListener):
|
||||||
|
def schedule(self, hass):
|
||||||
|
next_setting = sun.next_setting(hass)
|
||||||
|
|
||||||
|
next_time = self.schedule_next_event(hass, next_setting)
|
||||||
|
|
||||||
|
_LOGGER.info(
|
||||||
|
'SunsetEventListener scheduled for {}, wiill call service {}.{}'
|
||||||
|
.format(next_time, self._domain, self._service))
|
||||||
|
|
||||||
|
|
||||||
|
class SunriseEventListener(SunEventListener):
|
||||||
|
def schedule(self, hass):
|
||||||
|
next_rising = sun.next_rising(hass)
|
||||||
|
|
||||||
|
next_time = self.schedule_next_event(hass, next_rising)
|
||||||
|
|
||||||
|
_LOGGER.info(
|
||||||
|
'SunriseEventListener scheduled for {}, wiill call service {}.{}'
|
||||||
|
.format(next_time, self._domain, self._service))
|
||||||
|
|
Loading…
Reference in New Issue