Remove extra inner function for mqtt reload service (#118211)

This commit is contained in:
J. Nick Koston 2024-05-26 18:55:00 -10:00 committed by GitHub
parent 872e9f2d5e
commit 5b608bea01
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -364,63 +364,54 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
) )
# setup platforms and discovery # setup platforms and discovery
async def _reload_config(call: ServiceCall) -> None:
async def async_setup_reload_service() -> None: """Reload the platforms."""
"""Create the reload service for the MQTT domain.""" # Fetch updated manually configured items and validate
if hass.services.has_service(DOMAIN, SERVICE_RELOAD): try:
return config_yaml = await async_integration_yaml_config(
hass, DOMAIN, raise_on_failure=True
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
) )
# Check the schema before continuing reload except ConfigValidationError as ex:
await async_check_config_schema(hass, config_yaml) raise ServiceValidationError(
translation_domain=ex.translation_domain,
translation_key=ex.translation_key,
translation_placeholders=ex.translation_placeholders,
) from ex
# Remove repair issues new_config: list[ConfigType] = config_yaml.get(DOMAIN, [])
_async_remove_mqtt_issues(hass, mqtt_data) 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_data.config = new_config
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)
for component in mqtt_data.reload_handlers.values(): # Reload the modern yaml platforms
component() 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 for component in mqtt_data.reload_handlers.values():
hass.bus.async_fire(f"event_{DOMAIN}_reloaded", context=call.context) 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) await async_forward_entry_setup_and_setup_discovery(hass, entry, platforms_used)
# Setup reload service after all platforms have loaded # 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 # Setup discovery
if conf.get(CONF_DISCOVERY, DEFAULT_DISCOVERY): if conf.get(CONF_DISCOVERY, DEFAULT_DISCOVERY):
await discovery.async_start( await discovery.async_start(