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

View File

@ -168,6 +168,15 @@ class MonitorError(APIError):
self.device_id = device_id
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 = {
"0102": NotLoggedInError,