Add connect_error callback to ReconnectLogic (#111)

This commit is contained in:
Otto Winter 2021-10-13 10:46:13 +02:00 committed by GitHub
parent dd9d716c10
commit b3d1f13eeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,6 +28,7 @@ class ReconnectLogic(zeroconf.RecordUpdateListener): # type: ignore[misc,name-d
on_disconnect: Callable[[], Awaitable[None]],
zeroconf_instance: "zeroconf.Zeroconf",
name: Optional[str] = None,
on_connect_error: Optional[Callable[[Exception], Awaitable[None]]] = None,
) -> None:
"""Initialize ReconnectingLogic.
@ -39,6 +40,7 @@ class ReconnectLogic(zeroconf.RecordUpdateListener): # type: ignore[misc,name-d
self.name = name
self._on_connect_cb = on_connect
self._on_disconnect_cb = on_disconnect
self._on_connect_error_cb = on_connect_error
self._zc = zeroconf_instance
# Flag to check if the device is connected
self._connected = True
@ -118,6 +120,8 @@ class ReconnectLogic(zeroconf.RecordUpdateListener): # type: ignore[misc,name-d
try:
await self._cli.connect(on_stop=self._on_disconnect, login=True)
except Exception as err: # pylint: disable=broad-except
if self._on_connect_error_cb is not None:
await self._on_connect_error_cb(err)
level = logging.WARNING if tries == 0 else logging.DEBUG
_LOGGER.log(
level,