Use test devies

This commit is contained in:
Andre Basche 2023-05-11 00:43:48 +02:00
parent 022da71800
commit 81c202d730
4 changed files with 71 additions and 12 deletions

0
pyhon/__main__.py Executable file → Normal file
View File

View File

@ -1,11 +1,13 @@
import importlib import importlib
import json
import logging import logging
from contextlib import suppress from contextlib import suppress
from datetime import datetime, timedelta from datetime import datetime, timedelta
from pathlib import Path
from typing import Optional, Dict, Any from typing import Optional, Dict, Any
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from pyhon import helper, exceptions from pyhon import helper
from pyhon.commands import HonCommand from pyhon.commands import HonCommand
from pyhon.parameter.base import HonParameter from pyhon.parameter.base import HonParameter
from pyhon.parameter.fixed import HonParameterFixed from pyhon.parameter.fixed import HonParameterFixed
@ -125,9 +127,7 @@ class HonAppliance:
return self._zone return self._zone
@property @property
def api(self) -> "HonAPI": def api(self) -> Optional["HonAPI"]:
if self._api is None:
raise exceptions.NoAuthenticationException
return self._api return self._api
async def _recover_last_command_states(self): async def _recover_last_command_states(self):
@ -279,3 +279,34 @@ class HonAppliance:
whitespace=whitespace, whitespace=whitespace,
) )
return result.replace(self.mac_address, "xx-xx-xx-xx-xx-xx") 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"

View File

@ -7,7 +7,7 @@ from pyhon.parameter.program import HonParameterProgram
from pyhon.parameter.range import HonParameterRange from pyhon.parameter.range import HonParameterRange
if TYPE_CHECKING: if TYPE_CHECKING:
from pyhon import HonAPI from pyhon import HonAPI, exceptions
from pyhon.appliance import HonAppliance from pyhon.appliance import HonAppliance
@ -20,7 +20,7 @@ class HonCommand:
categories: Optional[Dict[str, "HonCommand"]] = None, categories: Optional[Dict[str, "HonCommand"]] = None,
category_name: str = "", category_name: str = "",
): ):
self._api: HonAPI = appliance.api self._api: Optional[HonAPI] = appliance.api
self._appliance: "HonAppliance" = appliance self._appliance: "HonAppliance" = appliance
self._name: str = name self._name: str = name
self._categories: Optional[Dict[str, "HonCommand"]] = categories self._categories: Optional[Dict[str, "HonCommand"]] = categories
@ -39,6 +39,12 @@ class HonCommand:
def name(self): def name(self):
return self._name return self._name
@property
def api(self) -> "HonAPI":
if self._api is None:
raise exceptions.NoAuthenticationException
return self._api
@property @property
def data(self): def data(self):
return self._data return self._data
@ -87,7 +93,7 @@ class HonCommand:
async def send(self) -> bool: async def send(self) -> bool:
params = self.parameter_groups.get("parameters", {}) params = self.parameter_groups.get("parameters", {})
ancillary_params = self.parameter_groups.get("ancillaryParameters", {}) 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 self._appliance, self._name, params, ancillary_params
) )
@ -129,5 +135,6 @@ class HonCommand:
for name, parameter in command.parameters.items(): for name, parameter in command.parameters.items():
if name in result: if name in result:
result[name] = self._more_options(result[name], parameter) result[name] = self._more_options(result[name], parameter)
result[name] = parameter else:
result[name] = parameter
return result return result

View File

@ -10,9 +10,14 @@ from pyhon.appliance import HonAppliance
class Hon: class Hon:
def __init__(self, email: str, password: str, session: ClientSession | None = None): def __init__(
self._email: str = email self,
self._password: str = password 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._session: ClientSession | None = session
self._appliances: List[HonAppliance] = [] self._appliances: List[HonAppliance] = []
self._api: Optional[HonAPI] = None self._api: Optional[HonAPI] = None
@ -34,9 +39,21 @@ class Hon:
raise exceptions.NoAuthenticationException raise exceptions.NoAuthenticationException
return self._api 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: async def create(self) -> Self:
self._api = await HonAPI( self._api = await HonAPI(
self._email, self._password, session=self._session self.email, self.password, session=self._session
).create() ).create()
await self.setup() await self.setup()
return self return self
@ -45,6 +62,10 @@ class Hon:
def appliances(self) -> List[HonAppliance]: def appliances(self) -> List[HonAppliance]:
return self._appliances 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: async def _create_appliance(self, appliance_data: Dict[str, Any], zone=0) -> None:
appliance = HonAppliance(self._api, appliance_data, zone=zone) appliance = HonAppliance(self._api, appliance_data, zone=zone)
if appliance.mac_address == "": if appliance.mac_address == "":