pbutterworth / astralpool_chlorinator

Home Assistant custom component to interface with Astra Pool Viron Equilibrium pool chlorinators
MIT License
4 stars 2 forks source link

Potential fix for connection issue #1

Closed DunkyDaMonkey closed 1 year ago

DunkyDaMonkey commented 1 year ago

As I said on the forum post, here's the try except code blocks that I've updated on my integration, I've only had mine on test for about an hour with this version, so potentially don't push the change.

https://github.com/pbutterworth/astralpool_chlorinator/blob/8ce920cb88768d35df3b1e4eeae9f01981769590/custom_components/astralpool_chlorinator/coordinator.py#L29

updated to:

        self._data_age = 0
        self.data = {}

https://github.com/pbutterworth/astralpool_chlorinator/blob/8ce920cb88768d35df3b1e4eeae9f01981769590/custom_components/astralpool_chlorinator/coordinator.py#L37-L40

updated to:

    async def _async_update_data(self):
        """Fetch data from API endpoint."""
        try: 
            data = await self.chlorinator.async_gatherdata()
        except Exception as x:
            data = {}
            #print(x)    
        if data != {}:
            self.data = data
            self._data_age = 0
        else:
            if self._data_age > 5:
                #data stale
                self.data = {}
            else:
                #reuse data
                self._data_age += 1    
        return self.data

Again, take or leave what you want, or even if its inspires you on a different change. If nothing else the try/except will make your code more resilient.

pbutterworth commented 1 year ago

OK, cool - I'm trying something similar now. I've adjusted the polling period to 20 seconds, and hit the API after 3 polling events, so that retries occur a little sooner.

DunkyDaMonkey commented 1 year ago

sounds like you are on to a winner, here's what my entries look like after the above code was changed, the grey bits in the bar are from restarts of home assistant when from other integrations or updates.

image

Let me know if I can help, if not I'll keep out of the way after all this is your baby :)

pbutterworth commented 1 year ago

@DunkyDaMonkey , I've integrated your suggestion. I've adjusted polling to 20 seconds, with a short return of the cached data until after 3 rounds. It's a bit of a hack, would be better to keep the 1 minute polling schedule, but somehow ask for a retry sooner than scheduled.

If you have any ideas on making the command writing more resilient, then have at it!