1 """Set up the demo environment that mimics interaction with devices."""
3 from __future__
import annotations
7 from homeassistant
import config_entries, setup
12 EVENT_HOMEASSISTANT_START,
24 COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM = [
26 Platform.ALARM_CONTROL_PANEL,
27 Platform.BINARY_SENSOR,
40 Platform.MEDIA_PLAYER,
52 Platform.WATER_HEATER,
56 COMPONENTS_WITH_DEMO_PLATFORM = [
58 Platform.IMAGE_PROCESSING,
59 Platform.DEVICE_TRACKER,
62 CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
65 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
66 """Set up the demo environment."""
67 hass.async_create_task(
68 hass.config_entries.flow.async_init(
69 DOMAIN, context={
"source": config_entries.SOURCE_IMPORT}, data={}
73 if DOMAIN
not in config:
77 for platform
in COMPONENTS_WITH_DEMO_PLATFORM:
80 config.setdefault(ha.DOMAIN, {})
81 config.setdefault(DOMAIN, {})
84 if not hass.config.latitude:
85 hass.config.latitude = 32.87336
87 if not hass.config.longitude:
88 hass.config.longitude = 117.22743
90 tasks = [setup.async_setup_component(hass,
"sun", config)]
94 setup.async_setup_component(
99 "living_room_preset": {
100 "options": [
"Visitors",
"Visitors with kids",
"Home Alone"]
104 "initial":
"Anne Therese",
105 "name":
"Cook today",
106 "options": [
"Paulus",
"Anne Therese"],
115 setup.async_setup_component(
123 "name":
"Notify Anne Therese is home",
132 setup.async_setup_component(
138 "icon":
"mdi:bell-ring-outline",
148 setup.async_setup_component(
154 "icon":
"mdi:bell-ring",
157 "name":
"Allowed Noise",
158 "unit_of_measurement": UnitOfSoundPressure.DECIBEL,
165 results = await asyncio.gather(*tasks)
167 if any(
not result
for result
in results):
171 persistent_notification.async_create(
173 "This is an example of a persistent notification.",
174 title=
"Example Notification",
177 async
def demo_start_listener(_event: Event) ->
None:
181 hass.bus.async_listen(EVENT_HOMEASSISTANT_START, demo_start_listener)
187 """Set the config entry up."""
189 await hass.config_entries.async_forward_entry_setups(
190 config_entry, COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM
196 """Unload a config entry."""
197 await hass.config_entries.async_unload_platforms(
198 config_entry, COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM
203 async
def finish_setup(hass: HomeAssistant, config: ConfigType) ->
None:
204 """Finish set up once demo platforms are set up."""
205 switches: list[str] |
None =
None
206 lights: list[str] |
None =
None
208 while not switches
and not lights:
210 if switches
is not None:
211 await asyncio.sleep(0)
212 switches = sorted(hass.states.async_entity_ids(
"switch"))
213 lights = sorted(hass.states.async_entity_ids(
"light"))
215 assert switches
is not None
216 assert lights
is not None
218 await setup.async_setup_component(
224 "alias": f
"Toggle {lights[0].split('.')[1]}",
227 "service":
"light.turn_off",
228 "data": {ATTR_ENTITY_ID: lights[0]},
230 {
"delay": {
"seconds": 5}},
232 "service":
"light.turn_on",
233 "data": {ATTR_ENTITY_ID: lights[0]},
235 {
"delay": {
"seconds": 5}},
237 "service":
"light.turn_off",
238 "data": {ATTR_ENTITY_ID: lights[0]},
247 await setup.async_setup_component(
253 "name":
"Romantic lights",
258 "xy_color": [0.33, 0.66],
264 "name":
"Switch on and off",
265 "entities": {switches[0]:
True, switches[1]:
False},
None finish_setup(HomeAssistant hass, ConfigType config)
bool async_setup_entry(HomeAssistant hass, ConfigEntry config_entry)
bool async_setup(HomeAssistant hass, ConfigType config)
bool async_unload_entry(HomeAssistant hass, ConfigEntry config_entry)
None async_load_platform(core.HomeAssistant hass, Platform|str component, str platform, DiscoveryInfoType|None discovered, ConfigType hass_config)