From 6c024c88750e1ef56671a85b98c268eaa01259c6 Mon Sep 17 00:00:00 2001 From: Gleb Sinyavskiy Date: Thu, 24 Nov 2022 21:51:18 +0100 Subject: [PATCH] Bump aiotractive, properly handle auth errors when reading events (#76715) * Bump aiotractive, start handling authorzation errors when reading events * Bump aiotractive, start handling authorzation errors when reading events * Properly handle unauthorized errors * Update homeassistant/components/tractive/__init__.py Co-authored-by: J. Nick Koston * Use await when unsibscribing after auth error * Update homeassistant/components/tractive/__init__.py Co-authored-by: J. Nick Koston Co-authored-by: J. Nick Koston --- homeassistant/components/tractive/__init__.py | 18 ++++++++++++++++-- .../components/tractive/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/tractive/__init__.py b/homeassistant/components/tractive/__init__.py index aa5048b89c0..cc0b2b2b6cb 100644 --- a/homeassistant/components/tractive/__init__.py +++ b/homeassistant/components/tractive/__init__.py @@ -83,7 +83,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: await client.close() raise ConfigEntryNotReady from error - tractive = TractiveClient(hass, client, creds["user_id"]) + tractive = TractiveClient(hass, client, creds["user_id"], entry) tractive.subscribe() try: @@ -148,7 +148,11 @@ class TractiveClient: """A Tractive client.""" def __init__( - self, hass: HomeAssistant, client: aiotractive.Tractive, user_id: str + self, + hass: HomeAssistant, + client: aiotractive.Tractive, + user_id: str, + config_entry: ConfigEntry, ) -> None: """Initialize the client.""" self._hass = hass @@ -157,6 +161,7 @@ class TractiveClient: self._last_hw_time = 0 self._last_pos_time = 0 self._listen_task: asyncio.Task | None = None + self._config_entry = config_entry @property def user_id(self) -> str: @@ -210,6 +215,15 @@ class TractiveClient: ): self._last_pos_time = event["position"]["time"] self._send_position_update(event) + except aiotractive.exceptions.UnauthorizedError: + self._config_entry.async_start_reauth(self._hass) + await self.unsubscribe() + _LOGGER.error( + "Authentication failed for %s, try reconfiguring device", + self._config_entry.data[CONF_EMAIL], + ) + return + except aiotractive.exceptions.TractiveError: _LOGGER.debug( "Tractive is not available. Internet connection is down? Sleeping %i seconds and retrying", diff --git a/homeassistant/components/tractive/manifest.json b/homeassistant/components/tractive/manifest.json index 308f190a063..496c69ddcf7 100644 --- a/homeassistant/components/tractive/manifest.json +++ b/homeassistant/components/tractive/manifest.json @@ -3,7 +3,7 @@ "name": "Tractive", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/tractive", - "requirements": ["aiotractive==0.5.4"], + "requirements": ["aiotractive==0.5.5"], "codeowners": ["@Danielhiversen", "@zhulik", "@bieniu"], "iot_class": "cloud_push", "loggers": ["aiotractive"], diff --git a/requirements_all.txt b/requirements_all.txt index 4d2363dec38..cf2504c8513 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -279,7 +279,7 @@ aioswitcher==3.1.0 aiosyncthing==0.5.1 # homeassistant.components.tractive -aiotractive==0.5.4 +aiotractive==0.5.5 # homeassistant.components.unifi aiounifi==41 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 1cebc382a7b..6e96e13f4b9 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -254,7 +254,7 @@ aioswitcher==3.1.0 aiosyncthing==0.5.1 # homeassistant.components.tractive -aiotractive==0.5.4 +aiotractive==0.5.5 # homeassistant.components.unifi aiounifi==41