Compare commits
	
		
			18 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| f73ef26e74 | |||
| 0ddbdd0b4e | |||
| 241ee95d5c | |||
| c9d4461f8f | |||
| 318e60e91e | |||
| fde41ac456 | |||
| aeabbe64e2 | |||
| 7c99ffeaf7 | |||
| 2941b57d09 | |||
| f00ee03c0d | |||
| ec7355e341 | |||
| 96233dd7fd | |||
| ef4f7f7398 | |||
| fc8c92d538 | |||
| fe4f6e766e | |||
| 6b346f766f | |||
| f52f84711f | |||
| c4d21be388 | 
| @ -24,6 +24,7 @@ def get_arguments(): | |||||||
|     subparser = parser.add_subparsers(title="commands", metavar="COMMAND") |     subparser = parser.add_subparsers(title="commands", metavar="COMMAND") | ||||||
|     keys = subparser.add_parser("keys", help="print as key format") |     keys = subparser.add_parser("keys", help="print as key format") | ||||||
|     keys.add_argument("keys", help="print as key format", action="store_true") |     keys.add_argument("keys", help="print as key format", action="store_true") | ||||||
|  |     keys.add_argument("--all", help="print also full keys", action="store_true") | ||||||
|     return vars(parser.parse_args()) |     return vars(parser.parse_args()) | ||||||
|  |  | ||||||
|  |  | ||||||
| @ -90,9 +91,12 @@ async def main(): | |||||||
|         for device in hon.devices: |         for device in hon.devices: | ||||||
|             print("=" * 10, device.appliance_type, "-", device.nick_name, "=" * 10) |             print("=" * 10, device.appliance_type, "-", device.nick_name, "=" * 10) | ||||||
|             if args.get("keys"): |             if args.get("keys"): | ||||||
|                 key_print(device.data["attributes"]["parameters"]) |                 data = device.data.copy() | ||||||
|                 key_print(device.data["appliance"]) |                 attr = "get" if args.get("all") else "pop" | ||||||
|                 key_print(device.data) |                 key_print(data["attributes"].__getattribute__(attr)("parameters")) | ||||||
|  |                 key_print(data.__getattribute__(attr)("appliance")) | ||||||
|  |                 key_print(data.__getattribute__(attr)("commands")) | ||||||
|  |                 key_print(data) | ||||||
|                 pretty_print(create_command(device.commands, concat=True)) |                 pretty_print(create_command(device.commands, concat=True)) | ||||||
|             else: |             else: | ||||||
|                 pretty_print({"data": device.data}) |                 pretty_print({"data": device.data}) | ||||||
|  | |||||||
							
								
								
									
										19
									
								
								pyhon/api.py
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								pyhon/api.py
									
									
									
									
									
								
							| @ -55,6 +55,8 @@ class HonConnection: | |||||||
