2019-03-19 06:07:39 +00:00
|
|
|
"""The template component."""
|
2021-04-06 19:10:39 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import asyncio
|
2021-09-29 14:19:06 +00:00
|
|
|
from collections.abc import Callable
|
2021-03-29 16:57:51 +00:00
|
|
|
import logging
|
|
|
|
|
2021-04-06 19:10:39 +00:00
|
|
|
from homeassistant import config as conf_util
|
2021-04-24 14:14:31 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_UNIQUE_ID,
|
|
|
|
EVENT_HOMEASSISTANT_START,
|
|
|
|
SERVICE_RELOAD,
|
|
|
|
)
|
2021-04-06 19:10:39 +00:00
|
|
|
from homeassistant.core import CoreState, Event, callback
|
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2021-03-29 16:57:51 +00:00
|
|
|
from homeassistant.helpers import (
|
|
|
|
discovery,
|
|
|
|
trigger as trigger_helper,
|
|
|
|
update_coordinator,
|
|
|
|
)
|
2021-04-06 19:10:39 +00:00
|
|
|
from homeassistant.helpers.reload import async_reload_integration_platforms
|
|
|
|
from homeassistant.loader import async_get_integration
|
2020-08-21 23:31:48 +00:00
|
|
|
|
2021-03-29 16:57:51 +00:00
|
|
|
from .const import CONF_TRIGGER, DOMAIN, PLATFORMS
|
2020-08-21 23:31:48 +00:00
|
|
|
|
2021-04-06 19:10:39 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-08-21 23:31:48 +00:00
|
|
|
|
2021-03-24 03:35:15 +00:00
|
|
|
async def async_setup(hass, config):
|
|
|
|
"""Set up the template integration."""
|
2021-03-29 16:57:51 +00:00
|
|
|
if DOMAIN in config:
|
2021-04-06 19:10:39 +00:00
|
|
|
await _process_config(hass, config)
|
|
|
|
|
|
|
|
async def _reload_config(call: Event) -> None:
|
|
|
|
"""Reload top-level + platforms."""
|
|
|
|
try:
|
|
|
|
unprocessed_conf = await conf_util.async_hass_config_yaml(hass)
|
|
|
|
except HomeAssistantError as err:
|
|
|
|
_LOGGER.error(err)
|
|
|
|
return
|
|
|
|
|
|
|
|
conf = await conf_util.async_process_component_config(
|
|
|
|
hass, unprocessed_conf, await async_get_integration(hass, DOMAIN)
|
|
|
|
)
|
2021-03-29 16:57:51 +00:00
|
|
|
|
2021-04-06 19:10:39 +00:00
|
|
|
if conf is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
await async_reload_integration_platforms(hass, DOMAIN, PLATFORMS)
|
|
|
|
|
|
|
|
if DOMAIN in conf:
|
|
|
|
await _process_config(hass, conf)
|
|
|
|
|
|
|
|
hass.bus.async_fire(f"event_{DOMAIN}_reloaded", context=call.context)
|
|
|
|
|
|
|
|
hass.helpers.service.async_register_admin_service(
|
|
|
|
DOMAIN, SERVICE_RELOAD, _reload_config
|
|
|
|
)
|
2020-08-21 23:31:48 +00:00
|
|
|
|
2021-03-24 03:35:15 +00:00
|
|
|
return True
|
2021-03-29 16:57:51 +00:00
|
|
|
|
|
|
|
|
2021-04-13 00:15:50 +00:00
|
|
|
async def _process_config(hass, hass_config):
|
2021-04-06 19:10:39 +00:00
|
|
|
"""Process config."""
|
2021-04-13 00:15:50 +00:00
|
|
|
coordinators: list[TriggerUpdateCoordinator] | None = hass.data.pop(DOMAIN, None)
|
2021-04-06 19:10:39 +00:00
|
|
|
|
|
|
|
# Remove old ones
|
|
|
|
if coordinators:
|
|
|
|
for coordinator in coordinators:
|
|
|
|
coordinator.async_remove()
|
|
|
|
|
2021-04-13 00:15:50 +00:00
|
|
|
async def init_coordinator(hass, conf_section):
|
|
|
|
coordinator = TriggerUpdateCoordinator(hass, conf_section)
|
|
|
|
await coordinator.async_setup(hass_config)
|
2021-04-06 19:10:39 +00:00
|
|
|
return coordinator
|
|
|
|
|
2021-04-13 00:15:50 +00:00
|
|
|
coordinator_tasks = []
|
|
|
|
|
|
|
|
for conf_section in hass_config[DOMAIN]:
|
|
|
|
if CONF_TRIGGER in conf_section:
|
|
|
|
coordinator_tasks.append(init_coordinator(hass, conf_section))
|
|
|
|
continue
|
|
|
|
|
|
|
|
for platform_domain in PLATFORMS:
|
|
|
|
if platform_domain in conf_section:
|
|
|
|
hass.async_create_task(
|
|
|
|
discovery.async_load_platform(
|
|
|
|
hass,
|
|
|
|
platform_domain,
|
|
|
|
DOMAIN,
|
2021-04-24 14:14:31 +00:00
|
|
|
{
|
|
|
|
"unique_id": conf_section.get(CONF_UNIQUE_ID),
|
|
|
|
"entities": conf_section[platform_domain],
|
|
|
|
},
|
2021-04-13 00:15:50 +00:00
|
|
|
hass_config,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if coordinator_tasks:
|
|
|
|
hass.data[DOMAIN] = await asyncio.gather(*coordinator_tasks)
|
2021-04-06 19:10:39 +00:00
|
|
|
|
|
|
|
|
2021-03-29 16:57:51 +00:00
|
|
|
class TriggerUpdateCoordinator(update_coordinator.DataUpdateCoordinator):
|
|
|
|
"""Class to handle incoming data."""
|
|
|
|
|
2021-04-06 19:10:39 +00:00
|
|
|
REMOVE_TRIGGER = object()
|
|
|
|
|
2021-03-29 16:57:51 +00:00
|
|
|
def __init__(self, hass, config):
|
|
|
|
"""Instantiate trigger data."""
|
2021-04-06 19:10:39 +00:00
|
|
|
super().__init__(hass, _LOGGER, name="Trigger Update Coordinator")
|
2021-03-29 16:57:51 +00:00
|
|
|
self.config = config
|
2021-04-06 19:10:39 +00:00
|
|
|
self._unsub_start: Callable[[], None] | None = None
|
|
|
|
self._unsub_trigger: Callable[[], None] | None = None
|
2021-03-29 16:57:51 +00:00
|
|
|
|
|
|
|
@property
|
2021-04-06 19:10:39 +00:00
|
|
|
def unique_id(self) -> str | None:
|
2021-03-29 16:57:51 +00:00
|
|
|
"""Return unique ID for the entity."""
|
|
|
|
return self.config.get("unique_id")
|
|
|
|
|
2021-04-06 19:10:39 +00:00
|
|
|
@callback
|
|
|
|
def async_remove(self):
|
|
|
|
"""Signal that the entities need to remove themselves."""
|
|
|
|
if self._unsub_start:
|
|
|
|
self._unsub_start()
|
|
|
|
if self._unsub_trigger:
|
|
|
|
self._unsub_trigger()
|
|
|
|
|
2021-03-29 16:57:51 +00:00
|
|
|
async def async_setup(self, hass_config):
|
|
|
|
"""Set up the trigger and create entities."""
|
|
|
|
if self.hass.state == CoreState.running:
|
|
|
|
await self._attach_triggers()
|
|
|
|
else:
|
2021-04-06 19:10:39 +00:00
|
|
|
self._unsub_start = self.hass.bus.async_listen_once(
|
2021-03-29 16:57:51 +00:00
|
|
|
EVENT_HOMEASSISTANT_START, self._attach_triggers
|
|
|
|
)
|
|
|
|
|
2021-04-13 00:15:50 +00:00
|
|
|
for platform_domain in PLATFORMS:
|
|
|
|
if platform_domain in self.config:
|
|
|
|
self.hass.async_create_task(
|
|
|
|
discovery.async_load_platform(
|
|
|
|
self.hass,
|
|
|
|
platform_domain,
|
|
|
|
DOMAIN,
|
|
|
|
{"coordinator": self, "entities": self.config[platform_domain]},
|
|
|
|
hass_config,
|
|
|
|
)
|
2021-04-02 23:57:16 +00:00
|
|
|
)
|
2021-03-29 16:57:51 +00:00
|
|
|
|
|
|
|
async def _attach_triggers(self, start_event=None) -> None:
|
|
|
|
"""Attach the triggers."""
|
2021-04-06 19:10:39 +00:00
|
|
|
if start_event is not None:
|
|
|
|
self._unsub_start = None
|
|
|
|
|
2021-03-29 16:57:51 +00:00
|
|
|
self._unsub_trigger = await trigger_helper.async_initialize_triggers(
|
|
|
|
self.hass,
|
|
|
|
self.config[CONF_TRIGGER],
|
|
|
|
self._handle_triggered,
|
|
|
|
DOMAIN,
|
|
|
|
self.name,
|
|
|
|
self.logger.log,
|
|
|
|
start_event is not None,
|
|
|
|
)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _handle_triggered(self, run_variables, context=None):
|
|
|
|
self.async_set_updated_data(
|
|
|
|
{"run_variables": run_variables, "context": context}
|
|
|
|
)
|