From cb10c911a417eebf40b6c6d929b2b6a6354f7a25 Mon Sep 17 00:00:00 2001 From: steve Date: Fri, 13 Oct 2023 21:37:28 +1000 Subject: [PATCH] command now polls status --- custom_components/fordpass/fordpass_new.py | 32 ++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/custom_components/fordpass/fordpass_new.py b/custom_components/fordpass/fordpass_new.py index 538f0c9..22f2940 100644 --- a/custom_components/fordpass/fordpass_new.py +++ b/custom_components/fordpass/fordpass_new.py @@ -592,8 +592,36 @@ def __request_and_poll_command(self, command, vin=None): _LOGGER.debug(r.status_code) _LOGGER.debug(r.text) if r.status_code == 201: - time.sleep(90) - return True + # New code to hanble checking states table from vehicle data + response = r.json() + command_id = response["id"] + current_status = response["currentStatus"] + i = 1 + while i < 9: + # Check status every 10 seconds for 90 seconds until command completes or time expires + status = self.status() + _LOGGER.debug("STATUS") + _LOGGER.debug(status) + + if "states" in status: + _LOGGER.debug("States located") + if f"{command}Command" in status["states"]: + _LOGGER.debug("Found command") + _LOGGER.debug(status["states"][f"{command}Command"]["commandId"]) + if status["states"][f"{command}Command"]["commandId"] == command_id: + _LOGGER.debug("Making progress") + _LOGGER.debug(status["states"][f"{command}Command"]) + if status["states"][f"{command}Command"]["value"]["toState"] == "success": + _LOGGER.debug("Command succeeded") + return True + if status["states"][f"{command}Command"]["value"]["toState"] == "expired": + _LOGGER.debug("Command expired") + return False + i += 1 + _LOGGER.debug("Looping again") + time.sleep(10) + #time.sleep(90) + return False return False def __request_and_poll(self, method, url):