1
mirror of https://github.com/home-assistant/core synced 2024-09-12 15:16:21 +02:00
ha-core/homeassistant/components/demo.py

136 lines
4.3 KiB
Python
Raw Normal View History

2014-11-02 18:41:41 +01:00
"""
homeassistant.components.demo
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sets up a demo environment that mimics interaction with devices
"""
import time
2014-11-02 18:41:41 +01:00
import homeassistant as ha
import homeassistant.bootstrap as bootstrap
import homeassistant.loader as loader
from homeassistant.const import (
CONF_PLATFORM, ATTR_ENTITY_PICTURE, ATTR_ENTITY_ID,
CONF_LATITUDE, CONF_LONGITUDE)
2014-11-02 18:41:41 +01:00
DOMAIN = "demo"
DEPENDENCIES = []
COMPONENTS_WITH_DEMO_PLATFORM = [
'switch', 'light', 'thermostat', 'sensor', 'media_player', 'notify']
2014-11-02 18:41:41 +01:00
def setup(hass, config):
""" Setup a demo environment. """
group = loader.get_component('group')
configurator = loader.get_component('configurator')
2014-11-02 18:41:41 +01:00
2014-11-29 07:49:29 +01:00
config.setdefault(ha.DOMAIN, {})
config.setdefault(DOMAIN, {})
if config[DOMAIN].get('hide_demo_state') != 1:
2014-11-02 18:41:41 +01:00
hass.states.set('a.Demo_Mode', 'Enabled')
# Setup sun
config[ha.DOMAIN].setdefault(CONF_LATITUDE, '32.87336')
config[ha.DOMAIN].setdefault(CONF_LONGITUDE, '-117.22743')
loader.get_component('sun').setup(hass, config)
2014-11-02 18:41:41 +01:00
# Setup demo platforms
for component in COMPONENTS_WITH_DEMO_PLATFORM:
bootstrap.setup_component(
hass, component, {component: {CONF_PLATFORM: 'demo'}})
2014-11-02 18:41:41 +01:00
# Setup room groups
lights = hass.states.entity_ids('light')
switches = hass.states.entity_ids('switch')
group.setup_group(hass, 'living room', [lights[0], lights[1], switches[0]])
group.setup_group(hass, 'bedroom', [lights[2], switches[1]])
2014-11-02 18:41:41 +01:00
# Setup scripts
bootstrap.setup_component(
hass, 'script',
{'script':
{'demo': {
'alias': 'Demo {}'.format(lights[0]),
'sequence': [{
'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-17 06:35:57 +01:00
# Setup scenes
bootstrap.setup_component(
hass, 'scene',
{'scene': [
{'name': 'Romantic lights',
'entities': {
lights[0]: True,
lights[1]: {'state': 'on', 'xy_color': [0.33, 0.66],
'brightness': 200},
}},
{'name': 'Switch on and off',
'entities': {
switches[0]: True,
switches[1]: False,
}},
]
})
# Setup fake device tracker
hass.states.set("device_tracker.paulus", "home",
2014-11-02 20:22:22 +01:00
{ATTR_ENTITY_PICTURE:
"http://graph.facebook.com/schoutsen/picture"})
hass.states.set("device_tracker.anne_therese", "not_home",
2014-11-02 20:22:22 +01:00
{ATTR_ENTITY_PICTURE:
"http://graph.facebook.com/anne.t.frederiksen/picture"})
2014-11-02 18:41:41 +01:00
hass.states.set("group.all_devices", "home",
{
"auto": True,
ATTR_ENTITY_ID: [
2014-11-02 18:41:41 +01:00
"device_tracker.Paulus",
"device_tracker.Anne_Therese"
]
})
# 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-20 06:39:24 +01:00
configurator_ids[0],
"Failed to register, please try again.")
configurator_ids.append(0)
else:
2015-01-20 06:39:24 +01: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 18:41:41 +01:00
return True