Skip to content

Commit

Permalink
Fix async_is_supervisor (handle timeouterror)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt committed Aug 22, 2024
1 parent de34af9 commit f5c1f56
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions hass_client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand All @@ -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.
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit f5c1f56

Please sign in to comment.