2014-11-02 17:41:41 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.demo
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Sets up a demo environment that mimics interaction with devices
|
|
|
|
"""
|
|
|
|
import random
|
|
|
|
|
|
|
|
import homeassistant as ha
|
2014-11-05 07:34:19 +00:00
|
|
|
import homeassistant.loader as loader
|
2014-12-07 07:57:02 +00:00
|
|
|
from homeassistant.helpers import extract_entity_ids
|
|
|
|
from homeassistant.const import (
|
2015-01-12 05:21:18 +00:00
|
|
|
SERVICE_TURN_ON, SERVICE_TURN_OFF,
|
|
|
|
STATE_ON, STATE_OFF, TEMP_CELCIUS,
|
|
|
|
ATTR_ENTITY_PICTURE, ATTR_ENTITY_ID, ATTR_UNIT_OF_MEASUREMENT,
|
|
|
|
CONF_LATITUDE, CONF_LONGITUDE)
|
2014-11-29 06:49:29 +00:00
|
|
|
from homeassistant.components.light import (
|
2014-12-30 16:41:42 +00:00
|
|
|
ATTR_XY_COLOR, ATTR_RGB_COLOR, ATTR_BRIGHTNESS, GROUP_NAME_ALL_LIGHTS)
|
2015-01-06 08:11:03 +00:00
|
|
|
from homeassistant.components.thermostat import (
|
2015-01-12 05:21:18 +00:00
|
|
|
ATTR_CURRENT_TEMPERATURE, ATTR_AWAY_MODE)
|
2014-12-30 16:41:42 +00:00
|
|
|
from homeassistant.util import split_entity_id, color_RGB_to_xy
|
2014-11-02 17:41:41 +00:00
|
|
|
|
|
|
|
DOMAIN = "demo"
|
|
|
|
|
|
|
|
DEPENDENCIES = []
|
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
|
|
|
""" Setup a demo environment. """
|
2014-11-05 07:34:19 +00:00
|
|
|
group = loader.get_component('group')
|
2014-11-02 17:41:41 +00:00
|
|
|
|
2014-11-29 06:49:29 +00:00
|
|
|
config.setdefault(ha.DOMAIN, {})
|
|
|
|
config.setdefault(DOMAIN, {})
|
|
|
|
|
2014-11-02 17:41:41 +00:00
|
|
|
if config[DOMAIN].get('hide_demo_state') != '1':
|
|
|
|
hass.states.set('a.Demo_Mode', 'Enabled')
|
|
|
|
|
|
|
|
light_colors = [
|
|
|
|
[0.861, 0.3259],
|
|
|
|
[0.6389, 0.3028],
|
|
|
|
[0.1684, 0.0416]
|
|
|
|
]
|
|
|
|
|
|
|
|
def mock_turn_on(service):
|
|
|
|
""" Will fake the component has been turned on. """
|
2014-11-29 06:49:29 +00:00
|
|
|
if service.data and ATTR_ENTITY_ID in service.data:
|
|
|
|
entity_ids = extract_entity_ids(hass, service)
|
|
|
|
else:
|
2014-11-29 07:19:59 +00:00
|
|
|
entity_ids = hass.states.entity_ids(service.domain)
|
2014-11-29 06:49:29 +00:00
|
|
|
|
|
|
|
for entity_id in entity_ids:
|
2014-11-02 17:41:41 +00:00
|
|
|
domain, _ = split_entity_id(entity_id)
|
|
|
|
|
|
|
|
if domain == "light":
|
2014-12-30 16:41:42 +00:00
|
|
|
rgb_color = service.data.get(ATTR_RGB_COLOR)
|
|
|
|
|
2014-12-31 02:36:55 +00:00
|
|
|
if rgb_color:
|
2014-12-30 16:41:42 +00:00
|
|
|
color = color_RGB_to_xy(
|
|
|
|
rgb_color[0], rgb_color[1], rgb_color[2])
|
|
|
|
|
2014-12-31 02:36:55 +00:00
|
|
|
else:
|
|
|
|
cur_state = hass.states.get(entity_id)
|
|
|
|
|
|
|
|
# Use current color if available
|
|
|
|
if cur_state and cur_state.attributes.get(ATTR_XY_COLOR):
|
|
|
|
color = cur_state.attributes.get(ATTR_XY_COLOR)
|
|
|
|
else:
|
|
|
|
color = random.choice(light_colors)
|
|
|
|
|
2014-12-30 16:41:42 +00:00
|
|
|
data = {
|
|
|
|
ATTR_BRIGHTNESS: service.data.get(ATTR_BRIGHTNESS, 200),
|
|
|
|
ATTR_XY_COLOR: color
|
|
|
|
}
|
2014-11-02 17:41:41 +00:00
|
|
|
else:
|
|
|
|
data = None
|
|
|
|
|
|
|
|
hass.states.set(entity_id, STATE_ON, data)
|
|
|
|
|
|
|
|
def mock_turn_off(service):
|
|
|
|
""" Will fake the component has been turned off. """
|
2014-11-29 06:49:29 +00:00
|
|
|
if service.data and ATTR_ENTITY_ID in service.data:
|
|
|
|
entity_ids = extract_entity_ids(hass, service)
|
|
|
|
else:
|
2014-11-29 07:19:59 +00:00
|
|
|
entity_ids = hass.states.entity_ids(service.domain)
|
2014-11-29 06:49:29 +00:00
|
|
|
|
|
|
|
for entity_id in entity_ids:
|
2014-11-02 17:41:41 +00:00
|
|
|
hass.states.set(entity_id, STATE_OFF)
|
|
|
|
|
|
|
|
# Setup sun
|
2014-12-07 07:57:02 +00:00
|
|
|
if CONF_LATITUDE not in config[ha.DOMAIN]:
|
|
|
|
config[ha.DOMAIN][CONF_LATITUDE] = '32.87336'
|
2014-11-02 17:41:41 +00:00
|
|
|
|
2014-12-07 07:57:02 +00:00
|
|
|
if CONF_LONGITUDE not in config[ha.DOMAIN]:
|
|
|
|
config[ha.DOMAIN][CONF_LONGITUDE] = '-117.22743'
|
2014-11-02 17:41:41 +00:00
|
|
|
|
2014-11-05 07:34:19 +00:00
|
|
|
loader.get_component('sun').setup(hass, config)
|
2014-11-02 17:41:41 +00:00
|
|
|
|
|
|
|
# Setup fake lights
|
|
|
|
lights = ['light.Bowl', 'light.Ceiling', 'light.TV_Back_light',
|
|
|
|
'light.Bed_light']
|
|
|
|
|
|
|
|
hass.services.register('light', SERVICE_TURN_ON, mock_turn_on)
|
|
|
|
hass.services.register('light', SERVICE_TURN_OFF, mock_turn_off)
|
|
|
|
|
|
|
|
mock_turn_on(ha.ServiceCall('light', SERVICE_TURN_ON,
|
|
|
|
{'entity_id': lights[0:2]}))
|
|
|
|
mock_turn_off(ha.ServiceCall('light', SERVICE_TURN_OFF,
|
|
|
|
{'entity_id': lights[2:]}))
|
|
|
|
|
|
|
|
group.setup_group(hass, GROUP_NAME_ALL_LIGHTS, lights, False)
|
|
|
|
|
2014-11-09 01:20:43 +00:00
|
|
|
# Setup switch
|
|
|
|
switches = ['switch.AC', 'switch.Christmas_Lights']
|
2014-11-02 17:41:41 +00:00
|
|
|
|
2014-11-09 01:20:43 +00:00
|
|
|
hass.services.register('switch', SERVICE_TURN_ON, mock_turn_on)
|
|
|
|
hass.services.register('switch', SERVICE_TURN_OFF, mock_turn_off)
|
2014-11-02 17:41:41 +00:00
|
|
|
|
2014-11-09 01:20:43 +00:00
|
|
|
mock_turn_on(ha.ServiceCall('switch', SERVICE_TURN_ON,
|
|
|
|
{'entity_id': switches[0:1]}))
|
|
|
|
mock_turn_off(ha.ServiceCall('switch', SERVICE_TURN_OFF,
|
|
|
|
{'entity_id': switches[1:]}))
|
2014-11-02 17:41:41 +00:00
|
|
|
|
|
|
|
# Setup room groups
|
2014-11-09 01:20:43 +00:00
|
|
|
group.setup_group(hass, 'living_room', lights[0:3] + switches[0:1])
|
|
|
|
group.setup_group(hass, 'bedroom', [lights[3]] + switches[1:])
|
2014-11-02 17:41:41 +00:00
|
|
|
|
|
|
|
# Setup process
|
|
|
|
hass.states.set("process.XBMC", STATE_ON)
|
|
|
|
|
|
|
|
# Setup device tracker
|
2014-11-02 19:22:22 +00:00
|
|
|
hass.states.set("device_tracker.Paulus", "home",
|
|
|
|
{ATTR_ENTITY_PICTURE:
|
|
|
|
"http://graph.facebook.com/schoutsen/picture"})
|
|
|
|
hass.states.set("device_tracker.Anne_Therese", "not_home",
|
|
|
|
{ATTR_ENTITY_PICTURE:
|
|
|
|
"http://graph.facebook.com/anne.t.frederiksen/picture"})
|
|
|
|
|
2014-11-02 17:41:41 +00:00
|
|
|
hass.states.set("group.all_devices", "home",
|
|
|
|
{
|
|
|
|
"auto": True,
|
|
|
|
"entity_id": [
|
|
|
|
"device_tracker.Paulus",
|
|
|
|
"device_tracker.Anne_Therese"
|
|
|
|
]
|
|
|
|
})
|
|
|
|
|
|
|
|
# Setup chromecast
|
2015-01-02 04:03:24 +00:00
|
|
|
hass.states.set("chromecast.Living_Rm", "Plex",
|
2014-11-20 07:42:57 +00:00
|
|
|
{'friendly_name': 'Living Room',
|
|
|
|
ATTR_ENTITY_PICTURE:
|
|
|
|
'http://graph.facebook.com/KillBillMovie/picture'})
|
2014-11-02 17:41:41 +00:00
|
|
|
|
2014-11-11 05:49:35 +00:00
|
|
|
# Setup tellstick sensors
|
|
|
|
hass.states.set("tellstick_sensor.Outside_temperature", "15.6",
|
|
|
|
{
|
|
|
|
'friendly_name': 'Outside temperature',
|
|
|
|
'unit_of_measurement': '°C'
|
|
|
|
})
|
|
|
|
hass.states.set("tellstick_sensor.Outside_humidity", "54",
|
|
|
|
{
|
|
|
|
'friendly_name': 'Outside humidity',
|
2014-11-11 21:35:06 +00:00
|
|
|
'unit_of_measurement': '%'
|
2014-11-11 05:49:35 +00:00
|
|
|
})
|
|
|
|
|
2015-01-06 08:11:03 +00:00
|
|
|
# Nest demo
|
2015-01-12 05:21:18 +00:00
|
|
|
hass.states.set("thermostat.Nest", "23",
|
2015-01-06 08:11:03 +00:00
|
|
|
{
|
2015-01-12 05:21:18 +00:00
|
|
|
ATTR_UNIT_OF_MEASUREMENT: TEMP_CELCIUS,
|
|
|
|
ATTR_CURRENT_TEMPERATURE: '18',
|
2015-01-06 08:11:03 +00:00
|
|
|
ATTR_AWAY_MODE: STATE_OFF
|
|
|
|
})
|
|
|
|
|
2014-11-02 17:41:41 +00:00
|
|
|
return True
|