From 81c202d7303acdf90e3517ffe5f8da188a1845b0 Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Thu, 11 May 2023 00:43:48 +0200 Subject: [PATCH] Use test devies --- pyhon/__main__.py | 0 pyhon/appliance.py | 39 +++++++++++++++++++++++++++++++++++---- pyhon/commands.py | 15 +++++++++++---- pyhon/hon.py | 29 +++++++++++++++++++++++++---- 4 files changed, 71 insertions(+), 12 deletions(-) mode change 100755 => 100644 pyhon/__main__.py diff --git a/pyhon/__main__.py b/pyhon/__main__.py old mode 100755 new mode 100644 diff --git a/pyhon/appliance.py b/pyhon/appliance.py index 4c75100..11749da 100644 --- a/pyhon/appliance.py +++ b/pyhon/appliance.py @@ -1,11 +1,13 @@ import importlib +import json import logging from contextlib import suppress from datetime import datetime, timedelta +from pathlib import Path from typing import Optional, Dict, Any from typing import TYPE_CHECKING -from pyhon import helper, exceptions +from pyhon import helper from pyhon.commands import HonCommand from pyhon.parameter.base import HonParameter from pyhon.parameter.fixed import HonParameterFixed @@ -125,9 +127,7 @@ class HonAppliance: return self._zone @property - def api(self) -> "HonAPI": - if self._api is None: - raise exceptions.NoAuthenticationException + def api(self) -> Optional["HonAPI"]: return self._api async def _recover_last_command_states(self): @@ -279,3 +279,34 @@ class HonAppliance: whitespace=whitespace, ) return result.replace(self.mac_address, "xx-xx-xx-xx-xx-xx") + + +class HonApplianceTest(HonAppliance): + def __init__(self, name): + super().__init__(None, {}) + self._name = name + self.load_commands() + self._info = self._appliance_model + + def load_commands(self): + device = Path(__file__).parent / "test_data" / f"{self._name}.json" + with open(str(device)) as f: + raw = json.loads(f.read()) + self._appliance_model = raw.pop("applianceModel") + raw.pop("dictionaryId", None) + self._commands = self._get_commands(raw) + + async def update(self): + return + + @property + def nick_name(self) -> str: + return self._name + + @property + def unique_id(self) -> str: + return self._name + + @property + def mac_address(self) -> str: + return "xx-xx-xx-xx-xx-xx" diff --git a/pyhon/commands.py b/pyhon/commands.py index 89158c1..532f2cd 100644 --- a/pyhon/commands.py +++ b/pyhon/commands.py @@ -7,7 +7,7 @@ from pyhon.parameter.program import HonParameterProgram from pyhon.parameter.range import HonParameterRange if TYPE_CHECKING: - from pyhon import HonAPI + from pyhon import HonAPI, exceptions from pyhon.appliance import HonAppliance @@ -20,7 +20,7 @@ class HonCommand: categories: Optional[Dict[str, "HonCommand"]] = None, category_name: str = "", ): - self._api: HonAPI = appliance.api + self._api: Optional[HonAPI] = appliance.api self._appliance: "HonAppliance" = appliance self._name: str = name self._categories: Optional[Dict[str, "HonCommand"]] = categories @@ -39,6 +39,12 @@ class HonCommand: def name(self): return self._name + @property + def api(self) -> "HonAPI": + if self._api is None: + raise exceptions.NoAuthenticationException + return self._api + @property def data(self): return self._data @@ -87,7 +93,7 @@ class HonCommand: async def send(self) -> bool: params = self.parameter_groups.get("parameters", {}) ancillary_params = self.parameter_groups.get("ancillaryParameters", {}) - return await self._api.send_command( + return await self.api.send_command( self._appliance, self._name, params, ancillary_params ) @@ -129,5 +135,6 @@ class HonCommand: for name, parameter in command.parameters.items(): if name in result: result[name] = self._more_options(result[name], parameter) - result[name] = parameter + else: + result[name] = parameter return result diff --git a/pyhon/hon.py b/pyhon/hon.py index 0dfde58..e7b5e9b 100644 --- a/pyhon/hon.py +++ b/pyhon/hon.py @@ -10,9 +10,14 @@ from pyhon.appliance import HonAppliance class Hon: - def __init__(self, email: str, password: str, session: ClientSession | None = None): - self._email: str = email - self._password: str = password + def __init__( + self, + email: Optional[str] = "", + password: Optional[str] = "", + session: Optional[ClientSession] = None, + ): + self._email: Optional[str] = email + self._password: Optional[str] = password self._session: ClientSession | None = session self._appliances: List[HonAppliance] = [] self._api: Optional[HonAPI] = None @@ -34,9 +39,21 @@ class Hon: raise exceptions.NoAuthenticationException return self._api + @property + def email(self) -> str: + if not self._email: + raise ValueError("Missing email") + return self._email + + @property + def password(self) -> str: + if not self._password: + raise ValueError("Missing password") + return self._password + async def create(self) -> Self: self._api = await HonAPI( - self._email, self._password, session=self._session + self.email, self.password, session=self._session ).create() await self.setup() return self @@ -45,6 +62,10 @@ class Hon: def appliances(self) -> List[HonAppliance]: return self._appliances + @appliances.setter + def appliances(self, appliances) -> None: + self._appliances = appliances + async def _create_appliance(self, appliance_data: Dict[str, Any], zone=0) -> None: appliance = HonAppliance(self._api, appliance_data, zone=zone) if appliance.mac_address == "":