2023-02-13 00:41:38 +00:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
from datetime import datetime
|
2023-06-25 15:29:04 +00:00
|
|
|
from pathlib import Path
|
2023-06-09 00:09:20 +00:00
|
|
|
from pprint import pformat
|
2023-06-28 17:02:11 +00:00
|
|
|
from types import TracebackType
|
|
|
|
from typing import Dict, Optional, Any, List, no_type_check, Type
|
2023-02-13 00:41:38 +00:00
|
|
|
|
2023-04-13 21:25:49 +00:00
|
|
|
from aiohttp import ClientSession
|
2023-04-15 21:02:37 +00:00
|
|
|
from typing_extensions import Self
|
2023-04-13 21:25:49 +00:00
|
|
|
|
|
|
|
from pyhon import const, exceptions
|
2023-04-09 16:13:50 +00:00
|
|
|
from pyhon.appliance import HonAppliance
|
2023-04-13 21:25:49 +00:00
|
|
|
from pyhon.connection.auth import HonAuth
|
2023-04-15 12:22:04 +00:00
|
|
|
from pyhon.connection.handler.anonym import HonAnonymousConnectionHandler
|
2023-04-15 21:02:37 +00:00
|
|
|
from pyhon.connection.handler.hon import HonConnectionHandler
|
2023-02-13 00:41:38 +00:00
|
|
|
|
2023-04-23 18:14:52 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2023-02-13 00:41:38 +00:00
|
|
|
|
|
|
|
|
2023-04-09 16:13:50 +00:00
|
|
|
class HonAPI:
|
2023-04-13 21:25:49 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
email: str = "",
|
|
|
|
password: str = "",
|
|
|
|
anonymous: bool = False,
|
|
|
|
session: Optional[ClientSession] = None,
|
|
|
|
) -> None:
|
2023-02-13 00:41:38 +00:00
|
|
|
super().__init__()
|
2023-04-13 21:25:49 +00:00
|
|
|
self._email: str = email
|
|
|
|
self._password: str = password
|
|
|
|
self._anonymous: bool = anonymous
|
|
|
|
self._hon_handler: Optional[HonConnectionHandler] = None
|
|
|
|
self._hon_anonymous_handler: Optional[HonAnonymousConnectionHandler] = None
|
|
|
|
self._session: Optional[ClientSession] = session
|
|
|
|
|
|
|
|
async def __aenter__(self) -> Self:
|
2023-04-09 18:50:28 +00:00
|
|
|
return await self.create()
|
2023-02-13 00:41:38 +00:00
|
|
|
|
2023-06-28 17:02:11 +00:00
|
|
|
async def __aexit__(
|
|
|
|
self,
|
|
|
|
exc_type: Optional[Type[BaseException]],
|
|
|
|
exc: Optional[BaseException],
|
|
|
|
traceback: Optional[TracebackType],
|
|
|
|
) -> None:
|
2023-04-10 04:34:19 +00:00
|
|
|
await self.close()
|
2023-02-13 00:41:38 +00:00
|
|
|
|
2023-04-13 21:25:49 +00:00
|
|
|
@property
|
|
|
|
def auth(self) -> HonAuth:
|
|
|
|
if self._hon is None or self._hon.auth is None:
|
|
|
|
raise exceptions.NoAuthenticationException
|
|
|
|
return self._hon.auth
|
|
|
|
|
|
|
|
@property
|
2023-06-28 17:02:11 +00:00
|
|
|
def _hon(self) -> HonConnectionHandler:
|
2023-04-13 21:25:49 +00:00
|
|
|
if self._hon_handler is None:
|
|
|
|
raise exceptions.NoAuthenticationException
|
|
|
|
return self._hon_handler
|
|
|
|
|
|
|
|
@property
|
2023-06-28 17:02:11 +00:00
|
|
|
def _hon_anonymous(self) -> HonAnonymousConnectionHandler:
|
2023-04-13 21:25:49 +00:00
|
|
|
if self._hon_anonymous_handler is None:
|
|
|
|
raise exceptions.NoAuthenticationException
|
|
|
|
return self._hon_anonymous_handler
|
|
|
|
|
|
|
|
async def create(self) -> Self:
|
|
|
|
self._hon_anonymous_handler = await HonAnonymousConnectionHandler(
|
2023-04-10 04:34:19 +00:00
|
|
|
self._session
|
|
|
|
).create()
|
2023-04-09 21:47:33 +00:00
|
|
|
if not self._anonymous:
|
2023-04-13 21:25:49 +00:00
|
|
|
self._hon_handler = await HonConnectionHandler(
|
2023-04-10 04:34:19 +00:00
|
|
|
self._email, self._password, self._session
|
|
|
|
).create()
|
2023-04-09 18:50:28 +00:00
|
|
|
return self
|
2023-02-13 00:41:38 +00:00
|
|
|
|
2023-06-25 15:29:04 +00:00
|
|
|
async def load_appliances(self) -> List[Dict[str, Any]]:
|
2023-04-09 16:13:50 +00:00
|
|
|
async with self._hon.get(f"{const.API_URL}/commands/v1/appliance") as resp:
|
2023-06-25 15:29:04 +00:00
|
|
|
if result := await resp.json():
|
|
|
|
return result.get("payload", {}).get("appliances", {})
|
|
|
|
return []
|
2023-04-09 18:50:28 +00:00
|
|
|
|
2023-06-25 15:29:04 +00:00
|
|
|
async def load_commands(self, appliance: HonAppliance) -> Dict[str, Any]:
|
2023-06-28 17:02:11 +00:00
|
|
|
params: Dict[str, str | int] = {
|
2023-04-09 18:50:28 +00:00
|
|
|
"applianceType": appliance.appliance_type,
|
|
|
|
"applianceModelId": appliance.appliance_model_id,
|
|
|
|
"macAddress": appliance.mac_address,
|
2023-02-13 00:41:38 +00:00
|
|
|
"os": const.OS,
|
|
|
|
"appVersion": const.APP_VERSION,
|
2023-05-20 11:24:24 +00:00
|
|
|
"code": appliance.code,
|
2023-02-13 00:41:38 +00:00
|
|
|
}
|
2023-04-23 12:40:39 +00:00
|
|
|
if firmware_id := appliance.info.get("eepromId"):
|
|
|
|
params["firmwareId"] = firmware_id
|
2023-04-23 17:28:56 +00:00
|
|
|
if firmware_version := appliance.info.get("fwVersion"):
|
|
|
|
params["fwVersion"] = firmware_version
|
2023-05-20 11:24:24 +00:00
|
|
|
if series := appliance.info.get("series"):
|
|
|
|
params["series"] = series
|
2023-04-13 21:25:49 +00:00
|
|
|
url: str = f"{const.API_URL}/commands/v1/retrieve"
|
2023-04-09 16:13:50 +00:00
|
|
|
async with self._hon.get(url, params=params) as response:
|
2023-06-28 17:02:11 +00:00
|
|
|
result: Dict[str, Any] = (await response.json()).get("payload", {})
|
2023-02-13 00:41:38 +00:00
|
|
|
if not result or result.pop("resultCode") != "0":
|
2023-05-20 11:24:24 +00:00
|
|
|
_LOGGER.error(await response.json())
|
2023-02-13 00:41:38 +00:00
|
|
|
return {}
|
|
|
|
return result
|
|
|
|
|
2023-06-25 15:29:04 +00:00
|
|
|
async def load_command_history(
|
|
|
|
self, appliance: HonAppliance
|
|
|
|
) -> List[Dict[str, Any]]:
|
2023-04-13 21:25:49 +00:00
|
|
|
url: str = (
|
|
|
|
f"{const.API_URL}/commands/v1/appliance/{appliance.mac_address}/history"
|
|
|
|
)
|
2023-04-09 16:13:50 +00:00
|
|
|
async with self._hon.get(url) as response:
|
2023-06-28 17:02:11 +00:00
|
|
|
result: Dict[str, Any] = await response.json()
|
2023-03-11 01:31:56 +00:00
|
|
|
if not result or not result.get("payload"):
|
2023-06-25 15:29:04 +00:00
|
|
|
return []
|
2023-03-11 01:31:56 +00:00
|
|
|
return result["payload"]["history"]
|
|
|
|
|
2023-06-25 15:29:04 +00:00
|
|
|
async def load_favourites(self, appliance: HonAppliance) -> List[Dict[str, Any]]:
|
2023-05-14 21:04:36 +00:00
|
|
|
url: str = (
|
|
|
|
f"{const.API_URL}/commands/v1/appliance/{appliance.mac_address}/favourite"
|
|
|
|
)
|
|
|
|
async with self._hon.get(url) as response:
|
2023-06-28 17:02:11 +00:00
|
|
|
result: Dict[str, Any] = await response.json()
|
2023-05-14 21:04:36 +00:00
|
|
|
if not result or not result.get("payload"):
|
2023-06-25 15:29:04 +00:00
|
|
|
return []
|
2023-05-14 21:04:36 +00:00
|
|
|
return result["payload"]["favourites"]
|
|
|
|
|
2023-06-25 15:29:04 +00:00
|
|
|
async def load_last_activity(self, appliance: HonAppliance) -> Dict[str, Any]:
|
2023-04-13 21:25:49 +00:00
|
|
|
url: str = f"{const.API_URL}/commands/v1/retrieve-last-activity"
|
2023-06-28 17:02:11 +00:00
|
|
|
params: Dict[str, str] = {"macAddress": appliance.mac_address}
|
2023-04-09 16:13:50 +00:00
|
|
|
async with self._hon.get(url, params=params) as response:
|
2023-06-28 17:02:11 +00:00
|
|
|
result: Dict[str, Any] = await response.json()
|
2023-03-19 00:08:54 +00:00
|
|
|
if result and (activity := result.get("attributes")):
|
|
|
|
return activity
|
|
|
|
return {}
|
|
|
|
|
2023-06-25 15:29:04 +00:00
|
|
|
async def load_appliance_data(self, appliance: HonAppliance) -> Dict[str, Any]:
|
2023-05-06 14:07:28 +00:00
|
|
|
url: str = f"{const.API_URL}/commands/v1/appliance-model"
|
2023-06-28 17:02:11 +00:00
|
|
|
params: Dict[str, str] = {
|
2023-06-25 15:29:04 +00:00
|
|
|
"code": appliance.code,
|
2023-05-06 14:07:28 +00:00
|
|
|
"macAddress": appliance.mac_address,
|
|
|
|
}
|
|
|
|
async with self._hon.get(url, params=params) as response:
|
2023-06-28 17:02:11 +00:00
|
|
|
result: Dict[str, Any] = await response.json()
|
2023-06-25 15:29:04 +00:00
|
|
|
if result:
|
|
|
|
return result.get("payload", {}).get("applianceModel", {})
|
2023-05-06 14:07:28 +00:00
|
|
|
return {}
|
|
|
|
|
2023-06-25 15:29:04 +00:00
|
|
|
async def load_attributes(self, appliance: HonAppliance) -> Dict[str, Any]:
|
2023-06-28 17:02:11 +00:00
|
|
|
params: Dict[str, str] = {
|
2023-04-09 18:50:28 +00:00
|
|
|
"macAddress": appliance.mac_address,
|
|
|
|
"applianceType": appliance.appliance_type,
|
2023-04-09 18:55:36 +00:00
|
|
|
"category": "CYCLE",
|
2023-02-13 00:41:38 +00:00
|
|
|
}
|
2023-04-13 21:25:49 +00:00
|
|
|
url: str = f"{const.API_URL}/commands/v1/context"
|
2023-04-09 16:13:50 +00:00
|
|
|
async with self._hon.get(url, params=params) as response:
|
2023-02-13 00:41:38 +00:00
|
|
|
return (await response.json()).get("payload", {})
|
|
|
|
|
2023-06-25 15:29:04 +00:00
|
|
|
async def load_statistics(self, appliance: HonAppliance) -> Dict[str, Any]:
|
2023-06-28 17:02:11 +00:00
|
|
|
params: Dict[str, str] = {
|
2023-04-09 18:50:28 +00:00
|
|
|
"macAddress": appliance.mac_address,
|
2023-04-09 18:55:36 +00:00
|
|
|
"applianceType": appliance.appliance_type,
|
2023-02-13 00:41:38 +00:00
|
|
|
}
|
2023-04-13 21:25:49 +00:00
|
|
|
url: str = f"{const.API_URL}/commands/v1/statistics"
|
2023-04-09 16:13:50 +00:00
|
|
|
async with self._hon.get(url, params=params) as response:
|
2023-02-13 00:41:38 +00:00
|
|
|
return (await response.json()).get("payload", {})
|
|
|
|
|
2023-06-25 15:29:04 +00:00
|
|
|
async def load_maintenance(self, appliance: HonAppliance) -> Dict[str, Any]:
|
2023-05-12 22:16:52 +00:00
|
|
|
url = f"{const.API_URL}/commands/v1/maintenance-cycle"
|
|
|
|
params = {"macAddress": appliance.mac_address}
|
|
|
|
async with self._hon.get(url, params=params) as response:
|
|
|
|
return (await response.json()).get("payload", {})
|
|
|
|
|
2023-04-13 21:25:49 +00:00
|
|
|
async def send_command(
|
|
|
|
self,
|
|
|
|
appliance: HonAppliance,
|
|
|
|
command: str,
|
2023-06-28 17:02:11 +00:00
|
|
|
parameters: Dict[str, Any],
|
|
|
|
ancillary_parameters: Dict[str, Any],
|
2023-04-13 21:25:49 +00:00
|
|
|
) -> bool:
|
|
|
|
now: str = datetime.utcnow().isoformat()
|
2023-06-28 17:02:11 +00:00
|
|
|
data: Dict[str, Any] = {
|
2023-04-09 18:50:28 +00:00
|
|
|
"macAddress": appliance.mac_address,
|
2023-02-13 00:41:38 +00:00
|
|
|
"timestamp": f"{now[:-3]}Z",
|
|
|
|
"commandName": command,
|
2023-04-09 18:50:28 +00:00
|
|
|
"transactionId": f"{appliance.mac_address}_{now[:-3]}Z",
|
2023-05-21 00:25:43 +00:00
|
|
|
"applianceOptions": appliance.options,
|
2023-04-14 22:29:24 +00:00
|
|
|
"device": self._hon.device.get(mobile=True),
|
2023-02-13 00:41:38 +00:00
|
|
|
"attributes": {
|
|
|
|
"channel": "mobileApp",
|
|
|
|
"origin": "standardProgram",
|
2023-04-09 18:55:36 +00:00
|
|
|
"energyLabel": "0",
|
2023-02-13 00:41:38 +00:00
|
|
|
},
|
|
|
|
"ancillaryParameters": ancillary_parameters,
|
|
|
|
"parameters": parameters,
|
2023-04-09 18:55:36 +00:00
|
|
|
"applianceType": appliance.appliance_type,
|
2023-02-13 00:41:38 +00:00
|
|
|
}
|
2023-04-13 21:25:49 +00:00
|
|
|
url: str = f"{const.API_URL}/commands/v1/send"
|
2023-04-14 22:29:24 +00:00
|
|
|
async with self._hon.post(url, json=data) as response:
|
2023-06-28 17:02:11 +00:00
|
|
|
json_data: Dict[str, Any] = await response.json()
|
2023-04-09 18:50:28 +00:00
|
|
|
if json_data.get("payload", {}).get("resultCode") == "0":
|
2023-02-13 00:41:38 +00:00
|
|
|
return True
|
2023-04-14 22:29:24 +00:00
|
|
|
_LOGGER.error(await response.text())
|
2023-06-09 00:09:20 +00:00
|
|
|
_LOGGER.error("%s - Payload:\n%s", url, pformat(data))
|
2023-02-13 00:41:38 +00:00
|
|
|
return False
|
2023-04-09 16:13:50 +00:00
|
|
|
|
2023-06-25 15:29:04 +00:00
|
|
|
async def appliance_configuration(self) -> Dict[str, Any]:
|
2023-05-06 14:07:28 +00:00
|
|
|
url: str = f"{const.API_URL}/config/v1/program-list-rules"
|
2023-04-09 16:13:50 +00:00
|
|
|
async with self._hon_anonymous.get(url) as response:
|
2023-06-28 17:02:11 +00:00
|
|
|
result: Dict[str, Any] = await response.json()
|
2023-04-09 16:13:50 +00:00
|
|
|
if result and (data := result.get("payload")):
|
|
|
|
return data
|
|
|
|
return {}
|
|
|
|
|
2023-06-25 15:29:04 +00:00
|
|
|
async def app_config(
|
|
|
|
self, language: str = "en", beta: bool = True
|
|
|
|
) -> Dict[str, Any]:
|
2023-04-13 21:25:49 +00:00
|
|
|
url: str = f"{const.API_URL}/app-config"
|
2023-06-28 17:02:11 +00:00
|
|
|
payload_data: Dict[str, str | int] = {
|
2023-04-09 16:13:50 +00:00
|
|
|
"languageCode": language,
|
|
|
|
"beta": beta,
|
|
|
|
"appVersion": const.APP_VERSION,
|
2023-04-09 18:55:36 +00:00
|
|
|
"os": const.OS,
|
2023-04-09 16:13:50 +00:00
|
|
|
}
|
2023-04-13 21:25:49 +00:00
|
|
|
payload: str = json.dumps(payload_data, separators=(",", ":"))
|
2023-04-09 16:13:50 +00:00
|
|
|
async with self._hon_anonymous.post(url, data=payload) as response:
|
|
|
|
if (result := await response.json()) and (data := result.get("payload")):
|
|
|
|
return data
|
|
|
|
return {}
|
|
|
|
|
2023-06-25 15:29:04 +00:00
|
|
|
async def translation_keys(self, language: str = "en") -> Dict[str, Any]:
|
2023-04-09 16:13:50 +00:00
|
|
|
config = await self.app_config(language=language)
|
|
|
|
if url := config.get("language", {}).get("jsonPath"):
|
|
|
|
async with self._hon_anonymous.get(url) as response:
|
|
|
|
if result := await response.json():
|
|
|
|
return result
|
|
|
|
return {}
|
2023-04-09 18:50:28 +00:00
|
|
|
|
2023-04-13 21:25:49 +00:00
|
|
|
async def close(self) -> None:
|
|
|
|
if self._hon_handler is not None:
|
|
|
|
await self._hon_handler.close()
|
|
|
|
if self._hon_anonymous_handler is not None:
|
|
|
|
await self._hon_anonymous_handler.close()
|
2023-06-25 15:29:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestAPI(HonAPI):
|
2023-06-28 17:02:11 +00:00
|
|
|
def __init__(self, path: Path):
|
2023-06-25 15:29:04 +00:00
|
|
|
super().__init__()
|
|
|
|
self._anonymous = True
|
|
|
|
self._path: Path = path
|
|
|
|
|
2023-06-28 17:02:11 +00:00
|
|
|
def _load_json(self, appliance: HonAppliance, file: str) -> Dict[str, Any]:
|
2023-06-25 15:29:04 +00:00
|
|
|
directory = f"{appliance.appliance_type}_{appliance.appliance_model_id}".lower()
|
2023-06-29 20:08:17 +00:00
|
|
|
if (path := self._path / directory / f"{file}.json").exists():
|
|
|
|
with open(path, "r", encoding="utf-8") as json_file:
|
|
|
|
return json.loads(json_file.read())
|
|
|
|
_LOGGER.warning(f"Can't open {str(path)}")
|
|
|
|
return {}
|
2023-06-25 15:29:04 +00:00
|
|
|
|
|
|
|
async def load_appliances(self) -> List[Dict[str, Any]]:
|
|
|
|
result = []
|
|
|
|
for appliance in self._path.glob("*/"):
|
|
|
|
with open(
|
|
|
|
appliance / "appliance_data.json", "r", encoding="utf-8"
|
|
|
|
) as json_file:
|
|
|
|
result.append(json.loads(json_file.read()))
|
|
|
|
return result
|
|
|
|
|
|
|
|
async def load_commands(self, appliance: HonAppliance) -> Dict[str, Any]:
|
|
|
|
return self._load_json(appliance, "commands")
|
|
|
|
|
|
|
|
@no_type_check
|
|
|
|
async def load_command_history(
|
|
|
|
self, appliance: HonAppliance
|
|
|
|
) -> List[Dict[str, Any]]:
|
|
|
|
return self._load_json(appliance, "command_history")
|
|
|
|
|
|
|
|
async def load_favourites(self, appliance: HonAppliance) -> List[Dict[str, Any]]:
|
|
|
|
return []
|
|
|
|
|
|
|
|
async def load_last_activity(self, appliance: HonAppliance) -> Dict[str, Any]:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
async def load_appliance_data(self, appliance: HonAppliance) -> Dict[str, Any]:
|
|
|
|
return self._load_json(appliance, "appliance_data")
|
|
|
|
|
|
|
|
async def load_attributes(self, appliance: HonAppliance) -> Dict[str, Any]:
|
|
|
|
return self._load_json(appliance, "attributes")
|
|
|
|
|
|
|
|
async def load_statistics(self, appliance: HonAppliance) -> Dict[str, Any]:
|
|
|
|
return self._load_json(appliance, "statistics")
|
|
|
|
|
|
|
|
async def load_maintenance(self, appliance: HonAppliance) -> Dict[str, Any]:
|
|
|
|
return self._load_json(appliance, "maintenance")
|
|
|
|
|
|
|
|
async def send_command(
|
|
|
|
self,
|
|
|
|
appliance: HonAppliance,
|
|
|
|
command: str,
|
2023-06-28 17:02:11 +00:00
|
|
|
parameters: Dict[str, Any],
|
|
|
|
ancillary_parameters: Dict[str, Any],
|
2023-06-25 15:29:04 +00:00
|
|
|
) -> bool:
|
|
|
|
return True
|