mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
completed time_event_listener
This commit is contained in:
parent
09908f5780
commit
c92089808f
@ -39,22 +39,22 @@ _SCHEDULE_FILE = 'schedule.json'
|
|||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
""" Create the schedules """
|
""" Create the schedules """
|
||||||
|
|
||||||
def setup_schedule(description):
|
def setup_schedule(schedule_data):
|
||||||
""" setup a schedule based on the description """
|
""" setup a schedule based on the description """
|
||||||
|
|
||||||
schedule = Schedule(hass, description['id'],
|
schedule = Schedule(schedule_data['id'],
|
||||||
name=description['name'],
|
name=schedule_data['name'],
|
||||||
description=description['description'],
|
description=schedule_data['description'],
|
||||||
entity_ids=description['entity_ids'],
|
entity_ids=schedule_data['entity_ids'],
|
||||||
days=description['days'])
|
days=schedule_data['days'])
|
||||||
|
|
||||||
for event_description in description['events']:
|
for event_data in schedule_data['events']:
|
||||||
event_type = \
|
event_listener_type = \
|
||||||
get_component('scheduler.{}'.format(event_description['type']))
|
get_component('scheduler.{}'.format(event_data['type']))
|
||||||
event = event_type.create(schedule, event_description)
|
event_listener = event_listener_type.create(schedule, event_data)
|
||||||
schedule.add_event(event)
|
schedule.add_event_listener(event_listener)
|
||||||
|
|
||||||
schedule.schedule()
|
schedule.schedule(hass)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
with open(hass.get_config_path(_SCHEDULE_FILE)) as schedule_file:
|
with open(hass.get_config_path(_SCHEDULE_FILE)) as schedule_file:
|
||||||
@ -69,11 +69,9 @@ def setup(hass, config):
|
|||||||
|
|
||||||
class Schedule(object):
|
class Schedule(object):
|
||||||
""" A Schedule """
|
""" A Schedule """
|
||||||
def __init__(self, hass, schedule_id, name=None, description=None,
|
def __init__(self, schedule_id, name=None, description=None,
|
||||||
entity_ids=None, days=None):
|
entity_ids=None, days=None):
|
||||||
|
|
||||||
self.hass = hass
|
|
||||||
|
|
||||||
self.schedule_id = schedule_id
|
self.schedule_id = schedule_id
|
||||||
self.name = name
|
self.name = name
|
||||||
self.description = description
|
self.description = description
|
||||||
@ -82,27 +80,27 @@ class Schedule(object):
|
|||||||
|
|
||||||
self.days = days or [0, 1, 2, 3, 4, 5, 6]
|
self.days = days or [0, 1, 2, 3, 4, 5, 6]
|
||||||
|
|
||||||
self._events = []
|
self._event_listeners = []
|
||||||
|
|
||||||
def add_event(self, event):
|
def add_event_listener(self, event_listener):
|
||||||
""" Add a event to the schedule """
|
""" Add a event to the schedule """
|
||||||
self._events.append(event)
|
self._event_listeners.append(event_listener)
|
||||||
|
|
||||||
def schedule(self):
|
def schedule(self, hass):
|
||||||
""" Schedule all the events in the schdule """
|
""" Schedule all the events in the schdule """
|
||||||
for event in self._events:
|
for event in self._event_listeners:
|
||||||
event.schedule()
|
event.schedule(hass)
|
||||||
|
|
||||||
|
|
||||||
class Event(object):
|
class EventListener(object):
|
||||||
""" The base Event class that the schedule uses """
|
""" The base Event class that the schedule uses """
|
||||||
def __init__(self, schedule):
|
def __init__(self, schedule):
|
||||||
self._schedule = schedule
|
self._schedule = schedule
|
||||||
|
|
||||||
def schedule(self):
|
def schedule(self, hass):
|
||||||
""" Schedule the event """
|
""" Schedule the event """
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def execute(self):
|
def execute(self, hass):
|
||||||
""" execute the event """
|
""" execute the event """
|
||||||
pass
|
pass
|
||||||
|
@ -15,34 +15,33 @@ which time.
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.components.scheduler import Event
|
from homeassistant.components.scheduler import EventListener
|
||||||
|
from homeassistant.components import ATTR_ENTITY_ID
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def create(schedule, description):
|
def create(schedule, event_listener_data):
|
||||||
""" Create a TimeEvent based on the description """
|
""" Create a TimeEvent based on the description """
|
||||||
|
|
||||||
service = description['service']
|
service = event_listener_data['service']
|
||||||
(hour, minute) = [int(x) for x in description['time'].split(':')]
|
(hour, minute) = [int(x) for x in event_listener_data['time'].split(':')]
|
||||||
|
|
||||||
return TimeEvent(schedule, service, hour, minute)
|
return TimeEventListener(schedule, service, hour, minute)
|
||||||
|
|
||||||
|
|
||||||
class TimeEvent(Event):
|
class TimeEventListener(EventListener):
|
||||||
""" The time event that the scheduler uses """
|
""" The time event that the scheduler uses """
|
||||||
|
|
||||||
def __init__(self, schedule, service, hour, minute):
|
def __init__(self, schedule, service, hour, minute):
|
||||||
Event.__init__(self, schedule)
|
EventListener.__init__(self, schedule)
|
||||||
|
|
||||||
(self._domain, self._service) = service.split('.')
|
(self._domain, self._service) = service.split('.')
|
||||||
|
|
||||||
self._hour = hour
|
self._hour = hour
|
||||||
self._minute = minute
|
self._minute = minute
|
||||||
|
|
||||||
print(self._domain, self._service)
|
def schedule(self, hass):
|
||||||
|
|
||||||
def schedule(self):
|
|
||||||
""" Schedule this event so that it will be called """
|
""" Schedule this event so that it will be called """
|
||||||
|
|
||||||
next_time = datetime.now().replace(hour=self._hour,
|
next_time = datetime.now().replace(hour=self._hour,
|
||||||
@ -59,16 +58,18 @@ class TimeEvent(Event):
|
|||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def execute(now):
|
def execute(now):
|
||||||
""" Call the execute method """
|
""" Call the execute method """
|
||||||
self.execute()
|
self.execute(hass)
|
||||||
|
|
||||||
self._schedule.hass.track_point_in_time(execute, next_time)
|
hass.track_point_in_time(execute, next_time)
|
||||||
|
|
||||||
_LOGGER.info('point in time scheduled at {} for {}'
|
_LOGGER.info(
|
||||||
.format(next_time, ""))
|
'TimeEventListener scheduled for {}, will call service {}.{}'
|
||||||
|
.format(next_time, self._domain, self._service))
|
||||||
|
|
||||||
def execute(self):
|
def execute(self, hass):
|
||||||
""" Call the service """
|
""" Call the service """
|
||||||
# data = {ATTR_ENTITY_ID: self._schedule.entity_ids}
|
data = {ATTR_ENTITY_ID: self._schedule.entity_ids}
|
||||||
# self._schedule.hass.call_service(self._domain, self._service, data)
|
hass.call_service(self._domain, self._service, data)
|
||||||
print("executoing time", self._domain, self._service)
|
|
||||||
self.schedule()
|
# Reschedule for next day
|
||||||
|
self.schedule(hass)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user