hass-core/homeassistant/components/demo.py

144 lines
4.2 KiB
Python
Raw Normal View History

2014-11-02 09:41:41 -08:00
"""
homeassistant.components.demo
2015-05-13 19:18:30 +02:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2014-11-02 09:41:41 -08:00
2015-05-13 19:18:30 +02:00
Sets up a demo environment that mimics interaction with devices.
2014-11-02 09:41:41 -08:00
"""
import time
2014-11-02 09:41:41 -08:00
2015-08-16 20:44:46 -07:00
import homeassistant.core as ha
import homeassistant.bootstrap as bootstrap
import homeassistant.loader as loader
from homeassistant.const import (
2015-11-03 00:19:28 -08:00
CONF_PLATFORM, ATTR_ENTITY_ID)
2014-11-02 09:41:41 -08:00
DOMAIN = "demo"
2015-11-03 00:19:28 -08:00
DEPENDENCIES = ['conversation', 'introduction', 'zone']
2014-11-02 09:41:41 -08:00
COMPONENTS_WITH_DEMO_PLATFORM = [
2015-12-05 01:12:38 -08:00
'alarm_control_panel',
'binary_sensor',
'camera',
'device_tracker',
2016-02-11 08:23:04 -05:00
'garage_door',
2015-12-05 01:12:38 -08:00
'light',
'lock',
'media_player',
'notify',
'rollershutter',
'sensor',
'switch',
'thermostat',
]
2014-11-02 09:41:41 -08:00
def setup(hass, config):
""" Setup a demo environment. """
group = loader.get_component('group')
configurator = loader.get_component('configurator')
2014-11-02 09:41:41 -08:00
2014-11-28 22:49:29 -08:00
config.setdefault(ha.DOMAIN, {})
config.setdefault(DOMAIN, {})
if config[DOMAIN].get('hide_demo_state') != 1:
2014-11-02 09:41:41 -08:00
hass.states.set('a.Demo_Mode', 'Enabled')
# Setup sun
2015-04-25 17:44:05 -07:00
if not hass.config.latitude:
hass.config.latitude = 32.87336
2015-04-25 17:44:05 -07:00
if not hass.config.longitude:
hass.config.longitude = 117.22743
2015-04-25 17:44:05 -07:00
bootstrap.setup_component(hass, 'sun')
2014-11-02 09:41:41 -08:00
# Setup demo platforms
demo_config = config.copy()
for component in COMPONENTS_WITH_DEMO_PLATFORM:
demo_config[component] = {CONF_PLATFORM: 'demo'}
bootstrap.setup_component(hass, component, demo_config)
2014-11-02 09:41:41 -08:00
# Setup room groups
2015-08-30 13:48:40 -07:00
lights = sorted(hass.states.entity_ids('light'))
switches = sorted(hass.states.entity_ids('switch'))
2015-08-23 19:46:18 -07:00
media_players = sorted(hass.states.entity_ids('media_player'))
2016-01-25 21:27:36 -08:00
group.Group(hass, 'living room', [
lights[2], lights[1], switches[0], media_players[1],
'scene.romantic_lights'])
2016-01-24 14:13:39 -08:00
group.Group(hass, 'bedroom', [lights[0], switches[1],
media_players[0]])
2016-01-25 21:27:36 -08:00
group.Group(hass, 'Rooms', [
'group.living_room', 'group.bedroom',
'scene.romantic_lights', 'rollershutter.kitchen_window',
'rollershutter.living_room_window',
], view=True)
# Setup scripts
bootstrap.setup_component(
hass, 'script',
2015-03-16 22:45:42 -07:00
{'script': {
'demo': {
2015-08-30 13:48:40 -07:00
'alias': 'Toggle {}'.format(lights[0].split('.')[1]),
'sequence': [{
2015-03-16 22:45:42 -07:00
'execute_service': 'light.turn_off',
'service_data': {ATTR_ENTITY_ID: lights[0]}
}, {
'delay': {'seconds': 5}
}, {
'execute_service': 'light.turn_on',
'service_data': {ATTR_ENTITY_ID: lights[0]}
}, {
'delay': {'seconds': 5}
}, {
'execute_service': 'light.turn_off',
'service_data': {ATTR_ENTITY_ID: lights[0]}
}]
}}})
2015-03-16 22:35:57 -07:00
# Setup scenes
bootstrap.setup_component(
hass, 'scene',
{'scene': [
{'name': 'Romantic lights',
'entities': {
2015-03-16 22:45:42 -07:00
lights[0]: True,
lights[1]: {'state': 'on', 'xy_color': [0.33, 0.66],
'brightness': 200},
2015-03-16 22:35:57 -07:00
}},
{'name': 'Switch on and off',
'entities': {
2015-03-16 22:45:42 -07:00
switches[0]: True,
switches[1]: False,
2015-03-16 22:35:57 -07:00
}},
2015-03-16 22:45:42 -07:00
]})
2015-03-16 22:35:57 -07:00
# Setup configurator
configurator_ids = []
def hue_configuration_callback(data):
""" Fake callback, mark config as done. """
time.sleep(2)
# First time it is called, pretend it failed.
if len(configurator_ids) == 1:
configurator.notify_errors(
2015-01-19 21:39:24 -08:00
configurator_ids[0],
"Failed to register, please try again.")
configurator_ids.append(0)
else:
2015-01-19 21:39:24 -08:00
configurator.request_done(configurator_ids[0])
request_id = configurator.request_config(
hass, "Philips Hue", hue_configuration_callback,
description=("Press the button on the bridge to register Philips Hue "
"with Home Assistant."),
description_image="/static/images/config_philips_hue.jpg",
submit_caption="I have pressed the button"
)
configurator_ids.append(request_id)
2014-11-02 09:41:41 -08:00
return True