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

196 lines
6.5 KiB
Python
Raw Normal View History

2014-11-02 18:41:41 +01:00
"""
2015-05-13 19:18:30 +02:00
Sets up a demo environment that mimics interaction with devices.
For more details about this component, please refer to the documentation
https://home-assistant.io/components/demo/
2014-11-02 18:41:41 +01:00
"""
import time
2014-11-02 18:41:41 +01:00
import homeassistant.bootstrap as bootstrap
2016-02-19 06:27:50 +01:00
import homeassistant.core as ha
import homeassistant.loader as loader
2016-02-19 06:27:50 +01:00
from homeassistant.const import ATTR_ENTITY_ID, CONF_PLATFORM
2014-11-02 18:41:41 +01:00
2015-11-03 09:19:28 +01:00
DEPENDENCIES = ['conversation', 'introduction', 'zone']
DOMAIN = 'demo'
2014-11-02 18:41:41 +01:00
COMPONENTS_WITH_DEMO_PLATFORM = [
2015-12-05 10:12:38 +01:00
'alarm_control_panel',
'binary_sensor',
'calendar',
2015-12-05 10:12:38 +01:00
'camera',
2016-08-21 08:44:31 +02:00
'climate',
'cover',
2015-12-05 10:12:38 +01:00
'device_tracker',
'fan',
2015-12-05 10:12:38 +01:00
'light',
'lock',
'media_player',
'notify',
'sensor',
'switch',
]
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')
persistent_notification = loader.get_component('persistent_notification')
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
2015-04-26 02:44:05 +02:00
if not hass.config.latitude:
hass.config.latitude = 32.87336
2015-04-26 02:44:05 +02:00
if not hass.config.longitude:
hass.config.longitude = 117.22743
2015-04-26 02:44:05 +02:00
bootstrap.setup_component(hass, 'sun')
2014-11-02 18:41:41 +01: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 18:41:41 +01:00
# Setup example persistent notification
persistent_notification.create(
hass, 'This is an example of a persistent notification.',
title='Example Notification')
2014-11-02 18:41:41 +01:00
# Setup room groups
2015-08-30 22:48:40 +02:00
lights = sorted(hass.states.entity_ids('light'))
switches = sorted(hass.states.entity_ids('switch'))
2015-08-24 04:46:18 +02:00
media_players = sorted(hass.states.entity_ids('media_player'))
group.Group.create_group(hass, 'living room', [
2016-02-27 03:12:03 +01:00
lights[1], switches[0], 'input_select.living_room_preset',
'rollershutter.living_room_window', media_players[1],
'scene.romantic_lights'])
group.Group.create_group(hass, 'bedroom', [
2016-06-10 08:27:35 +02:00
lights[0], switches[1], media_players[0],
'input_slider.noise_allowance'])
group.Group.create_group(hass, 'kitchen', [
2016-02-27 03:12:03 +01:00
lights[2], 'rollershutter.kitchen_window', 'lock.kitchen_door'])
group.Group.create_group(hass, 'doors', [
2016-02-27 03:12:03 +01:00
'lock.front_door', 'lock.kitchen_door',
'garage_door.right_garage_door', 'garage_door.left_garage_door'])
group.Group.create_group(hass, 'automations', [
2016-02-27 03:12:03 +01:00
'input_select.who_cooks', 'input_boolean.notify', ])
group.Group.create_group(hass, 'people', [
2016-02-27 03:12:03 +01:00
'device_tracker.demo_anne_therese', 'device_tracker.demo_home_boy',
'device_tracker.demo_paulus'])
group.Group.create_group(hass, 'downstairs', [
2016-02-27 03:12:03 +01:00
'group.living_room', 'group.kitchen',
'scene.romantic_lights', 'rollershutter.kitchen_window',
'rollershutter.living_room_window', 'group.doors',
'thermostat.ecobee',
2016-01-26 06:27:36 +01:00
], view=True)
# Setup scripts
bootstrap.setup_component(
hass, 'script',
2015-03-17 06:45:42 +01:00
{'script': {
'demo': {
2015-08-30 22:48:40 +02:00
'alias': 'Toggle {}'.format(lights[0].split('.')[1]),
'sequence': [{
'service': 'light.turn_off',
'data': {ATTR_ENTITY_ID: lights[0]}
2015-03-17 06:45:42 +01:00
}, {
'delay': {'seconds': 5}
}, {
'service': 'light.turn_on',
'data': {ATTR_ENTITY_ID: lights[0]}
2015-03-17 06:45:42 +01:00
}, {
'delay': {'seconds': 5}
}, {
'service': 'light.turn_off',
'data': {ATTR_ENTITY_ID: lights[0]}
2015-03-17 06:45:42 +01:00
}]
}}})
2015-03-17 06:35:57 +01:00
# Setup scenes
bootstrap.setup_component(
hass, 'scene',
{'scene': [
{'name': 'Romantic lights',
'entities': {
2015-03-17 06:45:42 +01:00
lights[0]: True,
lights[1]: {'state': 'on', 'xy_color': [0.33, 0.66],
'brightness': 200},
2015-03-17 06:35:57 +01:00
}},
{'name': 'Switch on and off',
'entities': {
2015-03-17 06:45:42 +01:00
switches[0]: True,
switches[1]: False,
2015-03-17 06:35:57 +01:00
}},
2015-03-17 06:45:42 +01:00
]})
2015-03-17 06:35:57 +01:00
2016-02-27 03:43:50 +01:00
# Set up input select
bootstrap.setup_component(
hass, 'input_select',
2016-02-27 03:12:03 +01:00
{'input_select':
{'living_room_preset': {'options': ['Visitors',
'Visitors with kids',
'Home Alone']},
'who_cooks': {'icon': 'mdi:panda',
2016-02-27 03:25:39 +01:00
'initial': 'Anne Therese',
2016-04-08 09:15:02 +02:00
'name': 'Cook today',
2016-02-27 03:25:39 +01:00
'options': ['Paulus', 'Anne Therese']}}})
2016-02-27 03:43:50 +01:00
# Set up input boolean
bootstrap.setup_component(
hass, 'input_boolean',
{'input_boolean': {'notify': {'icon': 'mdi:car',
2016-02-27 03:12:03 +01:00
'initial': False,
'name': 'Notify Anne Therese is home'}}})
2016-06-10 08:27:35 +02:00
# Set up input boolean
bootstrap.setup_component(
hass, 'input_slider',
{'input_slider': {
'noise_allowance': {'icon': 'mdi:bell-ring',
'min': 0,
'max': 10,
'name': 'Allowed Noise',
'unit_of_measurement': 'dB'}}})
2016-04-08 09:15:02 +02:00
# Set up weblink
bootstrap.setup_component(
hass, 'weblink',
{'weblink': {'entities': [{'name': 'Router',
'url': 'http://192.168.1.1'}]}})
# 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