From 781e1da8e8e31a4ecd2c589ba37536695db51d18 Mon Sep 17 00:00:00 2001 From: Linus Dietz <45101649+Dielee@users.noreply.github.com> Date: Thu, 21 Sep 2023 14:31:21 +0200 Subject: [PATCH] Fix #104 (#107) * Catch Volvo API not returning a valid json string #104 * Update changelog --- src/CHANGELOG.md | 5 +++++ src/const.py | 2 +- src/mqtt.py | 11 ++++++----- src/volvo.py | 7 ++++++- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index 3f1ef73..a0cee7f 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -1,3 +1,8 @@ +## v1.8.8 +### 🐛 Bug Fixes: + +- Fix json decode error, if volvo API returns a simple string #104 + ## v1.8.7 ### 🚀 Features: diff --git a/src/const.py b/src/const.py index 748419e..d2ba279 100644 --- a/src/const.py +++ b/src/const.py @@ -1,6 +1,6 @@ from config import settings -VERSION = "v1.8.7" +VERSION = "v1.8.8" OAUTH_URL = "https://volvoid.eu.volvocars.com/as/token.oauth2" VEHICLES_URL = "https://api.volvocars.com/connected-vehicle/v1/vehicles" diff --git a/src/mqtt.py b/src/mqtt.py index 0c0b3c2..30ba3d8 100644 --- a/src/mqtt.py +++ b/src/mqtt.py @@ -255,11 +255,12 @@ def update_car_data(force_update=False, overwrite={}): else: topic = f"homeassistant/{entity['domain']}/{vin}_{entity['id']}/state" - mqtt_client.publish( - topic, - json.dumps(state) if isinstance(state, dict) or isinstance(state, list) else state - ) - update_ha_device(entity, vin, state) + if state: + mqtt_client.publish( + topic, + json.dumps(state) if isinstance(state, dict) or isinstance(state, list) else state + ) + update_ha_device(entity, vin, state) def update_ha_device(entity, vin, state): diff --git a/src/volvo.py b/src/volvo.py index 92aae11..0756753 100644 --- a/src/volvo.py +++ b/src/volvo.py @@ -8,6 +8,7 @@ from threading import currentThread from datetime import datetime, timedelta from config import settings from babel.dates import format_datetime +from json import JSONDecodeError from const import charging_system_states, charging_connection_states, door_states, window_states, \ OAUTH_URL, VEHICLES_URL, VEHICLE_DETAILS_URL, RECHARGE_STATE_URL, CLIMATE_START_URL, \ WINDOWS_STATE_URL, LOCK_STATE_URL, TYRE_STATE_URL, supported_entities, BATTERY_CHARGE_STATE_URL, \ @@ -347,7 +348,11 @@ def api_call(url, method, vin, sensor_id=None, force_update=False, key_change=Fa return None logging.debug("Response status code: " + str(response.status_code)) - data = response.json() + try: + data = response.json() + except JSONDecodeError as e: + logging.error("Fetched json decode error, Volvo API seems to return garbage. Skipping update. Error: " + str(e)) + return None if response.status_code == 200: logging.debug(response.text)