core/homeassistant/components/template/__init__.py

67 lines
2.0 KiB
Python
Raw Normal View History

Consolidate all platforms that have tests (#22109) * Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
2019-03-19 06:07:39 +00:00
"""The template component."""
import logging
from homeassistant import config as conf_util
from homeassistant.const import SERVICE_RELOAD
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_per_platform, entity_platform
from homeassistant.loader import async_get_integration
from .const import DOMAIN, EVENT_TEMPLATE_RELOADED, PLATFORM_STORAGE_KEY
_LOGGER = logging.getLogger(__name__)
async def _async_setup_reload_service(hass):
if hass.services.has_service(DOMAIN, SERVICE_RELOAD):
return
async def _reload_config(call):
"""Reload the template platform config."""
try:
unprocessed_conf = await conf_util.async_hass_config_yaml(hass)
except HomeAssistantError as err:
_LOGGER.error(err)
return
for platform in hass.data[PLATFORM_STORAGE_KEY]:
integration = await async_get_integration(hass, platform.domain)
conf = await conf_util.async_process_component_config(
hass, unprocessed_conf, integration
)
if not conf:
continue
await platform.async_reset()
# Extract only the config for template, ignore the rest.
for p_type, p_config in config_per_platform(conf, platform.domain):
if p_type != DOMAIN:
continue
entities = await platform.platform.async_create_entities(hass, p_config)
await platform.async_add_entities(entities)
hass.bus.async_fire(EVENT_TEMPLATE_RELOADED, context=call.context)
hass.helpers.service.async_register_admin_service(
DOMAIN, SERVICE_RELOAD, _reload_config
)
async def async_setup_platform_reloadable(hass):
"""Template platform with reloadability."""
await _async_setup_reload_service(hass)
platform = entity_platform.current_platform.get()
if platform not in hass.data.setdefault(PLATFORM_STORAGE_KEY, []):
hass.data[PLATFORM_STORAGE_KEY].append(platform)