pyh0n/pyhon/commands.py

113 lines
3.7 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",
2023-05-06 19:37:28 +05:30
categories: Optional[Dict[str, "HonCommand"]] = None,
category_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
2023-05-06 19:37:28 +05:30
self._categories: Optional[Dict[str, "HonCommand"]] = categories
self._category_name: str = category_name
self._description: str = attributes.pop("description", "")
self._protocol_type: str = attributes.pop("protocolType", "")
self._parameters: Dict[str, HonParameter] = {}
self._data = {}
self._load_parameters(attributes)
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-05-06 19:37:28 +05:30
@property
def name(self):
return self._name
@property
def data(self):
return self._data
2023-02-13 06:11:38 +05:30
@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
2023-05-06 19:37:28 +05:30
def _load_parameters(self, attributes):
for key, items in attributes.items():
for name, data in items.items():
self._create_parameters(data, name, key)
def _create_parameters(self, data: Dict, name: str, parameter: str) -> None:
if name == "zoneMap" and self._appliance.zone:
data["default"] = self._appliance.zone
match data.get("typology"):
case "range":
self._parameters[name] = HonParameterRange(name, data, parameter)
case "enum":
self._parameters[name] = HonParameterEnum(name, data, parameter)
case "fixed":
self._parameters[name] = HonParameterFixed(name, data, parameter)
case _:
self._data[name] = data
return
if self._category_name:
if not self._categories:
self._parameters["program"] = HonParameterProgram("program", self, name)
def _parameters_by_group(self, group):
return {
name: v.value for name, v in self._parameters.items() if v.group == group
}
2023-02-13 06:11:38 +05:30
2023-04-16 05:06:10 +05:30
async def send(self) -> bool:
2023-05-06 19:37:28 +05:30
params = self._parameters_by_group("parameters")
ancillary_params = self._parameters_by_group("ancillary_parameters")
2023-04-16 05:06:10 +05:30
return await self._api.send_command(
2023-04-17 03:31:28 +05:30
self._appliance, self._name, params, ancillary_params
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-05-06 19:37:28 +05:30
def categories(self) -> Dict[str, "HonCommand"]:
if self._categories is None:
return {"_": self}
return self._categories
2023-04-16 02:32:37 +05:30
@property
2023-05-06 19:37:28 +05:30
def category(self) -> str:
return self._category_name
@category.setter
def category(self, category: str) -> None:
self._appliance.commands[self._name] = self.categories[category]
2023-02-19 02:55:51 +05:30
@property
2023-04-16 05:06:10 +05:30
def setting_keys(self) -> List[str]:
2023-05-06 19:37:28 +05:30
return list(
{param for cmd in self.categories.values() for param in cmd.parameters}
)
2023-02-19 02:55:51 +05:30
@property
2023-04-16 05:06:10 +05:30
def settings(self) -> Dict[str, HonParameter]:
2023-05-06 19:37:28 +05:30
result = {}
for command in self.categories.values():
for name, parameter in command.parameters.items():
if name in result:
continue
result[name] = parameter
return result