From f5c1f565feaac7fe2ac379bb1993640c2748a796 Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Thu, 22 Aug 2024 07:57:07 +0200 Subject: [PATCH] Fix async_is_supervisor (handle timeouterror) --- hass_client/utils.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/hass_client/utils.py b/hass_client/utils.py index 2174d74..8ff3374 100644 --- a/hass_client/utils.py +++ b/hass_client/utils.py @@ -6,7 +6,6 @@ import urllib.error import urllib.parse import urllib.request -from os import environ from typing import TYPE_CHECKING from aiohttp import ClientSession @@ -31,9 +30,12 @@ def get_websocket_url(url: str) -> str: def is_supervisor() -> bool: """Return if we're running inside the HA Supervisor (e.g. HAOS).""" try: - urllib.request.urlopen("http://supervisor/core/api", timeout=0.5) + urllib.request.urlopen("http://supervisor/core", timeout=1) except urllib.error.URLError as err: - return err.reason == "Unauthorized" and environ.get("HASSIO_TOKEN") is not None + # this should return a 401 unauthorized if it exists + return getattr(err, "code", 999) == 401 + except TimeoutError: + return False return False @@ -44,7 +46,10 @@ async def async_is_supervisor() -> bool: def get_auth_url( - hass_url: str, redirect_uri: str, client_id: str | None = None, state: str | None = None + hass_url: str, + redirect_uri: str, + client_id: str | None = None, + state: str | None = None, ) -> str: """ Return URL to auth flow. @@ -100,7 +105,9 @@ async def get_token( async with ClientSession() as session: resp = await session.post( - url, data=body, headers={"Content-Type": "application/x-www-form-urlencoded"} + url, + data=body, + headers={"Content-Type": "application/x-www-form-urlencoded"}, ) result = await resp.json() if "error" in result: