forked from peter/volvo2mqtt
Rework pre-climate state based on engine state
This commit is contained in:
+9
-9
@@ -17,7 +17,7 @@ subscribed_topics = []
|
|||||||
assumed_climate_state = {}
|
assumed_climate_state = {}
|
||||||
last_data_update = None
|
last_data_update = None
|
||||||
climate_timer = {}
|
climate_timer = {}
|
||||||
door_status = {}
|
engine_status = {}
|
||||||
devices = {}
|
devices = {}
|
||||||
|
|
||||||
|
|
||||||
@@ -62,21 +62,21 @@ def on_message(client, userdata, msg):
|
|||||||
|
|
||||||
payload = msg.payload.decode("UTF-8")
|
payload = msg.payload.decode("UTF-8")
|
||||||
if "climate_status" in msg.topic:
|
if "climate_status" in msg.topic:
|
||||||
global assumed_climate_state, climate_timer, door_status
|
global assumed_climate_state, climate_timer, engine_status
|
||||||
if payload == "ON":
|
if payload == "ON":
|
||||||
# Start the api call in another thread for HA performance
|
# Start the api call in another thread for HA performance
|
||||||
Thread(target=volvo.api_call, args=(CLIMATE_START_URL, "POST", vin)).start()
|
Thread(target=volvo.api_call, args=(CLIMATE_START_URL, "POST", vin)).start()
|
||||||
|
|
||||||
# Start door check thread to turn off climate if driver door is opened
|
# Start door check thread to turn off climate if driver door is opened
|
||||||
door_thread = Thread(target=volvo.check_door_status, args=(vin, ))
|
check_engine_thread = Thread(target=volvo.check_engine_status, args=(vin, ))
|
||||||
door_thread.start()
|
check_engine_thread.start()
|
||||||
door_status[vin] = door_thread
|
engine_status[vin] = check_engine_thread
|
||||||
# Starting timer to disable climate after 30 mins
|
|
||||||
|
|
||||||
|
# Starting timer to disable climate after 30 mins
|
||||||
climate_timer[vin] = Timer(30 * 60, volvo.disable_climate, (vin, ))
|
climate_timer[vin] = Timer(30 * 60, volvo.disable_climate, (vin, ))
|
||||||
climate_timer[vin].start()
|
climate_timer[vin].start()
|
||||||
# Set and update switch status
|
|
||||||
|
|
||||||
|
# Set and update switch status
|
||||||
assumed_climate_state[vin] = "ON"
|
assumed_climate_state[vin] = "ON"
|
||||||
update_car_data()
|
update_car_data()
|
||||||
elif payload == "OFF":
|
elif payload == "OFF":
|
||||||
@@ -84,8 +84,8 @@ def on_message(client, userdata, msg):
|
|||||||
Thread(target=volvo.api_call, args=(CLIMATE_STOP_URL, "POST", vin)).start()
|
Thread(target=volvo.api_call, args=(CLIMATE_STOP_URL, "POST", vin)).start()
|
||||||
|
|
||||||
# Stop door check thread if running
|
# Stop door check thread if running
|
||||||
if door_status[vin].is_alive():
|
if engine_status[vin].is_alive():
|
||||||
door_status[vin].do_run = False
|
engine_status[vin].do_run = False
|
||||||
|
|
||||||
# Stop climate timer if active
|
# Stop climate timer if active
|
||||||
if climate_timer[vin].is_alive():
|
if climate_timer[vin].is_alive():
|
||||||
|
|||||||
+16
-7
@@ -174,18 +174,27 @@ def initialize_climate(vins):
|
|||||||
|
|
||||||
def disable_climate(vin):
|
def disable_climate(vin):
|
||||||
logging.info("Turning climate off by timer!")
|
logging.info("Turning climate off by timer!")
|
||||||
mqtt.door_status[vin].do_run = False
|
mqtt.engine_status[vin].do_run = False
|
||||||
mqtt.assumed_climate_state[vin] = "OFF"
|
mqtt.assumed_climate_state[vin] = "OFF"
|
||||||
mqtt.update_car_data()
|
mqtt.update_car_data()
|
||||||
|
|
||||||
|
|
||||||
def check_door_status(vin):
|
def check_engine_status(vin):
|
||||||
|
endpoint_url = ""
|
||||||
|
engine_state_supported = False
|
||||||
|
for endpoint in supported_endpoints[vin]:
|
||||||
|
if "engine_state" == endpoint["id"]:
|
||||||
|
engine_state_supported = True
|
||||||
|
endpoint_url = endpoint["url"]
|
||||||
|
|
||||||
|
if not engine_state_supported:
|
||||||
|
# Exit thread as engine state is unsupported by car
|
||||||
|
return None
|
||||||
|
|
||||||
t = currentThread()
|
t = currentThread()
|
||||||
while getattr(t, "do_run", True):
|
while getattr(t, "do_run", True):
|
||||||
lock_door_left = api_call(LOCK_STATE_URL, "GET", vin, "door_front_left", True)
|
engine_state = api_call(endpoint_url, "GET", vin, "engine_state", True)
|
||||||
lock_door_right = api_call(LOCK_STATE_URL, "GET", vin, "door_front_right", True)
|
if engine_state == "RUNNING":
|
||||||
if lock_door_left == "ON" or lock_door_right == "ON":
|
|
||||||
Thread(target=api_call, args=(CLIMATE_STOP_URL, "POST", vin)).start()
|
|
||||||
mqtt.assumed_climate_state[vin] = "OFF"
|
mqtt.assumed_climate_state[vin] = "OFF"
|
||||||
mqtt.update_car_data()
|
mqtt.update_car_data()
|
||||||
break
|
break
|
||||||
@@ -258,7 +267,7 @@ def cached_request(url, method, vin, force_update=False):
|
|||||||
if (datetime.now(util.TZ) - cached_requests[vin + "_" + url]["last_update"]).total_seconds() \
|
if (datetime.now(util.TZ) - cached_requests[vin + "_" + url]["last_update"]).total_seconds() \
|
||||||
>= settings["updateInterval"] or (force_update and
|
>= settings["updateInterval"] or (force_update and
|
||||||
(datetime.now(util.TZ) - cached_requests[vin + "_" + url]
|
(datetime.now(util.TZ) - cached_requests[vin + "_" + url]
|
||||||
["last_update"]).total_seconds() >= 2):
|
["last_update"]).total_seconds() >= 2):
|
||||||
# Old Data in Cache, or force mode active, updating
|
# Old Data in Cache, or force mode active, updating
|
||||||
logging.debug("Starting " + method + " call against " + url)
|
logging.debug("Starting " + method + " call against " + url)
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user