mirror of
https://github.com/Dielee/volvo2mqtt.git
synced 2026-08-13 11:19:52 +02:00
Add engine start and stop logic
This commit is contained in:
+28
-1
@@ -10,7 +10,7 @@ from datetime import datetime
|
|||||||
from babel.dates import format_datetime
|
from babel.dates import format_datetime
|
||||||
from config import settings
|
from config import settings
|
||||||
from const import CLIMATE_START_URL, CLIMATE_STOP_URL, CAR_LOCK_URL, \
|
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
|
mqtt_client: mqtt.Client
|
||||||
subscribed_topics = []
|
subscribed_topics = []
|
||||||
@@ -20,6 +20,7 @@ climate_timer = {}
|
|||||||
engine_status = {}
|
engine_status = {}
|
||||||
devices = {}
|
devices = {}
|
||||||
active_schedules = {}
|
active_schedules = {}
|
||||||
|
engine_runtime = 1
|
||||||
|
|
||||||
|
|
||||||
def connect():
|
def connect():
|
||||||
@@ -114,6 +115,15 @@ def on_message(client, userdata, msg):
|
|||||||
elif "update_data" in msg.topic:
|
elif "update_data" in msg.topic:
|
||||||
if payload == "PRESS":
|
if payload == "PRESS":
|
||||||
update_car_data(True)
|
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:
|
elif "schedule" in msg.topic:
|
||||||
try:
|
try:
|
||||||
d = json.loads(payload)
|
d = json.loads(payload)
|
||||||
@@ -211,6 +221,23 @@ def start_climate(vin):
|
|||||||
update_car_data()
|
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():
|
def update_loop():
|
||||||
create_ha_devices()
|
create_ha_devices()
|
||||||
while True:
|
while True:
|
||||||
|
|||||||
+5
-3
@@ -1,3 +1,4 @@
|
|||||||
|
import json
|
||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
import mqtt
|
import mqtt
|
||||||
@@ -103,6 +104,7 @@ def get_supported_commands():
|
|||||||
supported_commands.append(command["command"])
|
supported_commands.append(command["command"])
|
||||||
else:
|
else:
|
||||||
logging.error("Error getting supported commands: " + str(commands.status_code) + ". " + str(data["error"].get("message")))
|
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():
|
def get_vehicles():
|
||||||
@@ -268,7 +270,7 @@ def check_supported_endpoints():
|
|||||||
# represents the actual engine state
|
# represents the actual engine state
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if entity["domain"] in ["switch", "lock"]:
|
if entity["domain"] in ["switch", "lock", "number"]:
|
||||||
state = check_supported_command(entity["commands"])
|
state = check_supported_command(entity["commands"])
|
||||||
else:
|
else:
|
||||||
if entity.get('url'):
|
if entity.get('url'):
|
||||||
@@ -377,7 +379,7 @@ def get_backend_status():
|
|||||||
return 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:
|
if datetime.now(util.TZ) >= token_expires_at:
|
||||||
refresh_auth()
|
refresh_auth()
|
||||||
|
|
||||||
@@ -398,7 +400,7 @@ def api_call(url, method, vin, sensor_id=None, force_update=False, key_change=Fa
|
|||||||
elif method == "POST":
|
elif method == "POST":
|
||||||
logging.debug("Starting " + method + " call against " + url)
|
logging.debug("Starting " + method + " call against " + url)
|
||||||
try:
|
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:
|
except requests.exceptions.RequestException as e:
|
||||||
logging.error("Error getting data: " + str(e))
|
logging.error("Error getting data: " + str(e))
|
||||||
return None
|
return None
|
||||||
|
|||||||
Reference in New Issue
Block a user