hass-core/homeassistant/actors.py

139 lines
5.5 KiB
Python
Raw Normal View History

"""
homeassistant.actors
~~~~~~~~~~~~~~~~~~~~
This module provides actors that will react to events happening within homeassistant.
"""
2013-09-21 17:59:31 -07:00
import logging
from datetime import timedelta
from phue import Bridge
2013-09-21 17:59:31 -07:00
2013-09-30 00:20:27 -07:00
from . import track_state_change
from .observers import (STATE_CATEGORY_SUN, SUN_STATE_BELOW_HORIZON, SUN_STATE_ABOVE_HORIZON,
STATE_CATEGORY_ALL_DEVICES, DEVICE_STATE_HOME, DEVICE_STATE_NOT_HOME,
track_time_change)
2013-09-21 17:59:31 -07:00
LIGHT_TRANSITION_TIME = timedelta(minutes=15)
HUE_MAX_TRANSITION_TIME = 9000
def _hue_process_transition_time(transition_seconds):
""" Transition time is in 1/10th seconds and cannot exceed MAX_TRANSITION_TIME. """
return min(HUE_MAX_TRANSITION_TIME, transition_seconds * 10)
2013-09-21 17:59:31 -07:00
class LightTrigger(object):
""" Class to turn on lights based on available devices and state of the sun. """
def __init__(self, eventbus, statemachine, weather, device_tracker, light_control):
2013-09-21 17:59:31 -07:00
self.eventbus = eventbus
self.statemachine = statemachine
self.weather = weather
self.light_control = light_control
self.logger = logging.getLogger(__name__)
# Track home coming of each seperate device
for category in device_tracker.device_state_categories():
track_state_change(eventbus, category, DEVICE_STATE_NOT_HOME, DEVICE_STATE_HOME, self._handle_device_state_change)
2013-09-21 17:59:31 -07:00
# Track when all devices are gone to shut down lights
track_state_change(eventbus, STATE_CATEGORY_ALL_DEVICES, DEVICE_STATE_HOME, DEVICE_STATE_NOT_HOME, self._handle_device_state_change)
2013-09-21 17:59:31 -07:00
# Track every time sun rises so we can schedule a time-based pre-sun set event
track_state_change(eventbus, STATE_CATEGORY_SUN, SUN_STATE_BELOW_HORIZON, SUN_STATE_ABOVE_HORIZON, self._handle_sun_rising)
# If the sun is already above horizon schedule the time-based pre-sun set event
if statemachine.is_state(STATE_CATEGORY_SUN, SUN_STATE_ABOVE_HORIZON):
self._handle_sun_rising(None, None, None)
def _handle_sun_rising(self, category, old_state, new_state):
"""The moment sun sets we want to have all the lights on.
We will schedule to have each light start after one another
and slowly transition in."""
start_point = self.weather.next_sun_setting() - LIGHT_TRANSITION_TIME * len(self.light_control.light_ids)
def turn_on(light_id):
""" Lambda can keep track of function parameters, not from local parameters
If we put the lambda directly in the below statement only the last light
would be turned on.. """
2013-09-21 17:59:31 -07:00
return lambda now: self._turn_light_on_before_sunset(light_id)
for index, light_id in enumerate(self.light_control.light_ids):
track_time_change(self.eventbus, turn_on(light_id),
point_in_time=start_point + index * LIGHT_TRANSITION_TIME)
def _turn_light_on_before_sunset(self, light_id=None):
""" Helper function to turn on lights slowly if there are devices home and the light is not on yet. """
if self.statemachine.is_state(STATE_CATEGORY_ALL_DEVICES, DEVICE_STATE_HOME) and not self.light_control.is_light_on(light_id):
2013-09-21 17:59:31 -07:00
self.light_control.turn_light_on(light_id, LIGHT_TRANSITION_TIME.seconds)
def _handle_device_state_change(self, category, old_state, new_state):
""" Function to handle tracked device state changes. """
lights_are_on = self.light_control.is_light_on()
light_needed = not lights_are_on and self.statemachine.is_state(STATE_CATEGORY_SUN, SUN_STATE_BELOW_HORIZON)
# Specific device came home ?
if category != STATE_CATEGORY_ALL_DEVICES and new_state.state == DEVICE_STATE_HOME and light_needed:
2013-09-21 17:59:31 -07:00
self.logger.info("Home coming event for {}. Turning lights on".format(category))
self.light_control.turn_light_on()
# Did all devices leave the house?
elif category == STATE_CATEGORY_ALL_DEVICES and new_state.state == DEVICE_STATE_NOT_HOME and lights_are_on:
2013-09-21 17:59:31 -07:00
self.logger.info("Everyone has left but lights are on. Turning lights off")
self.light_control.turn_light_off()
class HueLightControl(object):
""" Class to interface with the Hue light system. """
def __init__(self, host=None):
self.bridge = Bridge(host)
self.lights = self.bridge.get_light_objects()
self.light_ids = [light.light_id for light in self.lights]
def is_light_on(self, light_id=None):
""" Returns if specified or all light are on. """
if light_id is None:
return sum([1 for light in self.lights if light.on]) > 0
else:
return self.bridge.get_light(light_id, 'on')
def turn_light_on(self, light_id=None, transition_seconds=None):
""" Turn the specified or all lights on. """
if light_id is None:
light_id = self.light_ids
command = {'on': True, 'xy': [0.5119, 0.4147], 'bri':164}
if transition_seconds is not None:
command['transitiontime'] = _hue_process_transition_time(transition_seconds)
self.bridge.set_light(light_id, command)
def turn_light_off(self, light_id=None, transition_seconds=None):
""" Turn the specified or all lights off. """
if light_id is None:
light_id = self.light_ids
command = {'on': False}
if transition_seconds is not None:
command['transitiontime'] = _hue_process_transition_time(transition_seconds)
self.bridge.set_light(light_id, command)