|                     appliances = (await resp.json())["payload"]["appliances"] |                     appliances = (await resp.json())["payload"]["appliances"] | ||||||
|                     for appliance in appliances: |                     for appliance in appliances: | ||||||
|                         device = HonDevice(self, appliance) |                         device = HonDevice(self, appliance) | ||||||
|  |                         if device.mac_address is None: | ||||||
|  |                             continue | ||||||
|                         await asyncio.gather(*[ |                         await asyncio.gather(*[ | ||||||
|                             device.load_attributes(), |                             device.load_attributes(), | ||||||
|                             device.load_commands(), |                             device.load_commands(), | ||||||
| @ -70,7 +72,7 @@ class HonConnection: | |||||||
|             "applianceType": device.appliance_type, |             "applianceType": device.appliance_type, | ||||||
|             "code": device.appliance["code"], |             "code": device.appliance["code"], | ||||||
|             "applianceModelId": device.appliance_model_id, |             "applianceModelId": device.appliance_model_id, | ||||||
|             "firmwareId": "41", |             "firmwareId": device.appliance["eepromId"], | ||||||
|             "macAddress": device.mac_address, |             "macAddress": device.mac_address, | ||||||
|             "fwVersion": device.appliance["fwVersion"], |             "fwVersion": device.appliance["fwVersion"], | ||||||
|             "os": const.OS, |             "os": const.OS, | ||||||
| @ -84,6 +86,14 @@ class HonConnection: | |||||||
|                 return {} |                 return {} | ||||||
|             return result |             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): |     async def load_attributes(self, device: HonDevice, loop=False): | ||||||
|         params = { |         params = { | ||||||
|             "macAddress": device.mac_address, |             "macAddress": device.mac_address, | ||||||
| @ -92,9 +102,10 @@ class HonConnection: | |||||||
|         } |         } | ||||||
|         url = f"{const.API_URL}/commands/v1/context" |         url = f"{const.API_URL}/commands/v1/context" | ||||||
|         async with self._session.get(url, params=params, headers=await self._headers) as response: |         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()) |                 _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 self.load_attributes(device, loop=True) | ||||||
|             return (await response.json()).get("payload", {}) |             return (await response.json()).get("payload", {}) | ||||||
|  |  | ||||||
| @ -129,7 +140,7 @@ class HonConnection: | |||||||
|             }, |             }, | ||||||
|             "ancillaryParameters": ancillary_parameters, |             "ancillaryParameters": ancillary_parameters, | ||||||
|             "parameters": parameters, |             "parameters": parameters, | ||||||
|             "applianceType": device.appliance_type_name |             "applianceType": device.appliance_type | ||||||
|         } |         } | ||||||
|         url = f"{const.API_URL}/commands/v1/send" |         url = f"{const.API_URL}/commands/v1/send" | ||||||
|         async with self._session.post(url, headers=await self._headers, json=data) as resp: |         async with self._session.post(url, headers=await self._headers, json=data) as resp: | ||||||
|  | |||||||
							
								
								
									
										0
									
								
								pyhon/appliances/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								pyhon/appliances/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @ -5,4 +5,6 @@ class Appliance: | |||||||
