Skip to content

Commit

Permalink
Fix is_supervisor helper (#156)
Browse files Browse the repository at this point in the history
Fix the helper/utiliy to determine if we're running on supervisor
  • Loading branch information
marcelveldt authored Feb 14, 2024
1 parent 70b8998 commit 01dc35d
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions hass_client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import urllib.error
import urllib.parse
import urllib.request
from contextlib import suppress
from os import environ
from typing import TYPE_CHECKING

from aiohttp import ClientSession
Expand All @@ -29,8 +29,10 @@ def get_websocket_url(url: str) -> str:

def is_supervisor() -> bool:
"""Return if we're running inside the HA Supervisor (e.g. HAOS)."""
with suppress(urllib.error.URLError):
return urllib.request.urlopen("ws://supervisor/core/websocket").code == 401 # noqa: S310
try:
urllib.request.urlopen("http://supervisor/core/api", timeout=0.5)
except urllib.error.URLError as err:
return err.reason == "Unauthorized" and environ.get("HASSIO_TOKEN") is not None
return False


Expand Down

0 comments on commit 01dc35d

Please sign in to comment.