66import urllib .error
77import urllib .parse
88import urllib .request
9- from os import environ
109from typing import TYPE_CHECKING
1110
1211from aiohttp import ClientSession
@@ -31,9 +30,12 @@ def get_websocket_url(url: str) -> str:
3130def 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
4648def 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