|     def get(self): |     def get(self): | ||||||
|         if self._data["attributes"]["lastConnEvent"]["category"] == "DISCONNECTED": |         if self._data["attributes"]["lastConnEvent"]["category"] == "DISCONNECTED": | ||||||
|             self._data["attributes"]["parameters"]["machMode"] = "0" |             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 |         return self._data | ||||||
|  | |||||||
| @ -2,12 +2,12 @@ from pyhon.parameter import HonParameterFixed, HonParameterEnum, HonParameterRan | |||||||
|  |  | ||||||
|  |  | ||||||
| class HonCommand: | 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._connector = connector | ||||||
|         self._device = device |         self._device = device | ||||||
|         self._name = name |         self._name = name | ||||||
|         self._multi = multi or {} |         self._multi = multi or {} | ||||||
|         self._category = category |         self._program = program | ||||||
|         self._description = attributes.get("description", "") |         self._description = attributes.get("description", "") | ||||||
|         self._parameters = self._create_parameters(attributes.get("parameters", {})) |         self._parameters = self._create_parameters(attributes.get("parameters", {})) | ||||||
|         self._ancillary_parameters = self._create_parameters(attributes.get("ancillaryParameters", {})) |         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()} |         return {key: parameter.value for key, parameter in self._ancillary_parameters.items()} | ||||||
|  |  | ||||||
|     async def send(self): |     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) |         return await self._connector.send_command(self._device, self._name, parameters, self.ancillary_parameters) | ||||||
|  |  | ||||||
|     def get_programs(self): |     def get_programs(self): | ||||||
| @ -67,5 +67,5 @@ class HonCommand: | |||||||
|     @property |     @property | ||||||
|     def settings(self): |     def settings(self): | ||||||
|         """Parameters with typology enum and range""" |         """Parameters with typology enum and range""" | ||||||
|         return {s: self._parameters[s] for s in self.setting_keys} |         return {s: self._parameters.get(s) for s in self.setting_keys if self._parameters.get(s) is not None} | ||||||
|  |  | ||||||
|  | |||||||
| @ -3,7 +3,7 @@ API_URL = "https://api-iot.he.services" | |||||||
| APP = "hon" | APP = "hon" | ||||||
| # All seen id's (different accounts, different devices) are the same, so I guess this hash is static | # 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" | CLIENT_ID = "3MVG9QDx8IX8nP5T2Ha8ofvlmjLZl5L_gvfbT9.HJvpHGKoAS_dcMN8LYpTSYeVFCraUnV.2Ag1Ki7m4znVO6" | ||||||
| APP_VERSION = "1.51.9" | APP_VERSION = "1.53.7" | ||||||
| OS_VERSION = 31 | OS_VERSION = 31 | ||||||
| OS = "android" | OS = "android" | ||||||
| DEVICE_MODEL = "exynos9820" | DEVICE_MODEL = "exynos9820" | ||||||
|  | |||||||
| @ -1,11 +1,14 @@ | |||||||
| import importlib | import importlib | ||||||
|  | from contextlib import suppress | ||||||
|  |  | ||||||
| from pyhon.commands import HonCommand | from pyhon.commands import HonCommand | ||||||
|  | from pyhon.parameter import HonParameterFixed | ||||||
|  |  | ||||||
|  |  | ||||||
| class HonDevice: | class HonDevice: | ||||||
|     def __init__(self, connector, appliance): |     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._appliance = appliance | ||||||
|         self._connector = connector |         self._connector = connector | ||||||
|         self._appliance_model = {} |         self._appliance_model = {} | ||||||
| @ -23,7 +26,7 @@ class HonDevice: | |||||||
|         if "." in item: |         if "." in item: | ||||||
|             result = self.data |             result = self.data | ||||||
|             for key in item.split("."): |             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)] |                     result = result[int(key)] | ||||||
|                 else: |                 else: | ||||||
|                     result = result[key] |                     result = result[key] | ||||||
| @ -31,7 +34,15 @@ class HonDevice: | |||||||
|         else: |         else: | ||||||
|             if item in self.data: |             if item in self.data: | ||||||
|                 return self.data[item] |                 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): | ||||||
|  |             return default | ||||||
|  |  | ||||||
|     @property |     @property | ||||||
|     def appliance_model_id(self): |     def appliance_model_id(self): | ||||||
| @ -73,6 +84,21 @@ class HonDevice: | |||||||
|     def appliance(self): |     def appliance(self): | ||||||
|         return self._appliance |         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 not isinstance(data, HonParameterFixed) and parameters.get(key) is not None: | ||||||
|  |                     with suppress(ValueError): | ||||||
|  |                         data.value = parameters.get(key) | ||||||
|  |  | ||||||
|     async def load_commands(self): |     async def load_commands(self): | ||||||
|         raw = await self._connector.load_commands(self) |         raw = await self._connector.load_commands(self) | ||||||
|         self._appliance_model = raw.pop("applianceModel") |         self._appliance_model = raw.pop("applianceModel") | ||||||
| @ -84,11 +110,13 @@ class HonDevice: | |||||||
|                 commands[command] = HonCommand(command, attr, self._connector, self) |                 commands[command] = HonCommand(command, attr, self._connector, self) | ||||||
|             elif "parameters" in attr[list(attr)[0]]: |             elif "parameters" in attr[list(attr)[0]]: | ||||||
|                 multi = {} |                 multi = {} | ||||||
|                 for category, attr2 in attr.items(): |                 for program, attr2 in attr.items(): | ||||||
|                     cmd = HonCommand(command, attr2, self._connector, self, multi=multi, category=category) |                     program = program.split(".")[-1].lower() | ||||||
|                     multi[category] = cmd |                     cmd = HonCommand(command, attr2, self._connector, self, multi=multi, program=program) | ||||||
|  |                     multi[program] = cmd | ||||||
|                     commands[command] = cmd |                     commands[command] = cmd | ||||||
|         self._commands = commands |         self._commands = commands | ||||||
|  |         await self._recover_last_command_states(commands) | ||||||
|  |  | ||||||
|     @property |     @property | ||||||
|     def settings(self): |     def settings(self): | ||||||
| @ -120,7 +148,7 @@ class HonDevice: | |||||||
|     @property |     @property | ||||||
|     def data(self): |     def data(self): | ||||||
|         result = {"attributes": self.attributes, "appliance": self.appliance, "statistics": self.statistics, |         result = {"attributes": self.attributes, "appliance": self.appliance, "statistics": self.statistics, | ||||||
|                   "commands": self.parameters} |                   **self.parameters} | ||||||
|         if self._extra: |         if self._extra: | ||||||
|             return result | self._extra.Appliance(result).get() |             return result | self._extra.Appliance(result).get() | ||||||
|         return result |         return result | ||||||
|  | |||||||
| @ -30,7 +30,7 @@ class HonParameter: | |||||||
| class HonParameterFixed(HonParameter): | class HonParameterFixed(HonParameter): | ||||||
|     def __init__(self, key, attributes): |     def __init__(self, key, attributes): | ||||||
|         super().__init__(key, attributes) |         super().__init__(key, attributes) | ||||||
|         self._value = attributes["fixedValue"] |         self._value = attributes.get("fixedValue", None) | ||||||
|  |  | ||||||
|     def __repr__(self): |     def __repr__(self): | ||||||
|         return f"{self.__class__} (<{self.key}> fixed)" |         return f"{self.__class__} (<{self.key}> fixed)" | ||||||
| @ -75,6 +75,7 @@ class HonParameterRange(HonParameter): | |||||||
|  |  | ||||||
|     @value.setter |     @value.setter | ||||||
|     def value(self, value): |     def value(self, value): | ||||||
|  |         value = int(value) | ||||||
|         if self._min <= value <= self._max and not value % self._step: |         if self._min <= value <= self._max and not value % self._step: | ||||||
|             self._value = value |             self._value = value | ||||||
|         else: |         else: | ||||||
| @ -93,7 +94,7 @@ class HonParameterEnum(HonParameter): | |||||||
|  |  | ||||||
|     @property |     @property | ||||||
|     def values(self): |     def values(self): | ||||||
|         return [str(value) for value in self._values] |         return sorted([str(value) for value in self._values]) | ||||||
|  |  | ||||||
|     @property |     @property | ||||||
|     def value(self): |     def value(self): | ||||||
| @ -111,7 +112,7 @@ class HonParameterProgram(HonParameterEnum): | |||||||
|     def __init__(self, key, command): |     def __init__(self, key, command): | ||||||
|         super().__init__(key, {}) |         super().__init__(key, {}) | ||||||
|         self._command = command |         self._command = command | ||||||
|         self._value = command._category |         self._value = command._program | ||||||
|         self._values = command._multi |         self._values = command._multi | ||||||
|         self._typology = "enum" |         self._typology = "enum" | ||||||
|  |  | ||||||
|  | |||||||
							
								
								
									
										2
									
								
								setup.py
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								setup.py
									
									
									
									
									
								
							| @ -7,7 +7,7 @@ with open("README.md", "r") as f: | |||||||
|  |  | ||||||
| setup( | setup( | ||||||
|     name="pyhOn", |     name="pyhOn", | ||||||
|     version="0.3.0", |     version="0.3.8", | ||||||
|     author="Andre Basche", |     author="Andre Basche", | ||||||
|     description="Control hOn devices with python", |     description="Control hOn devices with python", | ||||||
|     long_description=long_description, |     long_description=long_description, | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	