1 """Class to reload platforms."""
3 from __future__
import annotations
6 from collections.abc
import Iterable
8 from typing
import Any, Literal, overload
10 from homeassistant
import config
as conf_util
17 from .entity
import Entity
18 from .entity_component
import EntityComponent
19 from .entity_platform
import EntityPlatform, async_get_platforms
20 from .service
import async_register_admin_service
21 from .typing
import ConfigType
23 _LOGGER = logging.getLogger(__name__)
25 PLATFORM_RESET_LOCK =
"lock_async_reset_platform_{}"
29 hass: HomeAssistant, integration_domain: str, platform_domains: Iterable[str]
31 """Reload an integration's platforms.
33 The platform must support being re-setup.
35 This functionality is only intended to be used for integrations that process
36 Home Assistant data and make this available to other integrations.
38 Examples are template, stats, derivative, utility meter.
41 unprocessed_conf = await conf_util.async_hass_config_yaml(hass)
42 except HomeAssistantError
as err:
48 for platform_domain
in platform_domains
51 await asyncio.gather(*tasks)
56 integration_domain: str,
58 unprocessed_config: ConfigType,
60 """Resetup a platform."""
63 conf = await conf_util.async_process_component_and_handle_errors(
64 hass, unprocessed_config, integration
70 root_config: dict[str, list[ConfigType]] = {platform_domain: []}
72 for p_type, p_config
in conf_util.config_per_platform(conf, platform_domain):
73 if p_type != integration_domain:
76 root_config[platform_domain].append(p_config)
78 component = await integration.async_get_component()
80 if hasattr(component,
"async_reset_platform"):
83 async
with hass.data.setdefault(
84 PLATFORM_RESET_LOCK.format(platform_domain), asyncio.Lock()
86 await component.async_reset_platform(hass, integration_domain)
87 await component.async_setup(hass, root_config)
93 hass, integration_domain, platform_domain
99 if not root_config[platform_domain]:
105 hass, integration_domain, platform_domain, root_config[platform_domain]
111 integration_domain: str,
112 platform_domain: str,
113 platform_configs: list[dict[str, Any]],
115 """Platform for the first time when new configuration is added."""
116 if platform_domain
not in hass.data:
118 hass, platform_domain, {platform_domain: platform_configs}
122 entity_component: EntityComponent[Entity] = hass.data[platform_domain]
124 entity_component.async_setup_platform(integration_domain, p_config)
125 for p_config
in platform_configs
127 await asyncio.gather(*tasks)
131 platform: EntityPlatform, platform_configs: list[dict[str, Any]]
133 """Reconfigure an already loaded platform."""
134 await platform.async_reset()
135 tasks = [platform.async_setup(p_config)
for p_config
in platform_configs]
136 await asyncio.gather(*tasks)
141 hass: HomeAssistant, integration_name: str
142 ) -> ConfigType |
None: ...
148 integration_name: str,
150 raise_on_failure: Literal[
True],
157 integration_name: str,
159 raise_on_failure: Literal[
False],
160 ) -> ConfigType |
None: ...
164 hass: HomeAssistant, integration_name: str, *, raise_on_failure: bool =
False
165 ) -> ConfigType |
None:
166 """Fetch the latest yaml configuration for an integration."""
168 config = await conf_util.async_hass_config_yaml(hass)
169 return await conf_util.async_process_component_and_handle_errors(
170 hass, config, integration, raise_on_failure=raise_on_failure
176 hass: HomeAssistant, integration_name: str, integration_platform_name: str
177 ) -> EntityPlatform |
None:
178 """Find an existing platform that is not a config entry."""
180 if integration_platform.config_entry
is not None:
182 if integration_platform.domain == integration_platform_name:
183 platform: EntityPlatform = integration_platform
190 hass: HomeAssistant, domain: str, platforms: Iterable[str]
192 """Create the reload service for the domain."""
193 if hass.services.has_service(domain, SERVICE_RELOAD):
196 async
def _reload_config(call: ServiceCall) ->
None:
197 """Reload the platforms."""
199 hass.bus.async_fire(f
"event_{domain}_reloaded", context=call.context)
205 hass: HomeAssistant, domain: str, platforms: Iterable[str]
207 """Sync version of async_setup_reload_service."""
208 asyncio.run_coroutine_threadsafe(
None _resetup_platform(HomeAssistant hass, str integration_domain, str platform_domain, ConfigType unprocessed_config)
None async_setup_reload_service(HomeAssistant hass, str domain, Iterable[str] platforms)
None setup_reload_service(HomeAssistant hass, str domain, Iterable[str] platforms)
None async_reload_integration_platforms(HomeAssistant hass, str integration_domain, Iterable[str] platform_domains)
None _async_reconfig_platform(EntityPlatform platform, list[dict[str, Any]] platform_configs)
EntityPlatform|None async_get_platform_without_config_entry(HomeAssistant hass, str integration_name, str integration_platform_name)
ConfigType|None async_integration_yaml_config(HomeAssistant hass, str integration_name)
None _async_setup_platform(HomeAssistant hass, str integration_domain, str platform_domain, list[dict[str, Any]] platform_configs)
None async_register_admin_service(HomeAssistant hass, str domain, str service, Callable[[ServiceCall], Awaitable[None]|None] service_func, VolSchemaType schema=vol.Schema({}, extra=vol.PREVENT_EXTRA))
Integration async_get_integration(HomeAssistant hass, str domain)
bool async_setup_component(core.HomeAssistant hass, str domain, ConfigType config)