From 4ebf9df90191225accadcbcfd7333fec6c2e8fdb Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 14 Oct 2022 18:03:43 +0200 Subject: [PATCH] Unconditionally call DomainBlueprints.populate (#80336) --- .../components/automation/__init__.py | 26 +++++++++---------- homeassistant/components/blueprint/models.py | 4 +++ homeassistant/components/script/__init__.py | 15 +++++------ 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 9ad1f79354e..fed941dea1a 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -242,11 +242,14 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # we will create entities before firing EVENT_COMPONENT_LOADED await async_process_integration_platform_for_component(hass, DOMAIN) - # To register the automation blueprints + # Register automation as valid domain for Blueprint async_get_blueprints(hass) - if not await _async_process_config(hass, config, component): - await async_get_blueprints(hass).async_populate() + await _async_process_config(hass, config, component) + + # Add some default blueprints to blueprints/automation, does nothing + # if blueprints/automation already exists + await async_get_blueprints(hass).async_populate() async def trigger_service_handler( entity: AutomationEntity, service_call: ServiceCall @@ -676,10 +679,9 @@ class AutomationEntityConfig: async def _prepare_automation_config( hass: HomeAssistant, config: ConfigType, -) -> tuple[bool, list[AutomationEntityConfig]]: +) -> list[AutomationEntityConfig]: """Parse configuration and prepare automation entity configuration.""" automation_configs: list[AutomationEntityConfig] = [] - blueprints_used = False for config_key in extract_domain_configs(config, DOMAIN): conf: list[ConfigType | blueprint.BlueprintInputs] = config[config_key] @@ -688,7 +690,6 @@ async def _prepare_automation_config( raw_blueprint_inputs = None raw_config = None if isinstance(config_block, blueprint.BlueprintInputs): - blueprints_used = True blueprint_inputs = config_block raw_blueprint_inputs = blueprint_inputs.config_with_inputs @@ -715,7 +716,7 @@ async def _prepare_automation_config( ) ) - return (blueprints_used, automation_configs) + return automation_configs def _automation_name(automation_config: AutomationEntityConfig) -> str: @@ -797,11 +798,8 @@ async def _async_process_config( hass: HomeAssistant, config: dict[str, Any], component: EntityComponent[AutomationEntity], -) -> bool: - """Process config and add automations. - - Returns if blueprints were used. - """ +) -> None: + """Process config and add automations.""" def automation_matches_config( automation: AutomationEntity, config: AutomationEntityConfig @@ -836,7 +834,7 @@ async def _async_process_config( return automation_matches, config_matches - blueprints_used, automation_configs = await _prepare_automation_config(hass, config) + automation_configs = await _prepare_automation_config(hass, config) automations: list[AutomationEntity] = list(component.entities) # Find automations and configurations which have matches @@ -860,7 +858,7 @@ async def _async_process_config( if entities: await component.async_add_entities(entities) - return blueprints_used + return async def _async_process_if( diff --git a/homeassistant/components/blueprint/models.py b/homeassistant/components/blueprint/models.py index bc0938b1097..7547a701220 100644 --- a/homeassistant/components/blueprint/models.py +++ b/homeassistant/components/blueprint/models.py @@ -339,6 +339,10 @@ class DomainBlueprints: async def async_populate(self) -> None: """Create folder if it doesn't exist and populate with examples.""" + if self._blueprints: + # If we have already loaded some blueprint the blueprint folder must exist + return + integration = await loader.async_get_integration(self.hass, self.domain) def populate(): diff --git a/homeassistant/components/script/__init__.py b/homeassistant/components/script/__init__.py index 27bc2556dc5..105309268d1 100644 --- a/homeassistant/components/script/__init__.py +++ b/homeassistant/components/script/__init__.py @@ -168,11 +168,14 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # we will create entities before firing EVENT_COMPONENT_LOADED await async_process_integration_platform_for_component(hass, DOMAIN) - # To register scripts as valid domain for Blueprint + # Register script as valid domain for Blueprint async_get_blueprints(hass) - if not await _async_process_config(hass, config, component): - await async_get_blueprints(hass).async_populate() + await _async_process_config(hass, config, component) + + # Add some default blueprints to blueprints/script, does nothing + # if blueprints/script already exists + await async_get_blueprints(hass).async_populate() async def reload_service(service: ServiceCall) -> None: """Call a service to reload scripts.""" @@ -228,13 +231,12 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: return True -async def _async_process_config(hass, config, component) -> bool: +async def _async_process_config(hass, config, component) -> None: """Process script configuration. Return true, if Blueprints were used. """ entities = [] - blueprints_used = False for config_key in extract_domain_configs(config, DOMAIN): conf: dict[str, dict[str, Any] | BlueprintInputs] = config[config_key] @@ -244,7 +246,6 @@ async def _async_process_config(hass, config, component) -> bool: raw_config = None if isinstance(config_block, BlueprintInputs): - blueprints_used = True blueprint_inputs = config_block raw_blueprint_inputs = blueprint_inputs.config_with_inputs @@ -271,8 +272,6 @@ async def _async_process_config(hass, config, component) -> bool: await component.async_add_entities(entities) - return blueprints_used - class ScriptEntity(ToggleEntity, RestoreEntity): """Representation of a script entity."""