Skip to content

Commit

Permalink
fix: handle mis-classified JSON data from Hubitat
Browse files Browse the repository at this point in the history
  • Loading branch information
jason0x43 committed Dec 30, 2023
1 parent 9049c03 commit 4cf077a
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions custom_components/hubitat/hubitatmaker/hub.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Hubitat API."""
import asyncio
import json
import socket
from contextlib import contextmanager
from logging import getLogger
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4cf077a

Please sign in to comment.