Compare commits

..

6 Commits

Author SHA1 Message Date
96233dd7fd Handle appliances without attributes 2023-03-11 22:55:11 +01:00
ef4f7f7398 Restore last command parameters 2023-03-11 02:31:56 +01:00
fc8c92d538 cut and lower program keys 2023-03-11 01:10:27 +01:00
fe4f6e766e Fix connection loose 2023-03-11 00:28:53 +01:00
6b346f766f Fix command start 2023-03-08 23:01:59 +01:00
f52f84711f Fix bugs 2023-03-08 22:18:44 +01:00
8 changed files with 47 additions and 19 deletions

View File

@ -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,
@ -92,9 +100,10 @@ class HonConnection:
}
url = f"{const.API_URL}/commands/v1/context"
async with self._session.get(url, params=params, headers=await self._headers) as response:
if response.status >= 400 and not loop:
if response.status == 403 and not loop:
_LOGGER.error("%s - Error %s - %s", url, response.status, await response.text())
await self.setup()
self._request_headers.pop("cognito-token", None)
self._request_headers.pop("id-token", None)
return await self.load_attributes(device, loop=True)
return (await response.json()).get("payload", {})
@ -129,7 +138,7 @@ class HonConnection:
},
"ancillaryParameters": ancillary_parameters,
"parameters": parameters,
"applianceType": device.appliance_type_name
"applianceType": device.appliance_type
}
url = f"{const.API_URL}/commands/v1/send"
async with self._session.post(url, headers=await self._headers, json=data) as resp:

View File

View File

@ -6,4 +6,5 @@ class Appliance:
if self._data["attributes"]["lastConnEvent"]["category"] == "DISCONNECTED":
self._data["attributes"]["parameters"]["machMode"] = "0"
self._data["active"] = bool(self._data.get("activity"))
self._data["pause"] = self._data["attributes"]["parameters"]["machMode"] == "3"
return self._data

View File

@ -2,12 +2,12 @@ from pyhon.parameter import HonParameterFixed, HonParameterEnum, HonParameterRan
class HonCommand:
def __init__(self, name, attributes, connector, device, multi=None, category=""):
def __init__(self, name, attributes, connector, device, multi=None, program=""):
self._connector = connector
self._device = device
self._name = name
self._multi = multi or {}
self._category = category
self._program = program
self._description = attributes.get("description", "")
self._parameters = self._create_parameters(attributes.get("parameters", {}))
self._ancillary_parameters = self._create_parameters(attributes.get("ancillaryParameters", {}))
@ -38,7 +38,7 @@ class HonCommand:
return {key: parameter.value for key, parameter in self._ancillary_parameters.items()}
async def send(self):
parameters = {name: parameter.value for name, parameter in self._parameters}
parameters = {name: parameter.value for name, parameter in self._parameters.items()}
return await self._connector.send_command(self._device, self._name, parameters, self.ancillary_parameters)
def get_programs(self):

View File

@ -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"

View File

@ -5,7 +5,8 @@ from pyhon.commands import HonCommand
class HonDevice:
def __init__(self, connector, appliance):
appliance["attributes"] = {v["parName"]: v["parValue"] for v in appliance["attributes"]}
if attributes := appliance.get("attributes"):
appliance["attributes"] = {v["parName"]: v["parValue"] for v in attributes}
self._appliance = appliance
self._connector = connector
self._appliance_model = {}
@ -23,7 +24,7 @@ class HonDevice:
if "." in item:
result = self.data
for key in item.split("."):
if all([k in "0123456789" for k in key]):
if all([k in "0123456789" for k in key]) and type(result) is list:
result = result[int(key)]
else:
result = result[key]
@ -31,15 +32,16 @@ class HonDevice:
else:
if item in self.data:
return self.data[item]
return self.attributes["parameters"].get(item, self.appliance[item])
if item in self.attributes["parameters"]:
return self.attributes["parameters"].get(item)
return self.appliance[item]
def get(self, item, default=None):
try:
return self[item]
except KeyError | IndexError:
except (KeyError, IndexError):
return default
@property
def appliance_model_id(self):
return self._appliance.get("applianceModelId")
@ -80,6 +82,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")
@ -91,11 +107,13 @@ class HonDevice:
commands[command] = HonCommand(command, attr, self._connector, self)
elif "parameters" in attr[list(attr)[0]]:
multi = {}
for category, attr2 in attr.items():
cmd = HonCommand(command, attr2, self._connector, self, multi=multi, category=category)
multi[category] = cmd
for program, attr2 in attr.items():
program = program.split(".")[-1].lower()
cmd = HonCommand(command, attr2, self._connector, self, multi=multi, program=program)
multi[program] = cmd
commands[command] = cmd
self._commands = commands
await self._recover_last_command_states(commands)
@property
def settings(self):
@ -127,7 +145,7 @@ class HonDevice:
@property
def data(self):
result = {"attributes": self.attributes, "appliance": self.appliance, "statistics": self.statistics,
"commands": self.parameters}
**self.parameters}
if self._extra:
return result | self._extra.Appliance(result).get()
return result

View File

@ -93,7 +93,7 @@ class HonParameterEnum(HonParameter):
@property
def values(self):
return [str(value) for value in self._values]
return sorted([str(value) for value in self._values])
@property
def value(self):
@ -111,7 +111,7 @@ class HonParameterProgram(HonParameterEnum):
def __init__(self, key, command):
super().__init__(key, {})
self._command = command
self._value = command._category
self._value = command._program
self._values = command._multi
self._typology = "enum"

View File

@ -7,7 +7,7 @@ with open("README.md", "r") as f:
setup(
name="pyhOn",
version="0.3.1",
version="0.3.5",
author="Andre Basche",
description="Control hOn devices with python",
long_description=long_description,