diff --git a/custom_components/fordpass/fordpass_new.py b/custom_components/fordpass/fordpass_new.py index 538f0c9..bdfdf6b 100644 --- a/custom_components/fordpass/fordpass_new.py +++ b/custom_components/fordpass/fordpass_new.py @@ -563,6 +563,7 @@ def __poll_status(self, url, command_id): def __request_and_poll_command(self, command, vin=None): """Send command to the new Command endpoint""" + self.__acquire_token() headers = { **apiHeaders, "Application-Id": self.region, @@ -592,8 +593,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 < 14: + # 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): diff --git a/custom_components/fordpass/manifest.json b/custom_components/fordpass/manifest.json index 755de44..cbbf2d2 100644 --- a/custom_components/fordpass/manifest.json +++ b/custom_components/fordpass/manifest.json @@ -12,6 +12,6 @@ "loggers": ["custom_components.fordpass"], "requirements": [], "ssdp": [], - "version": "0.1.56", + "version": "0.1.57", "zeroconf": [] } \ No newline at end of file diff --git a/custom_components/fordpass/sensor.py b/custom_components/fordpass/sensor.py index 439e338..5176602 100644 --- a/custom_components/fordpass/sensor.py +++ b/custom_components/fordpass/sensor.py @@ -420,6 +420,7 @@ def get_value(self, ftype): ) else: elecs["Maximum Battery Range"] = self.data["xevBatteryMaximumRange"]["value"] + return elecs ## SquidBytes: Added elVehCharging if self.sensor == "elVehCharging": diff --git a/info.md b/info.md index ffb4025..4a3907f 100644 --- a/info.md +++ b/info.md @@ -1,4 +1,8 @@ ## **Changelog** +### Version 1.57 +- Rewrote command function to actively poll until success or failure is returned +- Fixed bug where elveh attributes wasn't showing +- Fixed bug where command wouldn't check token expiry first ### Version 1.56 - Fix for error when missing GPS data from vehicle - Fix for electric vehicle error