Skip to content

Commit

Permalink
Support force update for backend state
Browse files Browse the repository at this point in the history
  • Loading branch information
Dielee committed Nov 21, 2023
1 parent be4a1b6 commit 161c02f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ def update_car_data(force_update=False, overwrite={}):
elif entity["id"] == "active_schedules":
state = active_schedules[vin]
elif entity["id"] == "api_backend_status":
state = volvo.backend_status
if force_update:
state = volvo.get_backend_status()
else:
state = volvo.backend_status
else:
if entity["id"] == ov_entity_id and vin == ov_vin:
state = ov_state
Expand Down
24 changes: 14 additions & 10 deletions src/volvo.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def authorize():
get_vcc_api_keys()
get_vehicles()
check_supported_endpoints()
Thread(target=get_backend_status).start()
Thread(target=backend_status_loop).start()
else:
message = auth.json()
raise Exception(message["error_description"])
Expand Down Expand Up @@ -316,20 +316,24 @@ def check_engine_status(vin):
time.sleep(5)


def get_backend_status():
global backend_status
def backend_status_loop():
while True:
response = session.get(API_BACKEND_STATUS, timeout=15)
try:
data = response.json()
backend_status = data["message"] if util.keys_exists(data, "message") else "No warnings"
except JSONDecodeError as e:
backend_status = "No warnings"

get_backend_status()
# Update every hour
time.sleep(3600)


def get_backend_status():
global backend_status
response = session.get(API_BACKEND_STATUS, timeout=15)
try:
data = response.json()
backend_status = data["message"] if util.keys_exists(data, "message") else "No warnings"
except JSONDecodeError as e:
backend_status = "No warnings"
return backend_status


def api_call(url, method, vin, sensor_id=None, force_update=False, key_change=False):
if datetime.now(util.TZ) >= token_expires_at:
refresh_auth()
Expand Down

0 comments on commit 161c02f

Please sign in to comment.