pyhOn/pyhon/commands.py

142 lines
4.8 KiB
Python
Raw Normal View History

2023-05-06 18:00:13 +00:00
from typing import Optional, Dict, Any, List, TYPE_CHECKING, Union
2023-04-15 23:36:10 +00:00
2023-05-14 21:03:46 +00:00
from pyhon import exceptions
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:
2023-05-14 21:03:46 +00:00
from pyhon import HonAPI
2023-04-15 23:36:10 +00:00
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],
appliance: "HonAppliance",
2023-05-06 14:07:28 +00:00
categories: Optional[Dict[str, "HonCommand"]] = None,
category_name: str = "",
2023-04-15 22:11:50 +00:00
):
2023-05-10 22:43:48 +00:00
self._api: Optional[HonAPI] = appliance.api
2023-04-15 23:36:10 +00:00
self._appliance: "HonAppliance" = appliance
self._name: str = name
2023-05-06 14:07:28 +00:00
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] = {}
2023-05-06 18:00:13 +00:00
self._data: Dict[str, Any] = {}
2023-05-06 22:28:24 +00:00
self._available_settings: Dict[str, HonParameter] = {}
2023-05-06 14:07:28 +00:00
self._load_parameters(attributes)
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-05-06 14:07:28 +00:00
@property
def name(self):
return self._name
2023-05-10 22:43:48 +00:00
@property
def api(self) -> "HonAPI":
if self._api is None:
raise exceptions.NoAuthenticationException
return self._api
2023-05-06 14:07:28 +00:00
@property
def data(self):
return self._data
2023-02-13 00:41:38 +00:00
@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
2023-05-06 22:28:24 +00:00
@property
def settings(self) -> Dict[str, HonParameter]:
return self._parameters
2023-05-06 18:00:13 +00:00
@property
def parameter_groups(self) -> Dict[str, Dict[str, Union[str, float]]]:
result: Dict[str, Dict[str, Union[str, float]]] = {}
for name, parameter in self._parameters.items():
result.setdefault(parameter.group, {})[name] = parameter.value
return result
@property
def parameter_value(self) -> Dict[str, Union[str, float]]:
return {n: p.value for n, p in self._parameters.items()}
2023-05-06 14:07:28 +00:00
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:
2023-05-06 22:28:24 +00:00
name = "program" if "PROGRAM" in self._category_name else "category"
self._parameters[name] = HonParameterProgram(name, self, "custom")
2023-02-13 00:41:38 +00:00
2023-04-15 23:36:10 +00:00
async def send(self) -> bool:
2023-05-06 23:17:02 +00:00
params = self.parameter_groups.get("parameters", {})
ancillary_params = self.parameter_groups.get("ancillaryParameters", {})
2023-05-10 22:43:48 +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-05-06 14:07:28 +00:00
def categories(self) -> Dict[str, "HonCommand"]:
if self._categories is None:
return {"_": self}
return self._categories
2023-04-15 21:02:37 +00:00
@property
2023-05-06 14:07:28 +00:00
def category(self) -> str:
return self._category_name
@category.setter
def category(self, category: str) -> None:
2023-05-07 22:03:29 +00:00
if category in self.categories:
self._appliance.commands[self._name] = self.categories[category]
2023-02-18 21:25:51 +00:00
@property
2023-04-15 23:36:10 +00:00
def setting_keys(self) -> List[str]:
2023-05-06 14:07:28 +00:00
return list(
{param for cmd in self.categories.values() for param in cmd.parameters}
)
2023-02-18 21:25:51 +00:00
2023-05-06 18:00:13 +00:00
@staticmethod
def _more_options(first: HonParameter, second: HonParameter):
if isinstance(first, HonParameterFixed) and not isinstance(
second, HonParameterFixed
):
return second
if len(second.values) > len(first.values):
return second
return first
2023-02-18 21:25:51 +00:00
@property
2023-05-06 22:28:24 +00:00
def available_settings(self) -> Dict[str, HonParameter]:
2023-05-06 18:00:13 +00:00
result: Dict[str, HonParameter] = {}
2023-05-06 14:07:28 +00:00
for command in self.categories.values():
for name, parameter in command.parameters.items():
if name in result:
2023-05-06 18:00:13 +00:00
result[name] = self._more_options(result[name], parameter)
2023-05-10 22:43:48 +00:00
else:
result[name] = parameter
2023-05-06 14:07:28 +00:00
return result