How to allow Custom Compontent for "YAML configuration reloading"?

Hi All,

I am having a custom component and want it to be able to do " YAML configuration reloading" without need of HA restart. Unfortunately I am unable to find somewhere how this is done…
Would be happy if somebody could explain me the requirements for my integration to be visible here or point me to the correct documentation page :slight_smile:

Thanks

(I am talking about this menu)
2022-02-10_15-28-05

I don’t think this is part of Home Assistant available to third party integrations.

This is though, if you do a config flow instead of yaml:

However, not being a developer, I have no idea how.

Thanks for your reply, but I am pretty sure it is available to third party integrations.
At least it is for this one I am using: https://github.com/PiotrMachowski/Home-Assistant-custom-components-Xiaomi-Cloud-Map-Extractor

2022-02-10_15-38-14

1 Like

Well there you go, I’ve never used a third party integration with that capability. TIL.

Here is the line for the XIAOMI integration.

You need to call this HA function:

await async_setup_reload_service(hass, DOMAIN, PLATFORMS)

You can find the function in here https://github.com/home-assistant/core/blob/51e14cebe34106177ff2eb406811ae2a625e8b0f/homeassistant/helpers/reload.py#L159

1 Like

I do it like this for one of my components.
It just registers a service, though, it doesn’t integrate with the UI

Ah, just checked and it does register in the UI. Never noticed before :wink:

async def async_setup(hass: HomeAssistant, config: dict):
    if DOMAIN not in config:
        return True

    hass.data[DOMAIN] = {}

    component = EntityComponent(_LOGGER, DOMAIN, hass)
    await _async_process_config(hass, config, component)

    async def reload_service_handler(service_call):
        if (conf := await component.async_prepare_reload()) is None:
            return
        await _async_process_config(hass, conf, component)
        hass.bus.async_fire(SIGNAL_STATE_UPDATED, context=service_call.context)

    reload_helper = ReloadServiceHelper(reload_service_handler)

    async_register_admin_service(
        hass,
        DOMAIN,
        SERVICE_RELOAD,
        reload_helper.execute_service,
        schema=vol.Schema({}),
    )

    return True
1 Like

Thanks thats a big hint already.

I never the less do not fully get how this should work… :frowning:
Wherefrom should this e called?

In the XIAOMI integration he calls the async_setup_reload_service within async_setup_platform,
but async_setup_platform is called during every start when the component is loaded…would that end up in a loop?

await async_setup_reload_service(hass, DOMAIN, PLATFORMS)

It just registers a service for reloading the integration and then provide the button in the UI for it. (I haven’t tested this but if you look at the second link I posted, you can see that it basically does what @koying service does)

1 Like

Thanks all of you - I think I got the idea behind :slight_smile:

Did not expect such fast reply and don’t have time to test straight this evening but hopefully during tomorow.

Will keep you updated if it works as describe

1 Like

Any luck with this? I’m trying to get the Reload button for the Config Flow to show

Looking at the simple code of “CPU speed”, I suspect you have the “reload” option as soon as you implement async_unload_entry.

I have the option for a bunch of my own components without doing anything about it.

1 Like

Hi, I’m searching for the same reload option in custom developed component. Indeed, as soon as I implemented the async_unload_entry, the Reload option became available in ‘Integrations’.

async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry):
    await hass.config_entries.async_unload_platforms(config_entry, "sensor")
    return True

I’ve also implemented async_remove_entry in __init__.py

async def async_remove_entry(hass, config_entry):
    try:
        await hass.config_entries.async_forward_entry_unload(config_entry, "sensor")
        _LOGGER.info("Successfully removed sensor from the integration")
    except ValueError:
        pass

And in my sensor.py I’ve added async_remove_entry:

async def async_remove_entry(hass, config_entry):
    _LOGGER.info("async_remove_entry " + NAME)
    try:
        await hass.config_entries.async_forward_entry_unload(config_entry, "sensor")
        _LOGGER.info("Successfully removed sensor from the integration")
    except ValueError:
        pass

and in my sensor component itself I defined: async_will_remove_from_hass which would clear my session so next time it’s used, it would re-initiate the session.

    async def async_will_remove_from_hass(self):
        """Clean up after entity before removal."""
        _LOGGER.info("async_will_remove_from_hass " + NAME)
        self._data.clear_session()

But when I try the reload I get an error indicating the entry can’t be setup since it already exists. While the remove entry is never called.

2023-01-08 13:12:45.268 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry Telenet Telemeter for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 372, in async_setup
    result = await component.async_setup_entry(hass, self)
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 431, in async_setup_entry
    return await component.async_setup_entry(entry)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 166, in async_setup_entry
    raise ValueError("Config entry has already been setup!")

Could someone explain me the flow to be used? or point to the correct docs? I was checking this info: Config Entries | Home Assistant Developer Docs