2013-12-11 08:07:30 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.device_sun_light_trigger
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Provides functionality to turn on lights based on
|
|
|
|
the state of the sun and devices.
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
import homeassistant as ha
|
2014-01-24 06:03:13 +00:00
|
|
|
import homeassistant.util as util
|
2014-01-24 07:26:00 +00:00
|
|
|
import homeassistant.components as components
|
|
|
|
from . import light, sun, device_tracker, group
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
LIGHT_TRANSITION_TIME = timedelta(minutes=15)
|
2014-03-26 07:08:50 +00:00
|
|
|
|
|
|
|
# Light profile to be used if none given
|
|
|
|
LIGHT_PROFILE = 'relax'
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-many-branches
|
2014-03-26 07:08:50 +00:00
|
|
|
def setup(bus, statemachine,
|
|
|
|
light_group=light.GROUP_NAME_ALL_LIGHTS,
|
|
|
|
light_profile=LIGHT_PROFILE):
|
2013-12-11 08:07:30 +00:00
|
|
|
""" Triggers to turn lights on or off based on device precense. """
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2014-01-24 06:03:13 +00:00
|
|
|
device_entity_ids = util.filter_entity_ids(statemachine.entity_ids,
|
|
|
|
device_tracker.DOMAIN)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
if not device_entity_ids:
|
2014-03-26 07:08:50 +00:00
|
|
|
logger.error("No devices found to track")
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
return False
|
|
|
|
|
2014-01-05 01:55:05 +00:00
|
|
|
# Get the light IDs from the specified group
|
2014-01-24 06:03:13 +00:00
|
|
|
light_ids = util.filter_entity_ids(
|
2014-01-21 07:23:02 +00:00
|
|
|
group.get_entity_ids(statemachine, light_group), light.DOMAIN)
|
2014-01-05 01:55:05 +00:00
|
|
|
|
|
|
|
if not light_ids:
|
2014-03-26 07:08:50 +00:00
|
|
|
logger.error("No lights found to turn on ")
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
return False
|
|
|
|
|
2014-02-03 05:33:16 +00:00
|
|
|
def calc_time_for_light_when_sunset():
|
|
|
|
""" Calculates the time when to start fading lights in when sun sets.
|
|
|
|
Returns None if no next_setting data available. """
|
|
|
|
next_setting = sun.next_setting(statemachine)
|
|
|
|
|
|
|
|
if next_setting:
|
2014-04-14 07:10:24 +00:00
|
|
|
return next_setting - LIGHT_TRANSITION_TIME * len(light_ids)
|
2014-02-03 05:33:16 +00:00
|
|
|
else:
|
|
|
|
return None
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
2014-01-30 06:47:50 +00:00
|
|
|
def schedule_light_on_sun_rise(entity, old_state, new_state):
|
2013-12-11 08:07:30 +00:00
|
|
|
"""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."""
|
|
|
|
|
|
|
|
def turn_light_on_before_sunset(light_id):
|
|
|
|
""" Helper function to turn on lights slowly if there
|
|
|
|
are devices home and the light is not on yet. """
|
2014-01-24 06:03:13 +00:00
|
|
|
if (device_tracker.is_on(statemachine) and
|
2013-12-11 08:07:30 +00:00
|
|
|
not light.is_on(statemachine, light_id)):
|
|
|
|
|
2014-03-16 22:00:59 +00:00
|
|
|
light.turn_on(bus, light_id,
|
|
|
|
transition=LIGHT_TRANSITION_TIME.seconds,
|
2014-03-26 07:08:50 +00:00
|
|
|
profile=light_profile)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
def turn_on(light_id):
|
|
|
|
""" Lambda can keep track of function parameters but not local
|
|
|
|
parameters. If we put the lambda directly in the below statement
|
|
|
|
only the last light will be turned on.. """
|
|
|
|
return lambda now: turn_light_on_before_sunset(light_id)
|
|
|
|
|
2014-02-03 05:33:16 +00:00
|
|
|
start_point = calc_time_for_light_when_sunset()
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-02-03 05:33:16 +00:00
|
|
|
if start_point:
|
|
|
|
for index, light_id in enumerate(light_ids):
|
2014-02-14 19:34:09 +00:00
|
|
|
ha.track_point_in_time(bus, turn_on(light_id),
|
|
|
|
(start_point +
|
|
|
|
index * LIGHT_TRANSITION_TIME))
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
# Track every time sun rises so we can schedule a time-based
|
|
|
|
# pre-sun set event
|
2014-01-30 06:47:50 +00:00
|
|
|
ha.track_state_change(bus, sun.ENTITY_ID,
|
|
|
|
schedule_light_on_sun_rise,
|
2014-01-05 01:55:05 +00:00
|
|
|
sun.STATE_BELOW_HORIZON, sun.STATE_ABOVE_HORIZON)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
# If the sun is already above horizon
|
|
|
|
# schedule the time-based pre-sun set event
|
2014-01-24 06:03:13 +00:00
|
|
|
if sun.is_on(statemachine):
|
2014-01-30 06:47:50 +00:00
|
|
|
schedule_light_on_sun_rise(None, None, None)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-01-30 06:47:50 +00:00
|
|
|
def check_light_on_dev_state_change(entity, old_state, new_state):
|
2013-12-11 08:07:30 +00:00
|
|
|
""" Function to handle tracked device state changes. """
|
2014-01-12 19:29:30 +00:00
|
|
|
lights_are_on = group.is_on(statemachine, light_group)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-01-24 06:03:13 +00:00
|
|
|
light_needed = not (lights_are_on or sun.is_on(statemachine))
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
# Specific device came home ?
|
2014-01-20 07:37:40 +00:00
|
|
|
if (entity != device_tracker.ENTITY_ID_ALL_DEVICES and
|
2014-01-24 07:26:00 +00:00
|
|
|
new_state.state == components.STATE_HOME):
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
# These variables are needed for the elif check
|
|
|
|
now = datetime.now()
|
2014-02-03 05:33:16 +00:00
|
|
|
start_point = calc_time_for_light_when_sunset()
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
# Do we need lights?
|
|
|
|
if light_needed:
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
"Home coming event for {}. Turning lights on".
|
2014-01-20 07:37:40 +00:00
|
|
|
format(entity))
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-04-13 19:59:45 +00:00
|
|
|
light.turn_on(bus, light_ids,
|
|
|
|
profile=light_profile)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
# Are we in the time span were we would turn on the lights
|
|
|
|
# if someone would be home?
|
|
|
|
# Check this by seeing if current time is later then the point
|
|
|
|
# in time when we would start putting the lights on.
|
2014-02-03 05:33:16 +00:00
|
|
|
elif (start_point and
|
|
|
|
start_point < now < sun.next_setting(statemachine)):
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
# Check for every light if it would be on if someone was home
|
|
|
|
# when the fading in started and turn it on if so
|
|
|
|
for index, light_id in enumerate(light_ids):
|
|
|
|
|
|
|
|
if now > start_point + index * LIGHT_TRANSITION_TIME:
|
|
|
|
light.turn_on(bus, light_id)
|
|
|
|
|
|
|
|
else:
|
|
|
|
# If this light didn't happen to be turned on yet so
|
|
|
|
# will all the following then, break.
|
|
|
|
break
|
|
|
|
|
|
|
|
# Did all devices leave the house?
|
2014-01-20 07:37:40 +00:00
|
|
|
elif (entity == device_tracker.ENTITY_ID_ALL_DEVICES and
|
2014-01-24 07:26:00 +00:00
|
|
|
new_state.state == components.STATE_NOT_HOME and lights_are_on):
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
logger.info(
|
|
|
|
"Everyone has left but there are devices on. Turning them off")
|
|
|
|
|
2014-01-24 07:52:38 +00:00
|
|
|
light.turn_off(bus)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
# Track home coming of each seperate device
|
2014-01-20 07:37:40 +00:00
|
|
|
for entity in device_entity_ids:
|
2014-01-30 06:47:50 +00:00
|
|
|
ha.track_state_change(bus, entity, check_light_on_dev_state_change,
|
2014-01-24 07:26:00 +00:00
|
|
|
components.STATE_NOT_HOME, components.STATE_HOME)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
# Track when all devices are gone to shut down lights
|
2014-01-20 07:37:40 +00:00
|
|
|
ha.track_state_change(bus, device_tracker.ENTITY_ID_ALL_DEVICES,
|
2014-01-30 06:47:50 +00:00
|
|
|
check_light_on_dev_state_change,
|
|
|
|
components.STATE_HOME, components.STATE_NOT_HOME)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
return True
|