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-03-29 16:57:51 +00:00
|
|
|
import logging
|
|
|
|
|
2021-04-06 19:10:39 +00:00
|
|
|
from homeassistant import config as conf_util
|
2023-08-30 14:22:52 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-09-06 06:54:25 +00:00
|
|
|
from homeassistant.const import CONF_UNIQUE_ID, SERVICE_RELOAD
|
|
|
|
from homeassistant.core import Event, HomeAssistant, ServiceCall
|
2021-04-06 19:10:39 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2023-09-06 06:54:25 +00:00
|
|
|
from homeassistant.helpers import discovery
|
2021-04-06 19:10:39 +00:00
|
|
|
from homeassistant.helpers.reload import async_reload_integration_platforms
|
2022-05-17 17:56:57 +00:00
|
|
|
from homeassistant.helpers.service import async_register_admin_service
|
2022-01-02 06:06:11 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2021-04-06 19:10:39 +00:00
|
|
|
from homeassistant.loader import async_get_integration
|
2020-08-21 23:31:48 +00:00
|
|
|
|
2023-09-06 06:54:25 +00:00
|
|
|
from .const import CONF_TRIGGER, DOMAIN, PLATFORMS
|
|
|
|
from .coordinator import TriggerUpdateCoordinator
|
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
|
|
|
|
2022-01-02 06:06:11 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
2021-03-24 03:35:15 +00:00
|
|
|
"""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)
|
|
|
|
|
2021-12-30 20:12:40 +00:00
|
|
|
async def _reload_config(call: Event | ServiceCall) -> None:
|
2021-04-06 19:10:39 +00:00
|
|
|
"""Reload top-level + platforms."""
|
|
|
|
try:
|
|
|
|
unprocessed_conf = await conf_util.async_hass_config_yaml(hass)
|
|
|
|
except HomeAssistantError as err:
|
|
|
|
_LOGGER.error(err)
|
|
|
|
return
|
|
|
|
|
2023-11-24 16:34:45 +00:00
|
|
|
integration = await async_get_integration(hass, DOMAIN)
|
|
|
|
conf = await conf_util.async_process_component_and_handle_errors(
|
|
|
|
hass, unprocessed_conf, integration
|
2021-04-06 19:10:39 +00:00
|
|
|
)
|
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)
|
|
|
|
|
2022-05-17 17:56:57 +00:00
|
|
|
async_register_admin_service(hass, 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
|
|
|
|
|
|
|
|
2023-08-30 14:22:52 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Set up a config entry."""
|
|
|
|
await hass.config_entries.async_forward_entry_setups(
|
|
|
|
entry, (entry.options["template_type"],)
|
|
|
|
)
|
|
|
|
entry.async_on_unload(entry.add_update_listener(config_entry_update_listener))
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def config_entry_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
|
|
|
"""Update listener, called when the config entry options are changed."""
|
|
|
|
await hass.config_entries.async_reload(entry.entry_id)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Unload a config entry."""
|
|
|
|
return await hass.config_entries.async_unload_platforms(
|
|
|
|
entry, (entry.options["template_type"],)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-02-10 09:59:54 +00:00
|
|
|
async def _process_config(hass: HomeAssistant, hass_config: ConfigType) -> None:
|
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)
|