Skip to content

Commit f5c1f56

Browse files
committed
Fix async_is_supervisor (handle timeouterror)
1 parent de34af9 commit f5c1f56

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

hass_client/utils.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import urllib.error
77
import urllib.parse
88
import urllib.request
9-
from os import environ
109
from typing import TYPE_CHECKING
1110

1211
from aiohttp import ClientSession
@@ -31,9 +30,12 @@ def get_websocket_url(url: str) -> str:
3130
def is_supervisor() -> bool:
3231
"""Return if we're running inside the HA Supervisor (e.g. HAOS)."""
3332
try:
34-
urllib.request.urlopen("http://supervisor/core/api", timeout=0.5)
33+
urllib.request.urlopen("http://supervisor/core", timeout=1)
3534
except urllib.error.URLError as err:
36-
return err.reason == "Unauthorized" and environ.get("HASSIO_TOKEN") is not None
35+
# this should return a 401 unauthorized if it exists
36+
return getattr(err, "code", 999) == 401
37+
except TimeoutError:
38+
return False
3739
return False
3840

3941

@@ -44,7 +46,10 @@ async def async_is_supervisor() -> bool:
4446

4547

4648
def get_auth_url(
47-
hass_url: str, redirect_uri: str, client_id: str | None = None, state: str | None = None
49+
hass_url: str,
50+
redirect_uri: str,
51+
client_id: str | None = None,
52+
state: str | None = None,
4853
) -> str:
4954
"""
5055
Return URL to auth flow.
@@ -100,7 +105,9 @@ async def get_token(
100105

101106
async with ClientSession() as session:
102107
resp = await session.post(
103-
url, data=body, headers={"Content-Type": "application/x-www-form-urlencoded"}
108+
url,
109+
data=body,
110+
headers={"Content-Type": "application/x-www-form-urlencoded"},
104111
)
105112
result = await resp.json()
106113
if "error" in result:

0 commit comments

Comments
 (0)