2013-12-11 08:07:30 +00:00
|
|
|
"""
|
2016-03-08 16:55:57 +00:00
|
|
|
Provides functionality to turn on lights based on the states.
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2015-11-09 12:12:18 +00:00
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/device_sun_light_trigger/
|
2013-12-11 08:07:30 +00:00
|
|
|
"""
|
2017-02-09 08:10:53 +00:00
|
|
|
import asyncio
|
2013-12-11 08:07:30 +00:00
|
|
|
import logging
|
2015-05-15 04:07:15 +00:00
|
|
|
from datetime import timedelta
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2016-09-09 07:06:24 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-11-05 23:36:20 +00:00
|
|
|
from homeassistant.core import callback
|
2015-05-15 04:07:15 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2014-12-07 07:57:02 +00:00
|
|
|
from homeassistant.const import STATE_HOME, STATE_NOT_HOME
|
2017-02-09 08:10:53 +00:00
|
|
|
from homeassistant.helpers.event import (
|
2017-05-09 07:03:34 +00:00
|
|
|
async_track_point_in_utc_time, async_track_state_change)
|
|
|
|
from homeassistant.helpers.sun import is_up, get_astral_event_next
|
2016-09-09 07:06:24 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2016-09-09 07:06:24 +00:00
|
|
|
DOMAIN = 'device_sun_light_trigger'
|
2017-05-09 07:03:34 +00:00
|
|
|
DEPENDENCIES = ['light', 'device_tracker', 'group']
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2016-09-09 07:06:24 +00:00
|
|
|
CONF_DEVICE_GROUP = 'device_group'
|
|
|
|
CONF_DISABLE_TURN_OFF = 'disable_turn_off'
|
|
|
|
CONF_LIGHT_GROUP = 'light_group'
|
|
|
|
CONF_LIGHT_PROFILE = 'light_profile'
|
2014-03-26 07:08:50 +00:00
|
|
|
|
2016-09-09 07:06:24 +00:00
|
|
|
DEFAULT_DISABLE_TURN_OFF = False
|
|
|
|
DEFAULT_LIGHT_PROFILE = 'relax'
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2016-09-09 07:06:24 +00:00
|
|
|
LIGHT_TRANSITION_TIME = timedelta(minutes=15)
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
|
|
|
vol.Optional(CONF_DEVICE_GROUP): cv.entity_id,
|
|
|
|
vol.Optional(CONF_DISABLE_TURN_OFF, default=DEFAULT_DISABLE_TURN_OFF):
|
|
|
|
cv.boolean,
|
|
|
|
vol.Optional(CONF_LIGHT_GROUP): cv.string,
|
|
|
|
vol.Optional(CONF_LIGHT_PROFILE, default=DEFAULT_LIGHT_PROFILE):
|
|
|
|
cv.string,
|
|
|
|
}),
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
2014-08-13 12:28:45 +00:00
|
|
|
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2017-02-09 08:10:53 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_setup(hass, config):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the triggers to control lights based on device presence."""
|
2016-03-06 03:56:50 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2018-05-01 18:57:30 +00:00
|
|
|
device_tracker = hass.components.device_tracker
|
|
|
|
group = hass.components.group
|
|
|
|
light = hass.components.light
|
2017-02-09 08:10:53 +00:00
|
|
|
conf = config[DOMAIN]
|
|
|
|
disable_turn_off = conf.get(CONF_DISABLE_TURN_OFF)
|
|
|
|
light_group = conf.get(CONF_LIGHT_GROUP, light.ENTITY_ID_ALL_LIGHTS)
|
|
|
|
light_profile = conf.get(CONF_LIGHT_PROFILE)
|
2017-04-30 05:04:49 +00:00
|
|
|
device_group = conf.get(
|
|
|
|
CONF_DEVICE_GROUP, device_tracker.ENTITY_ID_ALL_DEVICES)
|
|
|
|
device_entity_ids = group.get_entity_ids(
|
2018-05-01 18:57:30 +00:00
|
|
|
device_group, 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
|
2018-05-01 18:57:30 +00:00
|
|
|
light_ids = group.get_entity_ids(light_group, light.DOMAIN)
|
2014-01-05 01:55:05 +00:00
|
|
|
|
|
|
|
if not light_ids:
|
2016-09-09 07:06:24 +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():
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Calculate the time when to start fading lights in when sun sets.
|
|
|
|
|
|
|
|
Returns None if no next_setting data available.
|
2017-02-09 08:10:53 +00:00
|
|
|
|
|
|
|
Async friendly.
|
2016-03-08 16:55:57 +00:00
|
|
|
"""
|
2017-05-09 07:03:34 +00:00
|
|
|
next_setting = get_astral_event_next(hass, 'sunset')
|
2016-03-06 03:56:50 +00:00
|
|
|
if not next_setting:
|
2014-02-03 05:33:16 +00:00
|
|
|
return None
|
2016-03-06 03:56:50 +00:00
|
|
|
return next_setting - LIGHT_TRANSITION_TIME * len(light_ids)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2016-11-05 23:36:20 +00:00
|
|
|
def async_turn_on_before_sunset(light_id):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Turn on lights."""
|
2018-05-01 18:57:30 +00:00
|
|
|
if not device_tracker.is_on() or light.is_on(light_id):
|
2016-03-06 03:56:50 +00:00
|
|
|
return
|
2018-05-01 18:57:30 +00:00
|
|
|
light.async_turn_on(light_id,
|
2016-11-05 23:36:20 +00:00
|
|
|
transition=LIGHT_TRANSITION_TIME.seconds,
|
|
|
|
profile=light_profile)
|
2016-03-06 03:56:50 +00:00
|
|
|
|
2017-02-09 08:10:53 +00:00
|
|
|
def async_turn_on_factory(light_id):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Generate turn on callbacks as factory."""
|
2017-02-09 08:10:53 +00:00
|
|
|
@callback
|
|
|
|
def async_turn_on_light(now):
|
|
|
|
"""Turn on specific light."""
|
|
|
|
async_turn_on_before_sunset(light_id)
|
|
|
|
|
|
|
|
return async_turn_on_light
|
|
|
|
|
2016-03-06 03:56:50 +00:00
|
|
|
# Track every time sun rises so we can schedule a time-based
|
|
|
|
# pre-sun set event
|
2016-11-05 23:36:20 +00:00
|
|
|
@callback
|
2017-05-09 07:03:34 +00:00
|
|
|
def schedule_light_turn_on(now):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Turn on all the lights at the moment sun sets.
|
2016-03-08 16:55:57 +00:00
|
|
|
|
2016-03-07 17:49:31 +00:00
|
|
|
We will schedule to have each light start after one another
|
|
|
|
and slowly transition in.
|
|
|
|
"""
|
2016-03-06 03:56:50 +00:00
|
|
|
start_point = calc_time_for_light_when_sunset()
|
|
|
|
if not start_point:
|
|
|
|
return
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2016-03-06 03:56:50 +00:00
|
|
|
for index, light_id in enumerate(light_ids):
|
2017-05-09 07:03:34 +00:00
|
|
|
async_track_point_in_utc_time(
|
2017-02-09 08:10:53 +00:00
|
|
|
hass, async_turn_on_factory(light_id),
|
|
|
|
start_point + index * LIGHT_TRANSITION_TIME)
|
|
|
|
|
2017-05-09 07:03:34 +00:00
|
|
|
async_track_point_in_utc_time(hass, schedule_light_turn_on,
|
|
|
|
get_astral_event_next(hass, 'sunrise'))
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2016-03-07 17:49:31 +00:00
|
|
|
# If the sun is already above horizon schedule the time-based pre-sun set
|
|
|
|
# event.
|
2017-05-09 07:03:34 +00:00
|
|
|
if is_up(hass):
|
|
|
|
schedule_light_turn_on(None)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2016-11-05 23:36:20 +00:00
|
|
|
@callback
|
2017-02-09 08:10:53 +00:00
|
|
|
def check_light_on_dev_state_change(entity, old_state, new_state):
|
2016-03-06 03:56:50 +00:00
|
|
|
"""Handle tracked device state changes."""
|
2018-05-01 18:57:30 +00:00
|
|
|
lights_are_on = group.is_on(light_group)
|
2017-05-09 07:03:34 +00:00
|
|
|
light_needed = not (lights_are_on or is_up(hass))
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2016-03-06 03:56:50 +00:00
|
|
|
# These variables are needed for the elif check
|
2017-05-09 07:03:34 +00:00
|
|
|
now = dt_util.utcnow()
|
2016-03-06 03:56:50 +00:00
|
|
|
start_point = calc_time_for_light_when_sunset()
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2016-03-06 03:56:50 +00:00
|
|
|
# Do we need lights?
|
|
|
|
if light_needed:
|
|
|
|
logger.info("Home coming event for %s. Turning lights on", entity)
|
2018-05-01 18:57:30 +00:00
|
|
|
light.async_turn_on(light_ids, profile=light_profile)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2016-03-06 03:56:50 +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.
|
|
|
|
elif (start_point and
|
2017-05-09 07:03:34 +00:00
|
|
|
start_point < now < get_astral_event_next(hass, 'sunset')):
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2016-03-06 03:56:50 +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:
|
2018-05-01 18:57:30 +00:00
|
|
|
light.async_turn_on(light_id)
|
2016-03-06 03:56:50 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
# If this light didn't happen to be turned on yet so
|
|
|
|
# will all the following then, break.
|
|
|
|
break
|
|
|
|
|
2017-02-09 08:10:53 +00:00
|
|
|
async_track_state_change(
|
|
|
|
hass, device_entity_ids, check_light_on_dev_state_change,
|
|
|
|
STATE_NOT_HOME, STATE_HOME)
|
|
|
|
|
|
|
|
if disable_turn_off:
|
|
|
|
return True
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def turn_off_lights_when_all_leave(entity, old_state, new_state):
|
|
|
|
"""Handle device group state change."""
|
2018-05-01 18:57:30 +00:00
|
|
|
if not group.is_on(light_group):
|
2017-02-09 08:10:53 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
"Everyone has left but there are lights on. Turning them off")
|
2018-05-01 18:57:30 +00:00
|
|
|
light.async_turn_off(light_ids)
|
2017-02-09 08:10:53 +00:00
|
|
|
|
|
|
|
async_track_state_change(
|
|
|
|
hass, device_group, turn_off_lights_when_all_leave,
|
|
|
|
STATE_HOME, STATE_NOT_HOME)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
return True
|