pyhOn/pyhon/api.py

143 lines
5.5 KiB
Python
Raw Permalink Normal View History

2023-02-13 03:36:09 +01:00
import asyncio
2023-02-13 01:41:38 +01:00
import json
import logging
import secrets
from datetime import datetime
from typing import List
import aiohttp as aiohttp
2023-02-13 03:36:09 +01:00
from pyhon import const
from pyhon.auth import HonAuth
from pyhon.device import HonDevice
2023-02-13 01:41:38 +01:00
_LOGGER = logging.getLogger()
class HonConnection:
2023-02-13 03:36:09 +01:00
def __init__(self, email, password, session=None) -> None:
2023-02-13 01:41:38 +01:00
super().__init__()
self._email = email
self._password = password
self._request_headers = {"Content-Type": "application/json"}
2023-02-13 03:36:09 +01:00
self._session = session
2023-02-13 01:41:38 +01:00
self._devices = []
self._mobile_id = secrets.token_hex(8)
async def __aenter__(self):
self._session = aiohttp.ClientSession()
2023-03-04 21:27:10 +01:00
await self.setup()
2023-02-13 01:41:38 +01:00
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self._session.close()
@property
def devices(self) -> List[HonDevice]:
return self._devices
@property
async def _headers(self):
if "cognito-token" not in self._request_headers or "id-token" not in self._request_headers:
auth = HonAuth()
if await auth.authorize(self._email, self._password, self._mobile_id):
self._request_headers["cognito-token"] = auth.cognito_token
self._request_headers["id-token"] = auth.id_token
else:
raise PermissionError("Can't Login")
return self._request_headers
async def setup(self):
async with aiohttp.ClientSession() as session:
async with session.get(f"{const.API_URL}/commands/v1/appliance",
headers=await self._headers) as resp:
try:
appliances = (await resp.json())["payload"]["appliances"]
2023-02-13 03:36:09 +01:00
for appliance in appliances:
device = HonDevice(self, appliance)
await asyncio.gather(*[
device.load_attributes(),
device.load_commands(),
device.load_statistics()])
self._devices.append(device)
2023-02-13 01:41:38 +01:00
except json.JSONDecodeError:
_LOGGER.error("No JSON Data after GET: %s", await resp.text())
return False
return True
async def load_commands(self, device: HonDevice):
params = {
2023-03-08 00:58:25 +01:00
"applianceType": device.appliance_type,
"code": device.appliance["code"],
2023-02-13 01:41:38 +01:00
"applianceModelId": device.appliance_model_id,
"firmwareId": "41",
"macAddress": device.mac_address,
2023-03-08 00:58:25 +01:00
"fwVersion": device.appliance["fwVersion"],
2023-02-13 01:41:38 +01:00
"os": const.OS,
"appVersion": const.APP_VERSION,
2023-03-08 00:58:25 +01:00
"series": device.appliance["series"],
2023-02-13 01:41:38 +01:00
}
url = f"{const.API_URL}/commands/v1/retrieve"
async with self._session.get(url, params=params, headers=await self._headers) as response:
result = (await response.json()).get("payload", {})
if not result or result.pop("resultCode") != "0":
return {}
return result
2023-03-06 18:57:08 +01:00
async def load_attributes(self, device: HonDevice, loop=False):
2023-02-13 01:41:38 +01:00
params = {
"macAddress": device.mac_address,
2023-03-08 00:58:25 +01:00
"applianceType": device.appliance_type,
2023-02-13 01:41:38 +01:00
"category": "CYCLE"
}
url = f"{const.API_URL}/commands/v1/context"
async with self._session.get(url, params=params, headers=await self._headers) as response:
2023-03-06 19:45:46 +01:00
if response.status >= 400 and not loop:
2023-03-08 00:58:25 +01:00
_LOGGER.error("%s - Error %s - %s", url, response.status, await response.text())
2023-03-06 18:57:08 +01:00
await self.setup()
return await self.load_attributes(device, loop=True)
2023-02-13 01:41:38 +01:00
return (await response.json()).get("payload", {})
async def load_statistics(self, device: HonDevice):
params = {
"macAddress": device.mac_address,
2023-03-08 00:58:25 +01:00
"applianceType": device.appliance_type
2023-02-13 01:41:38 +01:00
}
url = f"{const.API_URL}/commands/v1/statistics"
async with self._session.get(url, params=params, headers=await self._headers) as response:
return (await response.json()).get("payload", {})
async def send_command(self, device, command, parameters, ancillary_parameters):
now = datetime.utcnow().isoformat()
data = {
"macAddress": device.mac_address,
"timestamp": f"{now[:-3]}Z",
"commandName": command,
"transactionId": f"{device.mac_address}_{now[:-3]}Z",
"applianceOptions": device.commands_options,
"device": {
"mobileId": self._mobile_id,
"mobileOs": const.OS,
"osVersion": const.OS_VERSION,
"appVersion": const.APP_VERSION,
"deviceModel": const.DEVICE_MODEL
},
"attributes": {
"channel": "mobileApp",
"origin": "standardProgram",
"energyLabel": "0"
},
"ancillaryParameters": ancillary_parameters,
"parameters": parameters,
"applianceType": device.appliance_type_name
}
url = f"{const.API_URL}/commands/v1/send"
async with self._session.post(url, headers=await self._headers, json=data) as resp:
try:
json_data = await resp.json()
except json.JSONDecodeError:
return False
if json_data["payload"]["resultCode"] == "0":
return True
return False