From 5b608bea016c20c3118823c07d1bdcf45e3aba09 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 26 May 2024 18:55:00 -1000 Subject: [PATCH] Remove extra inner function for mqtt reload service (#118211) --- homeassistant/components/mqtt/__init__.py | 85 ++++++++++------------- 1 file changed, 38 insertions(+), 47 deletions(-) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index bbde7b76d6d..6be2cc525d8 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -364,63 +364,54 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: ) # setup platforms and discovery - - async def async_setup_reload_service() -> None: - """Create the reload service for the MQTT domain.""" - if hass.services.has_service(DOMAIN, SERVICE_RELOAD): - return - - async def _reload_config(call: ServiceCall) -> None: - """Reload the platforms.""" - # Fetch updated manually configured items and validate - try: - config_yaml = await async_integration_yaml_config( - hass, DOMAIN, raise_on_failure=True - ) - except ConfigValidationError as ex: - raise ServiceValidationError( - translation_domain=ex.translation_domain, - translation_key=ex.translation_key, - translation_placeholders=ex.translation_placeholders, - ) from ex - - new_config: list[ConfigType] = config_yaml.get(DOMAIN, []) - platforms_used = platforms_from_config(new_config) - new_platforms = platforms_used - mqtt_data.platforms_loaded - await async_forward_entry_setup_and_setup_discovery( - hass, entry, new_platforms + async def _reload_config(call: ServiceCall) -> None: + """Reload the platforms.""" + # Fetch updated manually configured items and validate + try: + config_yaml = await async_integration_yaml_config( + hass, DOMAIN, raise_on_failure=True ) - # Check the schema before continuing reload - await async_check_config_schema(hass, config_yaml) + except ConfigValidationError as ex: + raise ServiceValidationError( + translation_domain=ex.translation_domain, + translation_key=ex.translation_key, + translation_placeholders=ex.translation_placeholders, + ) from ex - # Remove repair issues - _async_remove_mqtt_issues(hass, mqtt_data) + new_config: list[ConfigType] = config_yaml.get(DOMAIN, []) + platforms_used = platforms_from_config(new_config) + new_platforms = platforms_used - mqtt_data.platforms_loaded + await async_forward_entry_setup_and_setup_discovery(hass, entry, new_platforms) + # Check the schema before continuing reload + await async_check_config_schema(hass, config_yaml) - mqtt_data.config = new_config + # Remove repair issues + _async_remove_mqtt_issues(hass, mqtt_data) - # Reload the modern yaml platforms - mqtt_platforms = async_get_platforms(hass, DOMAIN) - tasks = [ - entity.async_remove() - for mqtt_platform in mqtt_platforms - for entity in mqtt_platform.entities.values() - if getattr(entity, "_discovery_data", None) is None - and mqtt_platform.config_entry - and mqtt_platform.domain in RELOADABLE_PLATFORMS - ] - await asyncio.gather(*tasks) + mqtt_data.config = new_config - for component in mqtt_data.reload_handlers.values(): - component() + # Reload the modern yaml platforms + mqtt_platforms = async_get_platforms(hass, DOMAIN) + tasks = [ + entity.async_remove() + for mqtt_platform in mqtt_platforms + for entity in mqtt_platform.entities.values() + if getattr(entity, "_discovery_data", None) is None + and mqtt_platform.config_entry + and mqtt_platform.domain in RELOADABLE_PLATFORMS + ] + await asyncio.gather(*tasks) - # Fire event - hass.bus.async_fire(f"event_{DOMAIN}_reloaded", context=call.context) + for component in mqtt_data.reload_handlers.values(): + component() - async_register_admin_service(hass, DOMAIN, SERVICE_RELOAD, _reload_config) + # Fire event + hass.bus.async_fire(f"event_{DOMAIN}_reloaded", context=call.context) await async_forward_entry_setup_and_setup_discovery(hass, entry, platforms_used) # Setup reload service after all platforms have loaded - await async_setup_reload_service() + if not hass.services.has_service(DOMAIN, SERVICE_RELOAD): + async_register_admin_service(hass, DOMAIN, SERVICE_RELOAD, _reload_config) # Setup discovery if conf.get(CONF_DISCOVERY, DEFAULT_DISCOVERY): await discovery.async_start(