pyhOn/pyhon/commands.py

119 lines
4.0 KiB
Python
Raw Normal View History

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