pyh0n/pyhon/commands.py

119 lines
4.0 KiB
Python
Raw Normal View History

2023-04-16 05:06:10 +05:30
from typing import Optional, Dict, Any, List, TYPE_CHECKING
2023-04-16 05:13:37 +05:30
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 06:11:38 +05:30
2023-04-16 05:06:10 +05:30
if TYPE_CHECKING:
from pyhon import HonAPI
from pyhon.appliance import HonAppliance
2023-02-13 06:11:38 +05:30
class HonCommand:
2023-04-16 03:41:50 +05:30
def __init__(
2023-04-16 05:06:10 +05:30
self,
name: str,
attributes: Dict[str, Any],
api: "HonAPI",
appliance: "HonAppliance",
programs: Optional[Dict[str, "HonCommand"]] = None,
program_name: str = "",
2023-04-16 03:41:50 +05:30
):
2023-04-16 05:06:10 +05:30
self._api: HonAPI = api
self._appliance: "HonAppliance" = appliance
self._name: str = name
self._programs: Optional[Dict[str, "HonCommand"]] = programs or {}
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-10 00:25:36 +05:30
attributes.get("ancillaryParameters", {})
)
2023-02-19 02:55:51 +05:30
2023-04-16 05:06:10 +05:30
def __repr__(self) -> str:
2023-02-19 02:55:51 +05:30
return f"{self._name} command"
2023-02-13 06:11:38 +05:30
2023-04-16 05:06:10 +05:30
def _create_parameters(self, parameters: Dict) -> Dict[str, HonParameter]:
result: Dict[str, HonParameter] = {}
2023-02-13 06:11:38 +05:30
for parameter, attributes in parameters.items():
2023-04-16 05:06:10 +05:30
if parameter == "zoneMap" and self._appliance.zone:
attributes["default"] = self._appliance.zone
2023-02-13 06:11:38 +05:30
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-16 02:32:37 +05:30
if self._programs:
2023-02-19 08:00:48 +05:30
result["program"] = HonParameterProgram("program", self)
2023-02-13 06:11:38 +05:30
return result
@property
2023-04-16 05:06:10 +05:30
def parameters(self) -> Dict[str, HonParameter]:
2023-03-07 00:15:46 +05:30
return self._parameters
2023-02-13 06:11:38 +05:30
@property
2023-04-16 05:06:10 +05:30
def ancillary_parameters(self) -> Dict[str, str | float]:
2023-04-10 00:25:36 +05:30
return {
key: parameter.value
for key, parameter in self._ancillary_parameters.items()
}
2023-02-13 06:11:38 +05:30
2023-04-16 05:06:10 +05:30
async def send(self) -> bool:
2023-04-10 00:25:36 +05:30
parameters = {
name: parameter.value for name, parameter in self._parameters.items()
}
2023-04-16 05:06:10 +05:30
return await self._api.send_command(
self._appliance, self._name, parameters, self.ancillary_parameters
2023-04-10 00:25:36 +05:30
)
2023-02-13 06:11:38 +05:30
2023-04-16 02:32:37 +05:30
@property
2023-04-16 05:06:10 +05:30
def programs(self) -> Dict[str, "HonCommand"]:
if self._programs is None:
return {}
2023-04-16 02:32:37 +05:30
return self._programs
@property
2023-04-16 05:06:10 +05:30
def program(self) -> str:
2023-04-16 02:32:37 +05:30
return self._program_name
2023-02-13 06:11:38 +05:30
2023-04-16 02:32:37 +05:30
@program.setter
2023-04-16 05:06:10 +05:30
def program(self, program: str) -> None:
self._appliance.commands[self._name] = self.programs[program]
2023-02-19 02:55:51 +05:30
2023-04-16 05:06:10 +05:30
def _get_settings_keys(self, command: Optional["HonCommand"] = None) -> List[str]:
if command is None:
command = self
2023-02-19 02:55:51 +05:30
keys = []
for key, parameter in command._parameters.items():
if isinstance(parameter, HonParameterFixed):
continue
if key not in keys:
keys.append(key)
return keys
@property
2023-04-16 05:06:10 +05:30
def setting_keys(self) -> List[str]:
2023-04-16 02:32:37 +05:30
if not self._programs:
2023-02-19 02:55:51 +05:30
return self._get_settings_keys()
2023-04-10 00:25:36 +05:30
result = [
2023-04-16 03:41:50 +05:30
key
for cmd in self._programs.values()
for key in self._get_settings_keys(cmd)
2023-04-10 00:25:36 +05:30
]
2023-02-19 02:55:51 +05:30
return list(set(result + ["program"]))
@property
2023-04-16 05:06:10 +05:30
def settings(self) -> Dict[str, HonParameter]:
2023-03-05 02:49:48 +05:30
"""Parameters with typology enum and range"""
2023-04-10 00:25:36 +05:30
return {
2023-04-16 05:06:10 +05:30
s: param
2023-04-10 00:25:36 +05:30
for s in self.setting_keys
2023-04-16 05:06:10 +05:30
if (param := self._parameters.get(s)) is not None
2023-04-10 00:25:36 +05:30
}