Skip to content

Commit

Permalink
Catch Volvo API not returning a valid json string #104
Browse files Browse the repository at this point in the history
  • Loading branch information
Dielee committed Sep 21, 2023
1 parent 6b419cf commit de1ac0f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/const.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
11 changes: 6 additions & 5 deletions src/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 6 additions & 1 deletion src/volvo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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, \
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit de1ac0f

Please sign in to comment.