From 4cf077ae5047feb3a5e6e66d6482e9cfb0c948e9 Mon Sep 17 00:00:00 2001 From: Jason Cheatham Date: Sat, 30 Dec 2023 00:26:31 -0700 Subject: [PATCH] fix: handle mis-classified JSON data from Hubitat --- custom_components/hubitat/hubitatmaker/hub.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/custom_components/hubitat/hubitatmaker/hub.py b/custom_components/hubitat/hubitatmaker/hub.py index 9101753..4df051a 100644 --- a/custom_components/hubitat/hubitatmaker/hub.py +++ b/custom_components/hubitat/hubitatmaker/hub.py @@ -1,5 +1,6 @@ """Hubitat API.""" import asyncio +import json import socket from contextlib import contextmanager from logging import getLogger @@ -374,14 +375,14 @@ async def _load_device(self, device_id: str, force_refresh=False) -> None: """Return full info for a specific device.""" if force_refresh or device_id not in self._devices: _LOGGER.debug("Loading device %s", device_id) - json = await self._api_request(f"devices/{device_id}") + data = await self._api_request(f"devices/{device_id}") try: if device_id in self._devices: - self._devices[device_id].update_state(json) + self._devices[device_id].update_state(data) else: - self._devices[device_id] = Device(json) + self._devices[device_id] = Device(data) except Exception as e: - _LOGGER.error("Invalid device info: %s", json) + _LOGGER.error("Invalid device info: %s", data) raise e _LOGGER.debug("Loaded device %s", device_id) @@ -431,10 +432,14 @@ async def _api_request(self, path: str, method="GET") -> Any: else: raise RequestError(resp) try: - json = await resp.json() - if "error" in json and json["error"]: + # Manually parse the response as JSON because Hubitat + # sometimes mis-reports the content type as text/html + # even though the data is JSON + text = await resp.text() + data = json.loads(text) + if "error" in data and data["error"]: raise RequestError(resp) - return json + return data except ContentTypeError as e: text = await resp.text() _LOGGER.warn("Unable to parse as JSON: %s", text)