Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions custom_components/remote_homeassistant/rest_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
"""Simple implementation to call Home Assistant REST API."""

import logging

from homeassistant import exceptions
from homeassistant.helpers.aiohttp_client import async_get_clientsession

_LOGGER = logging.getLogger(__name__)

API_URL = "{proto}://{host}:{port}/api/remote_homeassistant/discovery"


Expand All @@ -27,7 +31,7 @@ class UnsupportedVersion(exceptions.HomeAssistantError):


class EndpointMissing(exceptions.HomeAssistantError):
"""Error to indicate there is invalid auth."""
"""Error to indicate endpoint is missing."""


async def async_get_discovery_info(hass, host, port, secure, access_token, verify_ssl):
Expand All @@ -37,23 +41,46 @@ async def async_get_discovery_info(hass, host, port, secure, access_token, verif
host=host,
port=port,
)

access_token = str(access_token).strip()

headers = {
"Authorization": "Bearer " + access_token,
"Content-Type": "application/json",
}

session = async_get_clientsession(hass, verify_ssl)

# Fetch discovery info location for name and unique UUID
async with session.get(url, headers=headers) as resp:
if resp.status == 404:
raise EndpointMissing()

if 400 <= resp.status < 500:
body = await resp.text()
_LOGGER.error(
"Remote Home Assistant auth/API error: status=%s, url=%s, body=%s",
resp.status,
url,
body[:500],
)
raise InvalidAuth()

if resp.status != 200:
body = await resp.text()
_LOGGER.error(
"Remote Home Assistant API problem: status=%s, url=%s, body=%s",
resp.status,
url,
body[:500],
)
raise ApiProblem()
json = await resp.json()
if not isinstance(json, dict):
raise BadResponse(f"Bad response data: {json}")
if "uuid" not in json:

data = await resp.json()

if not isinstance(data, dict):
raise BadResponse(f"Bad response data: {data}")

if "uuid" not in data:
raise UnsupportedVersion()
return json

return data