2014-11-02 18:41:41 +01:00
|
|
|
"""
|
2017-04-30 07:04:49 +02:00
|
|
|
Set up the demo environment that mimics interaction with devices.
|
2016-02-24 10:38:06 +01:00
|
|
|
|
|
|
|
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
|
|
|
"""
|
2017-03-01 05:33:19 +01:00
|
|
|
import asyncio
|
2015-01-19 09:02:25 +01:00
|
|
|
import time
|
2014-11-02 18:41:41 +01:00
|
|
|
|
2018-07-18 11:54:27 +02:00
|
|
|
from homeassistant import bootstrap
|
2016-02-19 06:27:50 +01:00
|
|
|
import homeassistant.core as ha
|
|
|
|
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']
|
2016-08-22 14:19:19 +02:00
|
|
|
DOMAIN = 'demo'
|
2014-11-02 18:41:41 +01:00
|
|
|
|
2015-03-04 08:50:54 +01:00
|
|
|
COMPONENTS_WITH_DEMO_PLATFORM = [
|
2019-01-05 17:42:36 +01:00
|
|
|
'air_quality',
|
2015-12-05 10:12:38 +01:00
|
|
|
'alarm_control_panel',
|
|
|
|
'binary_sensor',
|
2016-11-19 07:29:20 +01:00
|
|
|
'calendar',
|
2015-12-05 10:12:38 +01:00
|
|
|
'camera',
|
2016-08-21 08:44:31 +02:00
|
|
|
'climate',
|
2016-09-06 23:41:26 +02:00
|
|
|
'cover',
|
2015-12-05 10:12:38 +01:00
|
|
|
'device_tracker',
|
2016-09-06 23:41:26 +02:00
|
|
|
'fan',
|
2017-01-14 08:18:03 +01:00
|
|
|
'image_processing',
|
2015-12-05 10:12:38 +01:00
|
|
|
'light',
|
|
|
|
'lock',
|
|
|
|
'media_player',
|
|
|
|
'notify',
|
|
|
|
'sensor',
|
|
|
|
'switch',
|
2017-01-14 08:18:03 +01:00
|
|
|
'tts',
|
2017-08-06 20:19:47 +02:00
|
|
|
'mailbox',
|
2015-12-05 10:12:38 +01:00
|
|
|
]
|
2015-02-28 16:31:39 +01:00
|
|
|
|
2014-11-02 18:41:41 +01:00
|
|
|
|
2018-10-01 08:52:42 +02:00
|
|
|
async def async_setup(hass, config):
|
2017-04-30 07:04:49 +02:00
|
|
|
"""Set up the demo environment."""
|
2017-07-16 21:39:38 +02:00
|
|
|
group = hass.components.group
|
|
|
|
configurator = hass.components.configurator
|
|
|
|
persistent_notification = hass.components.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, {})
|
|
|
|
|
2015-03-17 06:20:31 +01:00
|
|
|
if config[DOMAIN].get('hide_demo_state') != 1:
|
2017-03-01 05:33:19 +01:00
|
|
|
hass.states.async_set('a.Demo_Mode', 'Enabled')
|
2014-11-02 18:41:41 +01:00
|
|
|
|
|
|
|
# Setup sun
|
2015-04-26 02:44:05 +02:00
|
|
|
if not hass.config.latitude:
|
2015-09-24 08:18:55 +02:00
|
|
|
hass.config.latitude = 32.87336
|
2015-04-26 02:44:05 +02:00
|
|
|
|
|
|
|
if not hass.config.longitude:
|
2015-09-24 08:18:55 +02:00
|
|
|
hass.config.longitude = 117.22743
|
2015-04-26 02:44:05 +02:00
|
|
|
|
2017-03-01 05:33:19 +01:00
|
|
|
tasks = [
|
|
|
|
bootstrap.async_setup_component(hass, 'sun')
|
|
|
|
]
|
2014-11-02 18:41:41 +01:00
|
|
|
|
2017-04-30 07:04:49 +02:00
|
|
|
# Set up demo platforms
|
2015-12-06 18:08:34 +01:00
|
|
|
demo_config = config.copy()
|
2015-02-28 16:31:39 +01:00
|
|
|
for component in COMPONENTS_WITH_DEMO_PLATFORM:
|
2015-12-06 18:08:34 +01:00
|
|
|
demo_config[component] = {CONF_PLATFORM: 'demo'}
|
2017-03-01 05:33:19 +01:00
|
|
|
tasks.append(
|
|
|
|
bootstrap.async_setup_component(hass, component, demo_config))
|
|
|
|
|
|
|
|
# Set up input select
|
|
|
|
tasks.append(bootstrap.async_setup_component(
|
|
|
|
hass, 'input_select',
|
|
|
|
{'input_select':
|
|
|
|
{'living_room_preset': {'options': ['Visitors',
|
|
|
|
'Visitors with kids',
|
|
|
|
'Home Alone']},
|
|
|
|
'who_cooks': {'icon': 'mdi:panda',
|
|
|
|
'initial': 'Anne Therese',
|
|
|
|
'name': 'Cook today',
|
|
|
|
'options': ['Paulus', 'Anne Therese']}}}))
|
|
|
|
# Set up input boolean
|
|
|
|
tasks.append(bootstrap.async_setup_component(
|
|
|
|
hass, 'input_boolean',
|
|
|
|
{'input_boolean': {'notify': {
|
|
|
|
'icon': 'mdi:car',
|
|
|
|
'initial': False,
|
|
|
|
'name': 'Notify Anne Therese is home'}}}))
|
|
|
|
|
|
|
|
# Set up input boolean
|
|
|
|
tasks.append(bootstrap.async_setup_component(
|
2017-10-03 21:34:13 +02:00
|
|
|
hass, 'input_number',
|
|
|
|
{'input_number': {
|
2017-03-01 05:33:19 +01:00
|
|
|
'noise_allowance': {'icon': 'mdi:bell-ring',
|
|
|
|
'min': 0,
|
|
|
|
'max': 10,
|
|
|
|
'name': 'Allowed Noise',
|
|
|
|
'unit_of_measurement': 'dB'}}}))
|
|
|
|
|
|
|
|
# Set up weblink
|
|
|
|
tasks.append(bootstrap.async_setup_component(
|
|
|
|
hass, 'weblink',
|
|
|
|
{'weblink': {'entities': [{'name': 'Router',
|
|
|
|
'url': 'http://192.168.1.1'}]}}))
|
|
|
|
|
2018-10-01 08:52:42 +02:00
|
|
|
results = await asyncio.gather(*tasks, loop=hass.loop)
|
2017-03-01 05:33:19 +01:00
|
|
|
|
|
|
|
if any(not result for result in results):
|
|
|
|
return False
|
2014-11-02 18:41:41 +01:00
|
|
|
|
2017-04-30 07:04:49 +02:00
|
|
|
# Set up example persistent notification
|
2017-03-01 05:33:19 +01:00
|
|
|
persistent_notification.async_create(
|
2017-07-19 00:11:17 +02:00
|
|
|
'This is an example of a persistent notification.',
|
2016-06-26 01:40:33 +02:00
|
|
|
title='Example Notification')
|
|
|
|
|
2017-04-30 07:04:49 +02:00
|
|
|
# Set up room groups
|
2017-03-01 05:33:19 +01:00
|
|
|
lights = sorted(hass.states.async_entity_ids('light'))
|
|
|
|
switches = sorted(hass.states.async_entity_ids('switch'))
|
|
|
|
media_players = sorted(hass.states.async_entity_ids('media_player'))
|
2016-10-16 18:35:46 +02:00
|
|
|
|
2017-03-01 05:33:19 +01:00
|
|
|
tasks2 = []
|
2016-01-26 06:27:36 +01:00
|
|
|
|
2018-02-26 09:28:25 +01:00
|
|
|
# Set up history graph
|
|
|
|
tasks2.append(bootstrap.async_setup_component(
|
|
|
|
hass, 'history_graph',
|
|
|
|
{'history_graph': {'switches': {
|
|
|
|
'name': 'Recent Switches',
|
|
|
|
'entities': switches,
|
|
|
|
'hours_to_show': 1,
|
|
|
|
'refresh': 60
|
|
|
|
}}}
|
|
|
|
))
|
|
|
|
|
2017-04-30 07:04:49 +02:00
|
|
|
# Set up scripts
|
2017-03-01 05:33:19 +01:00
|
|
|
tasks2.append(bootstrap.async_setup_component(
|
2015-03-17 06:20:31 +01:00
|
|
|
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]),
|
2015-03-17 06:20:31 +01:00
|
|
|
'sequence': [{
|
2016-04-03 19:19:09 +02:00
|
|
|
'service': 'light.turn_off',
|
|
|
|
'data': {ATTR_ENTITY_ID: lights[0]}
|
2015-03-17 06:45:42 +01:00
|
|
|
}, {
|
|
|
|
'delay': {'seconds': 5}
|
|
|
|
}, {
|
2016-04-03 19:19:09 +02:00
|
|
|
'service': 'light.turn_on',
|
|
|
|
'data': {ATTR_ENTITY_ID: lights[0]}
|
2015-03-17 06:45:42 +01:00
|
|
|
}, {
|
|
|
|
'delay': {'seconds': 5}
|
|
|
|
}, {
|
2016-04-03 19:19:09 +02:00
|
|
|
'service': 'light.turn_off',
|
|
|
|
'data': {ATTR_ENTITY_ID: lights[0]}
|
2015-03-17 06:45:42 +01:00
|
|
|
}]
|
2017-03-01 05:33:19 +01:00
|
|
|
}}}))
|
2015-03-17 06:20:31 +01:00
|
|
|
|
2017-04-30 07:04:49 +02:00
|
|
|
# Set up scenes
|
2017-03-01 05:33:19 +01:00
|
|
|
tasks2.append(bootstrap.async_setup_component(
|
2015-03-17 06:35:57 +01:00
|
|
|
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
|
|
|
}},
|
2017-03-01 05:33:19 +01:00
|
|
|
]}))
|
2015-03-17 06:35:57 +01:00
|
|
|
|
2017-05-07 04:38:48 +02:00
|
|
|
tasks2.append(group.Group.async_create_group(hass, 'Living Room', [
|
2017-03-01 05:33:19 +01:00
|
|
|
lights[1], switches[0], 'input_select.living_room_preset',
|
2017-04-17 00:27:03 +02:00
|
|
|
'cover.living_room_window', media_players[1],
|
2017-03-01 05:33:19 +01:00
|
|
|
'scene.romantic_lights']))
|
2017-05-07 04:38:48 +02:00
|
|
|
tasks2.append(group.Group.async_create_group(hass, 'Bedroom', [
|
2017-03-01 05:33:19 +01:00
|
|
|
lights[0], switches[1], media_players[0],
|
2017-10-03 21:34:13 +02:00
|
|
|
'input_number.noise_allowance']))
|
2017-05-07 04:38:48 +02:00
|
|
|
tasks2.append(group.Group.async_create_group(hass, 'Kitchen', [
|
2017-04-17 00:27:03 +02:00
|
|
|
lights[2], 'cover.kitchen_window', 'lock.kitchen_door']))
|
2017-05-07 04:38:48 +02:00
|
|
|
tasks2.append(group.Group.async_create_group(hass, 'Doors', [
|
2017-03-01 05:33:19 +01:00
|
|
|
'lock.front_door', 'lock.kitchen_door',
|
|
|
|
'garage_door.right_garage_door', 'garage_door.left_garage_door']))
|
2017-05-07 04:38:48 +02:00
|
|
|
tasks2.append(group.Group.async_create_group(hass, 'Automations', [
|
2017-03-01 05:33:19 +01:00
|
|
|
'input_select.who_cooks', 'input_boolean.notify', ]))
|
2017-05-07 04:38:48 +02:00
|
|
|
tasks2.append(group.Group.async_create_group(hass, 'People', [
|
2017-03-01 05:33:19 +01:00
|
|
|
'device_tracker.demo_anne_therese', 'device_tracker.demo_home_boy',
|
|
|
|
'device_tracker.demo_paulus']))
|
2017-05-07 04:38:48 +02:00
|
|
|
tasks2.append(group.Group.async_create_group(hass, 'Downstairs', [
|
2017-03-01 05:33:19 +01:00
|
|
|
'group.living_room', 'group.kitchen',
|
2017-04-17 00:27:03 +02:00
|
|
|
'scene.romantic_lights', 'cover.kitchen_window',
|
|
|
|
'cover.living_room_window', 'group.doors',
|
2017-05-07 04:40:59 +02:00
|
|
|
'climate.ecobee',
|
2017-03-01 05:33:19 +01:00
|
|
|
], view=True))
|
2016-06-10 08:27:35 +02:00
|
|
|
|
2018-10-01 08:52:42 +02:00
|
|
|
results = await asyncio.gather(*tasks2, loop=hass.loop)
|
2017-03-01 05:33:19 +01:00
|
|
|
|
|
|
|
if any(not result for result in results):
|
|
|
|
return False
|
2016-06-10 08:27:35 +02:00
|
|
|
|
2017-04-30 07:04:49 +02:00
|
|
|
# Set up configurator
|
2015-01-19 09:02:25 +01:00
|
|
|
configurator_ids = []
|
|
|
|
|
|
|
|
def hue_configuration_callback(data):
|
2016-02-24 10:38:06 +01:00
|
|
|
"""Fake callback, mark config as done."""
|
2015-01-19 09:02:25 +01:00
|
|
|
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],
|
2015-01-19 09:02:25 +01:00
|
|
|
"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])
|
2015-01-19 09:02:25 +01:00
|
|
|
|
2017-03-01 05:33:19 +01:00
|
|
|
def setup_configurator():
|
2017-04-30 07:04:49 +02:00
|
|
|
"""Set up a configurator."""
|
2017-03-01 05:33:19 +01:00
|
|
|
request_id = configurator.request_config(
|
2017-07-16 21:39:38 +02:00
|
|
|
"Philips Hue", hue_configuration_callback,
|
2017-03-01 05:33:19 +01:00
|
|
|
description=("Press the button on the bridge to register Philips "
|
|
|
|
"Hue with Home Assistant."),
|
|
|
|
description_image="/static/images/config_philips_hue.jpg",
|
2017-06-21 06:10:53 +02:00
|
|
|
fields=[{'id': 'username', 'name': 'Username'}],
|
2017-03-01 05:33:19 +01:00
|
|
|
submit_caption="I have pressed the button"
|
|
|
|
)
|
|
|
|
configurator_ids.append(request_id)
|
|
|
|
|
|
|
|
hass.async_add_job(setup_configurator)
|
2015-01-19 09:02:25 +01:00
|
|
|
|
2014-11-02 18:41:41 +01:00
|
|
|
return True
|