Pause API polling during task in options flow

Hi,
I am working on a custom integration where I connect to a gateway via Rest API. You can retrieve a list of devices that are connected to the gateway. Additionally it’s possible to do a device scan if new devices are connected to the gateway.

So far I created a config flow that reads the device list and an options flow that can initiate the device scan.
My problem is I’m using the DataUpdateCoordinator class so HA is updating the devices in the background. This causes troubles when completely recreating the device list because during a short time some devices may not be reachable anymore and error messages are .

Is it possible to pause the update coordinator until the device scan is finished?

Without seeing your code, hard to give code example but basically set a flag in your dataupdatecoordinator that pauses updates

So,

class MyCoordinator:
    def __init__(self):
        self.pause_updating: bool = False


    async def update_data(self):
        if self.pause_updating:
            return
        
        # Update data from api

and then from your options flow use (obviously you will have to get the ref to your coordinator which should be stored on hass or config objects.

coordinator.pause_updating = True

You could also set your coordinator update interval to 0 and then set it back, but I think this is neater.

Thank you for your help!
This seems like a good approach to me. I will try that.

Just consider the fact if someone closes the option flow without saving so you don’t end up with the fact that no further update happens.

What most integrations are doing is simply reloading the integration when options are saved so then that case you’re experiencing could not happen.

Thanks for your reply!

That’s bad I definitely have to consider this.

That won’t really work for my case. The device scan can take some time (Worst case: ~30mins). When the device scan is started the device list in the API is wiped and newly filled with devices that are found. During that time the options window would show the progress animation but the coordinator still wants to update devices that are no longer accessible.

I probably should add some sort of timeout to “unpause” the coordinator.

The reloading takes place after someone saves so it’s not something the user needs to wait for but obviously the restart would take some additional time.
Anyhow with a listener you can apply whatever logic you want to update data somewhere. There should be examples in the code base to just populate new config data into the coordinator or whatever that would match similar to what you need.