From 89417696bd10fe0399b0b9c3498ad4ac1544306c Mon Sep 17 00:00:00 2001 From: Linus Dietz <45101649+Dielee@users.noreply.github.com> Date: Wed, 22 Nov 2023 08:49:36 +0100 Subject: [PATCH] Add engine start and stop logic --- src/mqtt.py | 29 ++++++++++++++++++++++++++++- src/volvo.py | 8 +++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/mqtt.py b/src/mqtt.py index e9d3689..ce232b7 100644 --- a/src/mqtt.py +++ b/src/mqtt.py @@ -10,7 +10,7 @@ from datetime import datetime from babel.dates import format_datetime from config import settings from const import CLIMATE_START_URL, CLIMATE_STOP_URL, CAR_LOCK_URL, \ - CAR_UNLOCK_URL, availability_topic, icon_states, old_entity_ids + CAR_UNLOCK_URL, ENGINE_START_URL, ENGINE_STOP_URL, availability_topic, icon_states, old_entity_ids mqtt_client: mqtt.Client subscribed_topics = [] @@ -20,6 +20,7 @@ climate_timer = {} engine_status = {} devices = {} active_schedules = {} +engine_runtime = 1 def connect(): @@ -114,6 +115,15 @@ def on_message(client, userdata, msg): elif "update_data" in msg.topic: if payload == "PRESS": update_car_data(True) + elif "engine_runtime" in msg.topic: + global engine_runtime + engine_runtime = int(payload) + logging.debug("Updated engine runtime to " + str(engine_runtime) + " minutes!") + elif "engine_state" in msg.topic: + if payload == "ON": + start_engine(vin) + elif payload == "OFF": + stop_engine(vin) elif "schedule" in msg.topic: try: d = json.loads(payload) @@ -211,6 +221,23 @@ def start_climate(vin): update_car_data() +def start_engine(vin): + body = {"runtimeMinutes": engine_runtime} + # Start the api call in another thread for HA performance + Thread(target=volvo.api_call, args=(ENGINE_START_URL, "POST", vin, None, None, None, body)).start() + + # Set and update switch status + update_car_data(False, {"entity_id": "engine_state", "vin": vin, "state": "ON"}) + + +def stop_engine(vin): + # Start the api call in another thread for HA performance + Thread(target=volvo.api_call, args=(ENGINE_STOP_URL, "POST", vin)).start() + + # Set and update switch status + update_car_data(False, {"entity_id": "engine_state", "vin": vin, "state": "OFF"}) + + def update_loop(): create_ha_devices() while True: diff --git a/src/volvo.py b/src/volvo.py index a704135..e17ffca 100644 --- a/src/volvo.py +++ b/src/volvo.py @@ -1,3 +1,4 @@ +import json import logging import requests import mqtt @@ -103,6 +104,7 @@ def get_supported_commands(): supported_commands.append(command["command"]) else: logging.error("Error getting supported commands: " + str(commands.status_code) + ". " + str(data["error"].get("message"))) + logging.debug("Supported commands: " + str(supported_commands)) def get_vehicles(): @@ -268,7 +270,7 @@ def check_supported_endpoints(): # represents the actual engine state continue - if entity["domain"] in ["switch", "lock"]: + if entity["domain"] in ["switch", "lock", "number"]: state = check_supported_command(entity["commands"]) else: if entity.get('url'): @@ -377,7 +379,7 @@ def get_backend_status(): return backend_status -def api_call(url, method, vin, sensor_id=None, force_update=False, key_change=False): +def api_call(url, method, vin, sensor_id=None, force_update=False, key_change=False, body=None): if datetime.now(util.TZ) >= token_expires_at: refresh_auth() @@ -398,7 +400,7 @@ def api_call(url, method, vin, sensor_id=None, force_update=False, key_change=Fa elif method == "POST": logging.debug("Starting " + method + " call against " + url) try: - response = session.post(url.format(vin), timeout=20) + response = session.post(url.format(vin), data=json.dumps(body), timeout=20) except requests.exceptions.RequestException as e: logging.error("Error getting data: " + str(e)) return None