2019-02-13 21:21:14 +01:00
|
|
|
"""Set up the demo environment that mimics interaction with devices."""
|
2017-03-01 05:33:19 +01:00
|
|
|
import asyncio
|
2019-04-14 16:59:06 -07:00
|
|
|
import logging
|
2015-01-19 00:02:25 -08:00
|
|
|
import time
|
2014-11-02 09:41:41 -08:00
|
|
|
|
2018-07-18 12:54:27 +03:00
|
|
|
from homeassistant import bootstrap
|
2016-02-18 21:27:50 -08:00
|
|
|
import homeassistant.core as ha
|
2019-04-14 16:59:06 -07:00
|
|
|
from homeassistant.const import ATTR_ENTITY_ID, EVENT_HOMEASSISTANT_START
|
2014-11-02 09:41:41 -08:00
|
|
|
|
2016-08-22 14:19:19 +02:00
|
|
|
DOMAIN = 'demo'
|
2019-04-14 16:59:06 -07:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-03-03 23:50:54 -08:00
|
|
|
COMPONENTS_WITH_DEMO_PLATFORM = [
|
2019-01-05 17:42:36 +01:00
|
|
|
'air_quality',
|
2015-12-05 01:12:38 -08:00
|
|
|
'alarm_control_panel',
|
|
|
|
'binary_sensor',
|
2016-11-19 01:29:20 -05:00
|
|
|
'calendar',
|
2015-12-05 01:12:38 -08:00
|
|
|
'camera',
|
2016-08-20 23:44:31 -07:00
|
|
|
'climate',
|
2016-09-06 23:41:26 +02:00
|
|
|
'cover',
|
2015-12-05 01:12:38 -08: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 01:12:38 -08:00
|
|
|
'light',
|
|
|
|
'lock',
|
|
|
|
'media_player',
|
|
|
|
'notify',
|
|
|
|
'sensor',
|
|
|
|
'switch',
|
2017-01-14 08:18:03 +01:00
|
|
|
'tts',
|
2017-08-06 11:19:47 -07:00
|
|
|
'mailbox',
|
2015-12-05 01:12:38 -08:00
|
|
|
]
|
2015-02-28 07:31:39 -08:00
|
|
|
|
2014-11-02 09:41:41 -08:00
|
|
|
|
2019-04-14 16:59:06 -07:00
|
|
|
async def async_setup(hass, config):
|
2017-04-30 07:04:49 +02:00
|
|
|
"""Set up the demo environment."""
|
2019-04-14 16:59:06 -07:00
|
|
|
if DOMAIN not in config:
|
|
|
|
return True
|
2014-11-02 09:41:41 -08:00
|
|
|
|
2014-11-28 22:49:29 -08:00
|
|
|
config.setdefault(ha.DOMAIN, {})
|
|
|
|
config.setdefault(DOMAIN, {})
|
|
|
|
|
2019-04-14 16:59:06 -07:00
|
|
|
# Set up demo platforms
|
|
|
|
for component in COMPONENTS_WITH_DEMO_PLATFORM:
|
|
|
|
hass.async_create_task(hass.helpers.discovery.async_load_platform(
|
|
|
|
component, DOMAIN, {}, config,
|
|
|
|
))
|
2014-11-02 09:41:41 -08:00
|
|
|
|
2019-04-14 16:59:06 -07:00
|
|
|
# Set up sun
|
2015-04-25 17:44:05 -07:00
|
|
|
if not hass.config.latitude:
|
2015-09-23 23:18:55 -07:00
|
|
|
hass.config.latitude = 32.87336
|
2015-04-25 17:44:05 -07:00
|
|
|
|
|
|
|
if not hass.config.longitude:
|
2015-09-23 23:18:55 -07:00
|
|
|
hass.config.longitude = 117.22743
|
2015-04-25 17:44:05 -07:00
|
|
|
|
2017-03-01 05:33:19 +01:00
|
|
|
tasks = [
|
2019-04-14 16:59:06 -07:00
|
|
|
bootstrap.async_setup_component(hass, 'sun', config)
|
2017-03-01 05:33:19 +01:00
|
|
|
]
|
2014-11-02 09:41:41 -08:00
|
|
|
|
2017-03-01 05:33:19 +01:00
|
|
|
# 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']}}}))
|
2019-04-14 16:59:06 -07:00
|
|
|
|
2017-03-01 05:33:19 +01:00
|
|
|
# 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 14:34:13 -05: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'}]}}))
|
|
|
|
|
2019-04-14 16:59:06 -07:00
|
|
|
results = await asyncio.gather(*tasks)
|
2017-03-01 05:33:19 +01:00
|
|
|
|
|
|
|
if any(not result for result in results):
|
|
|
|
return False
|
2014-11-02 09:41:41 -08:00
|
|
|
|
2017-04-30 07:04:49 +02:00
|
|
|
# Set up example persistent notification
|
2019-04-14 16:59:06 -07:00
|
|
|
hass.components.persistent_notification.async_create(
|
2017-07-18 15:11:17 -07:00
|
|
|
'This is an example of a persistent notification.',
|
2016-06-25 16:40:33 -07:00
|
|
|
title='Example Notification')
|
|
|
|
|
2019-04-14 16:59:06 -07:00
|
|
|
# Set up configurator
|
|
|
|
configurator_ids = []
|
|
|
|
configurator = hass.components.configurator
|
|
|
|
|
|
|
|
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(
|
|
|
|
configurator_ids[0],
|
|
|
|
"Failed to register, please try again.")
|
|
|
|
|
|
|
|
configurator_ids.append(0)
|
|
|
|
else:
|
|
|
|
configurator.request_done(configurator_ids[0])
|
|
|
|
|
|
|
|
request_id = configurator.async_request_config(
|
|
|
|
"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",
|
|
|
|
fields=[{'id': 'username', 'name': 'Username'}],
|
|
|
|
submit_caption="I have pressed the button"
|
|
|
|
)
|
|
|
|
configurator_ids.append(request_id)
|
|
|
|
|
|
|
|
async def demo_start_listener(_event):
|
|
|
|
"""Finish set up."""
|
|
|
|
await finish_setup(hass, config)
|
|
|
|
|
|
|
|
hass.bus.async_listen(EVENT_HOMEASSISTANT_START, demo_start_listener)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def finish_setup(hass, config):
|
|
|
|
"""Finish set up once demo platforms are set up."""
|
2017-03-01 05:33:19 +01:00
|
|
|
lights = sorted(hass.states.async_entity_ids('light'))
|
|
|
|
switches = sorted(hass.states.async_entity_ids('switch'))
|
2016-01-25 21:27:36 -08:00
|
|
|
|
2018-02-26 00:28:25 -08:00
|
|
|
# Set up history graph
|
2019-04-14 16:59:06 -07:00
|
|
|
await bootstrap.async_setup_component(
|
2018-02-26 00:28:25 -08:00
|
|
|
hass, 'history_graph',
|
|
|
|
{'history_graph': {'switches': {
|
|
|
|
'name': 'Recent Switches',
|
|
|
|
'entities': switches,
|
|
|
|
'hours_to_show': 1,
|
|
|
|
'refresh': 60
|
|
|
|
}}}
|
2019-04-14 16:59:06 -07:00
|
|
|
)
|
2018-02-26 00:28:25 -08:00
|
|
|
|
2017-04-30 07:04:49 +02:00
|
|
|
# Set up scripts
|
2019-04-14 16:59:06 -07:00
|
|
|
await bootstrap.async_setup_component(
|
2015-03-16 22:20:31 -07:00
|
|
|
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]),
|
2015-03-16 22:20:31 -07:00
|
|
|
'sequence': [{
|
2016-04-03 10:19:09 -07:00
|
|
|
'service': 'light.turn_off',
|
|
|
|
'data': {ATTR_ENTITY_ID: lights[0]}
|
2015-03-16 22:45:42 -07:00
|
|
|
}, {
|
|
|
|
'delay': {'seconds': 5}
|
|
|
|
}, {
|
2016-04-03 10:19:09 -07:00
|
|
|
'service': 'light.turn_on',
|
|
|
|
'data': {ATTR_ENTITY_ID: lights[0]}
|
2015-03-16 22:45:42 -07:00
|
|
|
}, {
|
|
|
|
'delay': {'seconds': 5}
|
|
|
|
}, {
|
2016-04-03 10:19:09 -07:00
|
|
|
'service': 'light.turn_off',
|
|
|
|
'data': {ATTR_ENTITY_ID: lights[0]}
|
2015-03-16 22:45:42 -07:00
|
|
|
}]
|
2019-04-14 16:59:06 -07:00
|
|
|
}}})
|
2015-03-16 22:20:31 -07:00
|
|
|
|
2017-04-30 07:04:49 +02:00
|
|
|
# Set up scenes
|
2019-04-14 16:59:06 -07:00
|
|
|
await bootstrap.async_setup_component(
|
2015-03-16 22:35:57 -07:00
|
|
|
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
|
|
|
}},
|
2019-04-14 16:59:06 -07:00
|
|
|
]})
|