Many updates regarding event decorators

1. Added HASS to the arguments for callbacks that are created with
event decorators.
2. Added a service decorator.
3. Updated example.py in the example config to use the event decorators.
This commit is contained in:
Ryan Kraus 2016-01-24 19:52:22 -05:00
parent 40dbeb0b60
commit 57725136c0
2 changed files with 104 additions and 76 deletions

View file

@ -1,16 +1,36 @@
""" Event Decorators for custom components """
import functools
from homeassistant.helpers import event
HASS = None
def _callback(action, *args, **kwargs):
""" adds HASS to callback arguments """
action(HASS, *args, **kwargs)
def service(domain, service):
""" Decorator factory to register a service """
def register_service_decorator(action):
""" Decorator to register a service """
HASS.services.register(domain, service,
functools.partial(_callback, action))
return action
return register_service_decorator
def track_state_change(entity_ids, from_state=None, to_state=None):
""" Decorator factory to track state changes for entity id """
def track_state_change_decorator(action):
""" Decorator to track state changes """
event.track_state_change(HASS, entity_ids, action,
event.track_state_change(HASS, entity_ids,
functools.partial(_callback, action),
from_state, to_state)
return action
@ -22,7 +42,9 @@ def track_sunrise(offset=None):
def track_sunrise_decorator(action):
""" Decorator to track sunrise events """
event.track_sunrise(HASS, action, offset)
event.track_sunrise(HASS,
functools.partial(_callback, action),
action, offset)
return action
return track_sunrise_decorator
@ -33,7 +55,9 @@ def track_sunset(offset=None):
def track_sunset_decorator(action):
""" Decorator to track sunset events """
event.track_sunset(HASS, action, offset)
event.track_sunset(HASS,
functools.partial(_callback, action),
offset)
return action
return track_sunset_decorator
@ -46,8 +70,9 @@ def track_time_change(year=None, month=None, day=None, hour=None, minute=None,
def track_time_change_decorator(action):
""" Decorator to track time changes """
event.track_time_change(HASS, action, year, month, day, hour,
minute, second)
event.track_time_change(HASS,
functools.partial(_callback, action),
year, month, day, hour, minute, second)
return action
return track_time_change_decorator