From ef4f7f739815b7975162502945b09e1fcb0c542e Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Sat, 11 Mar 2023 02:31:56 +0100 Subject: [PATCH] Restore last command parameters --- pyhon/api.py | 8 ++++++++ pyhon/const.py | 2 +- pyhon/device.py | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/pyhon/api.py b/pyhon/api.py index b72ad7c..2903d7e 100644 --- a/pyhon/api.py +++ b/pyhon/api.py @@ -84,6 +84,14 @@ class HonConnection: return {} return result + async def command_history(self, device: HonDevice): + url = f"{const.API_URL}/commands/v1/appliance/{device.mac_address}/history" + async with self._session.get(url, headers=await self._headers) as response: + result = await response.json() + if not result or not result.get("payload"): + return {} + return result["payload"]["history"] + async def load_attributes(self, device: HonDevice, loop=False): params = { "macAddress": device.mac_address, diff --git a/pyhon/const.py b/pyhon/const.py index f984c50..a9034d2 100644 --- a/pyhon/const.py +++ b/pyhon/const.py @@ -3,7 +3,7 @@ API_URL = "https://api-iot.he.services" APP = "hon" # All seen id's (different accounts, different devices) are the same, so I guess this hash is static CLIENT_ID = "3MVG9QDx8IX8nP5T2Ha8ofvlmjLZl5L_gvfbT9.HJvpHGKoAS_dcMN8LYpTSYeVFCraUnV.2Ag1Ki7m4znVO6" -APP_VERSION = "1.51.9" +APP_VERSION = "1.53.7" OS_VERSION = 31 OS = "android" DEVICE_MODEL = "exynos9820" diff --git a/pyhon/device.py b/pyhon/device.py index 5e2b8c4..eb4b9e2 100644 --- a/pyhon/device.py +++ b/pyhon/device.py @@ -81,6 +81,20 @@ class HonDevice: def appliance(self): return self._appliance + async def _recover_last_command_states(self, commands): + command_history = await self._connector.command_history(self) + for name, command in commands.items(): + last = next((index for (index, d) in enumerate(command_history) if d.get("command", {}).get("commandName") == name), None) + if last is None: + continue + parameters = command_history[last].get("command", {}).get("parameters", {}) + if command._multi and parameters.get("program"): + command.set_program(parameters.pop("program").split(".")[-1].lower()) + command = self.commands[name] + for key, data in command.settings.items(): + if parameters.get(key) is not None: + data.value = parameters.get(key) + async def load_commands(self): raw = await self._connector.load_commands(self) self._appliance_model = raw.pop("applianceModel") @@ -98,6 +112,7 @@ class HonDevice: multi[program] = cmd commands[command] = cmd self._commands = commands + await self._recover_last_command_states(commands) @property def settings(self):