2023-04-15 23:36:10 +00:00
|
|
|
from typing import Optional, Dict, Any, List, TYPE_CHECKING
|
|
|
|
|
2023-04-15 23:43:37 +00:00
|
|
|
from pyhon.parameter.base import HonParameter
|
|
|
|
from pyhon.parameter.enum import HonParameterEnum
|
|
|
|
from pyhon.parameter.fixed import HonParameterFixed
|
|
|
|
from pyhon.parameter.program import HonParameterProgram
|
|
|
|
from pyhon.parameter.range import HonParameterRange
|
2023-02-13 00:41:38 +00:00
|
|
|
|
2023-04-15 23:36:10 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from pyhon import HonAPI
|
|
|
|
from pyhon.appliance import HonAppliance
|
|
|
|
|
2023-02-13 00:41:38 +00:00
|
|
|
|
|
|
|
class HonCommand:
|
2023-04-15 22:11:50 +00:00
|
|
|
def __init__(
|
2023-04-15 23:36:10 +00:00
|
|
|
self,
|
|
|
|
name: str,
|
|
|
|
attributes: Dict[str, Any],
|
|
|
|
api: "HonAPI",
|
|
|
|
appliance: "HonAppliance",
|
|
|
|
programs: Optional[Dict[str, "HonCommand"]] = None,
|
|
|
|
program_name: str = "",
|
2023-04-15 22:11:50 +00:00
|
|
|
):
|
2023-04-15 23:36:10 +00:00
|
|
|
self._api: HonAPI = api
|
|
|
|
self._appliance: "HonAppliance" = appliance
|
|
|
|
self._name: str = name
|
2023-04-24 02:33:00 +00:00
|
|
|
self._programs: Optional[Dict[str, "HonCommand"]] = programs
|
2023-04-15 23:36:10 +00:00
|
|
|
self._program_name: str = program_name
|
|
|
|
self._description: str = attributes.get("description", "")
|
|
|
|
self._parameters: Dict[str, HonParameter] = self._create_parameters(
|
|
|
|
attributes.get("parameters", {})
|
|
|
|
)
|
|
|
|
self._ancillary_parameters: Dict[str, HonParameter] = self._create_parameters(
|
2023-04-09 18:55:36 +00:00
|
|
|
attributes.get("ancillaryParameters", {})
|
|
|
|
)
|
2023-02-18 21:25:51 +00:00
|
|
|
|
2023-04-15 23:36:10 +00:00
|
|
|
def __repr__(self) -> str:
|
2023-02-18 21:25:51 +00:00
|
|
|
return f"{self._name} command"
|
2023-02-13 00:41:38 +00:00
|
|
|
|
2023-04-15 23:36:10 +00:00
|
|
|
def _create_parameters(self, parameters: Dict) -> Dict[str, HonParameter]:
|
|
|
|
result: Dict[str, HonParameter] = {}
|
2023-02-13 00:41:38 +00:00
|
|
|
for parameter, attributes in parameters.items():
|
2023-04-15 23:36:10 +00:00
|
|
|
if parameter == "zoneMap" and self._appliance.zone:
|
|
|
|
attributes["default"] = self._appliance.zone
|
2023-02-13 00:41:38 +00:00
|
|
|
match attributes.get("typology"):
|
|
|
|
case "range":
|
|
|
|
result[parameter] = HonParameterRange(parameter, attributes)
|
|
|
|
case "enum":
|
|
|
|
result[parameter] = HonParameterEnum(parameter, attributes)
|
|
|
|
case "fixed":
|
|
|
|
result[parameter] = HonParameterFixed(parameter, attributes)
|
2023-04-15 21:02:37 +00:00
|
|
|
if self._programs:
|
2023-02-19 02:30:48 +00:00
|
|
|
result["program"] = HonParameterProgram("program", self)
|
2023-02-13 00:41:38 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
@property
|
2023-04-15 23:36:10 +00:00
|
|
|
def parameters(self) -> Dict[str, HonParameter]:
|
2023-03-06 18:45:46 +00:00
|
|
|
return self._parameters
|
2023-02-13 00:41:38 +00:00
|
|
|
|
|
|
|
@property
|
2023-04-16 01:29:28 +00:00
|
|
|
def ancillary_parameters(self) -> Dict[str, HonParameter]:
|
|
|
|
return self._ancillary_parameters
|
2023-02-13 00:41:38 +00:00
|
|
|
|
2023-04-15 23:36:10 +00:00
|
|
|
async def send(self) -> bool:
|
2023-04-16 22:01:28 +00:00
|
|
|
params = {k: v.value for k, v in self._parameters.items()}
|
|
|
|
ancillary_params = {k: v.value for k, v in self._ancillary_parameters.items()}
|
2023-04-15 23:36:10 +00:00
|
|
|
return await self._api.send_command(
|
2023-04-16 22:01:28 +00:00
|
|
|
self._appliance, self._name, params, ancillary_params
|
2023-04-09 18:55:36 +00:00
|
|
|
)
|
2023-02-13 00:41:38 +00:00
|
|
|
|
2023-04-15 21:02:37 +00:00
|
|
|
@property
|
2023-04-15 23:36:10 +00:00
|
|
|
def programs(self) -> Dict[str, "HonCommand"]:
|
|
|
|
if self._programs is None:
|
|
|
|
return {}
|
2023-04-15 21:02:37 +00:00
|
|
|
return self._programs
|
|
|
|
|
|
|
|
@property
|
2023-04-15 23:36:10 +00:00
|
|
|
def program(self) -> str:
|
2023-04-15 21:02:37 +00:00
|
|
|
return self._program_name
|
2023-02-13 00:41:38 +00:00
|
|
|
|
2023-04-15 21:02:37 +00:00
|
|
|
@program.setter
|
2023-04-15 23:36:10 +00:00
|
|
|
def program(self, program: str) -> None:
|
|
|
|
self._appliance.commands[self._name] = self.programs[program]
|
2023-02-18 21:25:51 +00:00
|
|
|
|
2023-04-15 23:36:10 +00:00
|
|
|
def _get_settings_keys(self, command: Optional["HonCommand"] = None) -> List[str]:
|
|
|
|
if command is None:
|
|
|
|
command = self
|
2023-02-18 21:25:51 +00:00
|
|
|
keys = []
|
2023-04-16 01:29:28 +00:00
|
|
|
for key, parameter in (
|
|
|
|
command._parameters | command._ancillary_parameters
|
|
|
|
).items():
|
2023-02-18 21:25:51 +00:00
|
|
|
if key not in keys:
|
|
|
|
keys.append(key)
|
|
|
|
return keys
|
|
|
|
|
|
|
|
@property
|
2023-04-15 23:36:10 +00:00
|
|
|
def setting_keys(self) -> List[str]:
|
2023-04-15 21:02:37 +00:00
|
|
|
if not self._programs:
|
2023-02-18 21:25:51 +00:00
|
|
|
return self._get_settings_keys()
|
2023-04-09 18:55:36 +00:00
|
|
|
result = [
|
2023-04-15 22:11:50 +00:00
|
|
|
key
|
|
|
|
for cmd in self._programs.values()
|
|
|
|
for key in self._get_settings_keys(cmd)
|
2023-04-09 18:55:36 +00:00
|
|
|
]
|
2023-02-18 21:25:51 +00:00
|
|
|
return list(set(result + ["program"]))
|
|
|
|
|
|
|
|
@property
|
2023-04-15 23:36:10 +00:00
|
|
|
def settings(self) -> Dict[str, HonParameter]:
|
2023-04-09 18:55:36 +00:00
|
|
|
return {
|
2023-04-15 23:36:10 +00:00
|
|
|
s: param
|
2023-04-09 18:55:36 +00:00
|
|
|
for s in self.setting_keys
|
2023-04-15 23:36:10 +00:00
|
|
|
if (param := self._parameters.get(s)) is not None
|
2023-04-16 01:29:28 +00:00
|
|
|
or (param := self._ancillary_parameters.get(s)) is not None
|
2023-04-09 18:55:36 +00:00
|
|
|
}
|