Fixed some lint errors

This commit is contained in:
Gustav Ahlberg 2014-12-29 00:59:22 +01:00
parent 03e30ea5ed
commit b0b62d5db0
3 changed files with 53 additions and 30 deletions

View File

@ -17,11 +17,11 @@ entity_ids, and events.
""" """
import logging import logging
import json import json
import importlib
from homeassistant.components import switch
from homeassistant.loader import get_component from homeassistant.loader import get_component
from homeassistant.const import ATTR_ENTITY_ID
# The domain of your component. Should be equal to the name of your component # The domain of your component. Should be equal to the name of your component
DOMAIN = 'scheduler' DOMAIN = 'scheduler'
@ -69,6 +69,8 @@ def setup(hass, config):
class Schedule(object): class Schedule(object):
""" A Schedule """ """ A Schedule """
# pylint: disable=too-many-arguments
def __init__(self, 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):
@ -80,22 +82,22 @@ 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._event_listeners = [] self.__event_listeners = []
def add_event_listener(self, event_listener): def add_event_listener(self, event_listener):
""" Add a event to the schedule """ """ Add a event to the schedule """
self._event_listeners.append(event_listener) self.__event_listeners.append(event_listener)
def schedule(self, hass): def schedule(self, hass):
""" Schedule all the events in the schdule """ """ Schedule all the events in the schdule """
for event in self._event_listeners: for event in self.__event_listeners:
event.schedule(hass) event.schedule(hass)
class EventListener(object): class EventListener(object):
""" The base EventListner 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.my_schedule = schedule
def schedule(self, hass): def schedule(self, hass):
""" Schedule the event """ """ Schedule the event """
@ -105,18 +107,20 @@ class EventListener(object):
""" execute the event """ """ execute the event """
pass pass
# pylint: disable=too-few-public-methods
class ServiceEventListener(EventListener): class ServiceEventListener(EventListener):
""" A EventListner that calls a service when executed """ """ A EventListner that calls a service when executed """
def __init__(self, schdule, service): def __init__(self, schdule, service):
EventListener.__init__(self, schdule) EventListener.__init__(self, schdule)
(self._domain, self._service) = service.split('.') (self.domain, self.service) = service.split('.')
def execute(self, hass): def execute(self, hass):
""" Call the service """ """ Call the service """
data = {ATTR_ENTITY_ID: self._schedule.entity_ids} data = {ATTR_ENTITY_ID: self.my_schedule.entity_ids}
hass.call_service(self._domain, self._service, data) hass.call_service(self.domain, self.service, data)
# Reschedule for next day # Reschedule for next day
self.schedule(hass) self.schedule(hass)

View File

@ -23,12 +23,14 @@ _LOGGER = logging.getLogger(__name__)
def create(schedule, event_listener_data): def create(schedule, event_listener_data):
""" Create a sun event listener based on the description. """
negative_offset = False negative_offset = False
service = event_listener_data['service'] service = event_listener_data['service']
offset_str = event_listener_data['offset'] offset_str = event_listener_data['offset']
event = event_listener_data['event'] event = event_listener_data['event']
if (offset_str.startswith('-')): if offset_str.startswith('-'):
negative_offset = True negative_offset = True
offset_str = offset_str[1:] offset_str = offset_str[1:]
@ -42,26 +44,35 @@ def create(schedule, event_listener_data):
return SunriseEventListener(schedule, service, offset, negative_offset) return SunriseEventListener(schedule, service, offset, negative_offset)
# pylint: disable=too-few-public-methods
class SunEventListener(ServiceEventListener): class SunEventListener(ServiceEventListener):
""" This is the base class for sun event listeners. """
def __init__(self, schedule, service, offset, negative_offset): def __init__(self, schedule, service, offset, negative_offset):
ServiceEventListener.__init__(self, schedule, service) ServiceEventListener.__init__(self, schedule, service)
self._offset = offset self.offset = offset
self._negative_offset = negative_offset self.negative_offset = negative_offset
def __get_next_time(self, next_event): def __get_next_time(self, next_event):
if self._negative_offset: """
next_time = next_event - self._offset Returns when the next time the service should be called.
Taking into account the offset and which days the event should execute.
"""
if self.negative_offset:
next_time = next_event - self.offset
else: else:
next_time = next_event + self._offset next_time = next_event + self.offset
while next_time < datetime.now() or \ while next_time < datetime.now() or \
next_time.weekday() not in self._schedule.days: next_time.weekday() not in self.my_schedule.days:
next_time = next_time + timedelta(days=1) next_time = next_time + timedelta(days=1)
return next_time return next_time
def schedule_next_event(self, hass, next_event): def schedule_next_event(self, hass, next_event):
""" Schedule the event """
next_time = self.__get_next_time(next_event) next_time = self.__get_next_time(next_event)
# pylint: disable=unused-argument # pylint: disable=unused-argument
@ -74,23 +85,30 @@ class SunEventListener(ServiceEventListener):
return next_time return next_time
# pylint: disable=too-few-public-methods
class SunsetEventListener(SunEventListener): class SunsetEventListener(SunEventListener):
""" This class is used the call a service when the sun sets. """
def schedule(self, hass): def schedule(self, hass):
""" Schedule the event """
next_setting = sun.next_setting(hass) next_setting = sun.next_setting(hass)
next_time = self.schedule_next_event(hass, next_setting) next_time = self.schedule_next_event(hass, next_setting)
_LOGGER.info( _LOGGER.info(
'SunsetEventListener scheduled for {}, wiill call service {}.{}' 'SunsetEventListener scheduled for {}, will call service {}.{}'
.format(next_time, self._domain, self._service)) .format(next_time, self.domain, self.service))
# pylint: disable=too-few-public-methods
class SunriseEventListener(SunEventListener): class SunriseEventListener(SunEventListener):
""" This class is used the call a service when the sun rises. """
def schedule(self, hass): def schedule(self, hass):
""" Schedule the event """
next_rising = sun.next_rising(hass) next_rising = sun.next_rising(hass)
next_time = self.schedule_next_event(hass, next_rising) next_time = self.schedule_next_event(hass, next_rising)
_LOGGER.info( _LOGGER.info(
'SunriseEventListener scheduled for {}, wiill call service {}.{}' 'SunriseEventListener scheduled for {}, will call service {}.{}'
.format(next_time, self._domain, self._service)) .format(next_time, self.domain, self.service))

View File

@ -16,7 +16,6 @@ from datetime import datetime, timedelta
import logging import logging
from homeassistant.components.scheduler import ServiceEventListener from homeassistant.components.scheduler import ServiceEventListener
from homeassistant.components import ATTR_ENTITY_ID
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -31,28 +30,30 @@ def create(schedule, event_listener_data):
return TimeEventListener(schedule, service, hour, minute, second) return TimeEventListener(schedule, service, hour, minute, second)
# pylint: disable=too-few-public-methods
class TimeEventListener(ServiceEventListener): class TimeEventListener(ServiceEventListener):
""" The time event that the scheduler uses """ """ The time event that the scheduler uses """
# pylint: disable=too-many-arguments
def __init__(self, schedule, service, hour, minute, second): def __init__(self, schedule, service, hour, minute, second):
ServiceEventListener.__init__(self, schedule, service) ServiceEventListener.__init__(self, schedule, service)
self._hour = hour self.hour = hour
self._minute = minute self.minute = minute
self._second = second self.second = second
def schedule(self, hass): def schedule(self, hass):
""" 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,
minute=self._minute, minute=self.minute,
second=self._second, second=self.second,
microsecond=0) microsecond=0)
# Calculate the next time the event should be executed. # Calculate the next time the event should be executed.
# That is the next day that the schedule is configured to run # That is the next day that the schedule is configured to run
while next_time < datetime.now() or \ while next_time < datetime.now() or \
next_time.weekday() not in self._schedule.days: next_time.weekday() not in self.my_schedule.days:
next_time = next_time + timedelta(days=1) next_time = next_time + timedelta(days=1)
@ -65,4 +66,4 @@ class TimeEventListener(ServiceEventListener):
_LOGGER.info( _LOGGER.info(
'TimeEventListener scheduled for {}, will call service {}.{}' 'TimeEventListener scheduled for {}, will call service {}.{}'
.format(next_time, self._domain, self._service)) .format(next_time, self.domain, self.service))