2013-12-11 00:07:30 -08:00
|
|
|
"""
|
2014-01-04 17:55:05 -08:00
|
|
|
homeassistant.components.light
|
2013-12-11 00:07:30 -08:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Provides functionality to interact with lights.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
2014-03-15 00:24:28 -07:00
|
|
|
import socket
|
2013-12-11 00:07:30 -08:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
import homeassistant as ha
|
|
|
|
import homeassistant.util as util
|
2014-03-15 00:24:28 -07:00
|
|
|
from homeassistant.components import (group, STATE_ON, STATE_OFF,
|
|
|
|
SERVICE_TURN_ON, SERVICE_TURN_OFF,
|
|
|
|
ATTR_ENTITY_ID)
|
|
|
|
|
2013-12-11 00:07:30 -08:00
|
|
|
|
|
|
|
DOMAIN = "light"
|
|
|
|
|
2014-01-19 23:37:40 -08:00
|
|
|
GROUP_NAME_ALL_LIGHTS = 'all_lights'
|
|
|
|
ENTITY_ID_ALL_LIGHTS = group.ENTITY_ID_FORMAT.format(
|
|
|
|
GROUP_NAME_ALL_LIGHTS)
|
2013-12-11 00:07:30 -08:00
|
|
|
|
2014-01-19 23:37:40 -08:00
|
|
|
ENTITY_ID_FORMAT = DOMAIN + ".{}"
|
2013-12-11 00:07:30 -08:00
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
|
|
|
|
|
|
|
|
2014-01-20 23:23:02 -08:00
|
|
|
def is_on(statemachine, entity_id=None):
|
2013-12-11 00:07:30 -08:00
|
|
|
""" Returns if the lights are on based on the statemachine. """
|
2014-01-20 23:23:02 -08:00
|
|
|
entity_id = entity_id or ENTITY_ID_ALL_LIGHTS
|
2013-12-11 00:07:30 -08:00
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
return statemachine.is_state(entity_id, STATE_ON)
|
2013-12-11 00:07:30 -08:00
|
|
|
|
|
|
|
|
2014-01-20 23:23:02 -08:00
|
|
|
def turn_on(bus, entity_id=None, transition_seconds=None):
|
2013-12-11 00:07:30 -08:00
|
|
|
""" Turns all or specified light on. """
|
|
|
|
data = {}
|
|
|
|
|
2014-01-20 23:23:02 -08:00
|
|
|
if entity_id:
|
2014-03-15 00:24:28 -07:00
|
|
|
data[ATTR_ENTITY_ID] = entity_id
|
2013-12-11 00:07:30 -08:00
|
|
|
|
|
|
|
if transition_seconds:
|
|
|
|
data["transition_seconds"] = transition_seconds
|
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
bus.call_service(DOMAIN, SERVICE_TURN_ON, data)
|
2013-12-11 00:07:30 -08:00
|
|
|
|
|
|
|
|
2014-01-20 23:23:02 -08:00
|
|
|
def turn_off(bus, entity_id=None, transition_seconds=None):
|
2013-12-11 00:07:30 -08:00
|
|
|
""" Turns all or specified light off. """
|
|
|
|
data = {}
|
|
|
|
|
2014-01-20 23:23:02 -08:00
|
|
|
if entity_id:
|
2014-03-15 00:24:28 -07:00
|
|
|
data[ATTR_ENTITY_ID] = entity_id
|
2013-12-11 00:07:30 -08:00
|
|
|
|
|
|
|
if transition_seconds:
|
|
|
|
data["transition_seconds"] = transition_seconds
|
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
bus.call_service(DOMAIN, SERVICE_TURN_OFF, data)
|
2013-12-11 00:07:30 -08:00
|
|
|
|
|
|
|
|
|
|
|
def setup(bus, statemachine, light_control):
|
|
|
|
""" Exposes light control via statemachine and services. """
|
|
|
|
|
2014-01-04 17:55:05 -08:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
ent_to_light = {}
|
|
|
|
light_to_ent = {}
|
2014-01-23 22:03:13 -08:00
|
|
|
|
2013-12-11 00:07:30 -08:00
|
|
|
def update_light_state(time): # pylint: disable=unused-argument
|
|
|
|
""" Track the state of the lights. """
|
|
|
|
try:
|
|
|
|
should_update = datetime.now() - update_light_state.last_updated \
|
|
|
|
> MIN_TIME_BETWEEN_SCANS
|
|
|
|
|
|
|
|
except AttributeError: # if last_updated does not exist
|
|
|
|
should_update = True
|
|
|
|
|
|
|
|
if should_update:
|
2014-01-04 17:55:05 -08:00
|
|
|
logger.info("Updating light status")
|
2013-12-11 00:07:30 -08:00
|
|
|
update_light_state.last_updated = datetime.now()
|
2014-03-15 00:24:28 -07:00
|
|
|
names = None
|
2013-12-11 00:07:30 -08:00
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
states = light_control.get_states()
|
2013-12-11 00:07:30 -08:00
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
for light_id, is_light_on in states.items():
|
|
|
|
try:
|
|
|
|
entity_id = light_to_ent[light_id]
|
|
|
|
except KeyError:
|
|
|
|
# We have not seen this light before, set it up
|
2013-12-11 00:07:30 -08:00
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
# Load light names if not loaded this update call
|
|
|
|
if names is None:
|
|
|
|
names = light_control.get_names()
|
2013-12-11 00:07:30 -08:00
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
name = names.get(
|
|
|
|
light_id, "Unknown Light {}".format(len(ent_to_light)))
|
|
|
|
|
|
|
|
logger.info("Found new light {}".format(name))
|
2013-12-11 00:07:30 -08:00
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
entity_id = ENTITY_ID_FORMAT.format(util.slugify(name))
|
|
|
|
|
|
|
|
ent_to_light[entity_id] = light_id
|
|
|
|
light_to_ent[light_id] = entity_id
|
|
|
|
|
|
|
|
statemachine.set_state(entity_id,
|
|
|
|
STATE_ON if is_light_on else STATE_OFF)
|
|
|
|
|
|
|
|
# Update light state and discover lights for tracking the group
|
2014-01-04 17:55:05 -08:00
|
|
|
update_light_state(None)
|
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
# Track all lights in a group
|
|
|
|
group.setup(bus, statemachine,
|
|
|
|
GROUP_NAME_ALL_LIGHTS, light_to_ent.values())
|
2014-01-04 17:55:05 -08:00
|
|
|
|
|
|
|
def handle_light_service(service):
|
2013-12-11 00:07:30 -08:00
|
|
|
""" Hande a turn light on or off service call. """
|
2014-03-15 00:24:28 -07:00
|
|
|
entity_id = service.data.get(ATTR_ENTITY_ID, None)
|
2013-12-11 00:07:30 -08:00
|
|
|
transition_seconds = service.data.get("transition_seconds", None)
|
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
if service.service == SERVICE_TURN_ON:
|
|
|
|
light_control.turn_light_on(ent_to_light.get(entity_id),
|
|
|
|
transition_seconds)
|
2013-12-11 00:07:30 -08:00
|
|
|
else:
|
2014-03-15 00:24:28 -07:00
|
|
|
light_control.turn_light_off(ent_to_light.get(entity_id),
|
|
|
|
transition_seconds)
|
2013-12-11 00:07:30 -08:00
|
|
|
|
|
|
|
update_light_state(None)
|
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
# Update light state every 30 seconds
|
|
|
|
ha.track_time_change(bus, update_light_state, second=[0, 30])
|
|
|
|
|
|
|
|
# Listen for light on and light off service calls
|
|
|
|
bus.register_service(DOMAIN, SERVICE_TURN_ON,
|
2014-01-04 17:55:05 -08:00
|
|
|
handle_light_service)
|
2013-12-11 00:07:30 -08:00
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
bus.register_service(DOMAIN, SERVICE_TURN_OFF,
|
2014-01-04 17:55:05 -08:00
|
|
|
handle_light_service)
|
2013-12-11 00:07:30 -08:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class HueLightControl(object):
|
|
|
|
""" Class to interface with the Hue light system. """
|
|
|
|
|
|
|
|
def __init__(self, host=None):
|
2014-01-12 12:35:10 -08:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2013-12-11 00:07:30 -08:00
|
|
|
try:
|
|
|
|
import phue
|
|
|
|
except ImportError:
|
2014-01-12 12:35:10 -08:00
|
|
|
logger.exception(
|
|
|
|
"HueLightControl:Error while importing dependency phue.")
|
2013-12-11 00:07:30 -08:00
|
|
|
|
|
|
|
self.success_init = False
|
|
|
|
|
|
|
|
return
|
|
|
|
|
2014-01-12 12:35:10 -08:00
|
|
|
try:
|
|
|
|
self._bridge = phue.Bridge(host)
|
|
|
|
except socket.error: # Error connecting using Phue
|
|
|
|
logger.exception((
|
|
|
|
"HueLightControl:Error while connecting to the bridge. "
|
|
|
|
"Is phue registered?"))
|
|
|
|
|
|
|
|
self.success_init = False
|
|
|
|
|
|
|
|
return
|
2013-12-11 00:07:30 -08:00
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
if len(self._bridge.get_light()) == 0:
|
2014-01-12 12:35:10 -08:00
|
|
|
logger.error("HueLightControl:Could not find any lights. ")
|
|
|
|
|
|
|
|
self.success_init = False
|
|
|
|
else:
|
|
|
|
self.success_init = True
|
2013-12-11 00:07:30 -08:00
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
def get_names(self):
|
|
|
|
""" Return a dict with id mapped to name. """
|
|
|
|
try:
|
|
|
|
return {int(item[0]): item[1]['name'] for item
|
|
|
|
in self._bridge.get_light().items()}
|
2013-12-11 00:07:30 -08:00
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
except (socket.error, KeyError):
|
|
|
|
# socket.error because sometimes we cannot reach Hue
|
|
|
|
# KeyError if we got unexpected data
|
|
|
|
return {}
|
2014-01-12 12:35:10 -08:00
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
def get_states(self):
|
|
|
|
""" Return a dict with id mapped to boolean is_on. """
|
2014-01-12 12:35:10 -08:00
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
try:
|
|
|
|
# Light is on if reachable and on
|
|
|
|
return {int(itm[0]):
|
|
|
|
itm[1]['state']['reachable'] and itm[1]['state']['on']
|
|
|
|
for itm in self._bridge.get_api()['lights'].items()}
|
2014-01-12 11:29:08 -08:00
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
except (socket.error, KeyError):
|
|
|
|
# socket.error because sometimes we cannot reach Hue
|
|
|
|
# KeyError if we got unexpected data
|
|
|
|
return {}
|
2013-12-11 00:07:30 -08:00
|
|
|
|
|
|
|
def turn_light_on(self, light_id=None, transition_seconds=None):
|
|
|
|
""" Turn the specified or all lights on. """
|
|
|
|
self._turn_light(True, light_id, transition_seconds)
|
|
|
|
|
|
|
|
def turn_light_off(self, light_id=None, transition_seconds=None):
|
|
|
|
""" Turn the specified or all lights off. """
|
|
|
|
self._turn_light(False, light_id, transition_seconds)
|
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
def _turn_light(self, turn, light_id, transition_seconds):
|
2013-12-11 00:07:30 -08:00
|
|
|
""" Helper method to turn lights on or off. """
|
2014-03-15 00:24:28 -07:00
|
|
|
if turn:
|
|
|
|
command = {'on': True, 'xy': [0.5119, 0.4147], 'bri': 164}
|
2013-12-11 00:07:30 -08:00
|
|
|
else:
|
2014-03-15 00:24:28 -07:00
|
|
|
command = {'on': False}
|
2013-12-11 00:07:30 -08:00
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
if light_id is None:
|
|
|
|
light_id = [light.light_id for light in self._bridge.lights]
|
2013-12-11 00:07:30 -08:00
|
|
|
|
2014-03-15 00:24:28 -07:00
|
|
|
if transition_seconds is not None:
|
2013-12-11 00:07:30 -08:00
|
|
|
# Transition time is in 1/10th seconds and cannot exceed
|
2014-01-12 12:35:10 -08:00
|
|
|
# 900 seconds.
|
2013-12-11 00:07:30 -08:00
|
|
|
command['transitiontime'] = min(9000, transition_seconds * 10)
|
|
|
|
|
|
|
|
self._bridge.set_light(light_id, command)
|