1
0
mirror of https://github.com/no2chem/wideq.git synced 2025-05-20 09:00:09 -07:00

try auto fix json encoding error

This commit is contained in:
pifou 2020-10-13 22:56:27 +02:00
parent 5e47dd0bb2
commit 810a3ce747
2 changed files with 16 additions and 5 deletions

View File

@ -6,6 +6,7 @@ import enum
import logging import logging
import requests import requests
import base64 import base64
import re
from collections import namedtuple from collections import namedtuple
from typing import Any, Dict, Generator, List, Optional from typing import Any, Dict, Generator, List, Optional
@ -436,11 +437,12 @@ class Device(object):
try: try:
return json.loads(data) return json.loads(data)
except json.decoder.JSONDecodeError: except json.decoder.JSONDecodeError:
LOGGER.warning( try:
'JSONDecodeError malformed json (%s)', data) # malformed JSON may contains unwanted [bracket]
# Added fix to correct malformed JSON! LOGGER.debug('attempting to fix JSON format')
# see https://github.com/sampsyo/wideq/issues/64 return json.loads( re.sub(r'^\{(.*?)\}$', r'\1', data))
return json.loads(data.replace('{[', '[').replace(']}', ']')) except json.decoder.JSONDecodeError:
raise core.JsonError(self.device.id, data)
def _get_control(self, key): def _get_control(self, key):
"""Look up a device's control value.""" """Look up a device's control value."""

View File

@ -168,6 +168,15 @@ class MonitorError(APIError):
self.device_id = device_id self.device_id = device_id
self.code = code self.code = code
class JsonError(APIError):
"""JSON Decode error
The received json value is malformed
"""
def __init__(self, device_id, json):
self.device_id = device_id
self.json = json
API_ERRORS = { API_ERRORS = {
"0102": NotLoggedInError, "0102": NotLoggedInError,