Advice needed on managing socket based connection issues

I manage eufy_security custom integration which communicates with an add-on via web sockets. In short, two basic things happen;

  • integration might receive a message anytime
  • integration can send a command and expect a response for it.

For some reason, if the connection drops between integration and add-on, what is the best way to recover this? I am aware that, while initial setup of integration, we can use ConfigEnrtyNotReady exception which would force HA to retry it regularly to some extent.

But, in my example, initial setup is over and I am interested both recovering and informing the user properly afterwards.

Open to hear your suggestions

Given your initilize method only runs at startup, you would have to catch the fact the webservice has disconnected in your update_local method and get it to reestablish the connection there. If you have managed to recover the connection then maybe a warning error in the logs, if not reestablished then an error level log. If it requires user intervention then you can raise a reauth exception which will show reauth required in the integrarion page. You could also raise a persistant notificarion but these can be annoying if it is only to inform.

1 Like

Thanks for detailed description, inside update_local, if connection is not established, is there a mechanism to force integration into retrial mode as it was done at startup?

You can just create your reconnect code here. The configentrynotready exception basically retries loading the whole integration. As you have everyrhing loaded already but just lost connection to webservice, it makes more sense to just catch that and reconnect than reload.

Something like…

        try:
            await self._api.poll_refresh()
            return self.data
        except WebSocketConnectionException as exc:
            try:
               await self._api.connect()
               await self._api.poll_refresh()
               return self.data
            except CaptchaRequiredException as exc:
               self.config.captcha_id = exc.captcha_id
               self.config.captcha_img = exc.captcha_img
               raise ConfigEntryAuthFailed() from exc
           except MultiFactorCodeRequiredException as exc:
               self.config.mfa_required = True
               raise ConfigEntryAuthFailed() from exc
           except (DriverNotConnectedException, WebSocketConnectionException) as exc:
               raise UpdateFailed(f"Error communicating with Add-on: {exc}") from exc

Ps sorry for any bad layout, doing on my tablet.

1 Like

Thanks, lastly what is the best way to inform the user about this other than logging and notification?

Setting up the entity as unavailable or having a specific entity about integration’s health?

If it just reconnects, I wouldn’t bother notifying and your integration has handled and carried on working. If it fails, I would raise an error in the logs, maybe an update status entity or a connected entity will help see there is an issue.

Coming back here, finally solved this.

When the issue happens, i am triggering config entry load retrying and it works pretty well.

Thanks for your suggestions