pyh0n/pyhon/commands.py

103 lines
3.1 KiB
Python
Raw Normal View History

2023-04-10 00:25:36 +05:30
from pyhon.parameter import (
HonParameterFixed,
HonParameterEnum,
HonParameterRange,
HonParameterProgram,
)
2023-02-13 06:11:38 +05:30
class HonCommand:
2023-04-16 03:41:50 +05:30
def __init__(
self, name: str, attributes, connector, device, programs=None, program_name=""
):
2023-02-13 06:11:38 +05:30
self._connector = connector
self._device = device
self._name = name
2023-04-16 02:32:37 +05:30
self._programs = programs or {}
self._program_name = program_name
2023-02-13 06:11:38 +05:30
self._description = attributes.get("description", "")
self._parameters = self._create_parameters(attributes.get("parameters", {}))
2023-04-10 00:25:36 +05:30
self._ancillary_parameters = self._create_parameters(
attributes.get("ancillaryParameters", {})
)
2023-02-19 02:55:51 +05:30
def __repr__(self):
return f"{self._name} command"
2023-02-13 06:11:38 +05:30
def _create_parameters(self, parameters):
result = {}
for parameter, attributes in parameters.items():
2023-04-15 07:42:38 +05:30
if parameter == "zoneMap" and self._device.zone:
attributes["default"] = self._device.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
def parameters(self):
2023-03-07 00:15:46 +05:30
return self._parameters
2023-02-13 06:11:38 +05:30
@property
def ancillary_parameters(self):
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
async def send(self):
2023-04-10 00:25:36 +05:30
parameters = {
name: parameter.value for name, parameter in self._parameters.items()
}
return await self._connector.send_command(
self._device, self._name, parameters, self.ancillary_parameters
)
2023-02-13 06:11:38 +05:30
2023-04-16 02:32:37 +05:30
@property
def programs(self):
return self._programs
@property
def program(self):
return self._program_name
2023-02-13 06:11:38 +05:30
2023-04-16 02:32:37 +05:30
@program.setter
def program(self, program):
self._device.commands[self._name] = self._programs[program]
2023-02-19 02:55:51 +05:30
def _get_settings_keys(self, command=None):
command = command or self
keys = []
for key, parameter in command._parameters.items():
if isinstance(parameter, HonParameterFixed):
continue
if key not in keys:
keys.append(key)
return keys
@property
def setting_keys(self):
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
def settings(self):
2023-03-05 02:49:48 +05:30
"""Parameters with typology enum and range"""
2023-04-10 00:25:36 +05:30
return {
s: self._parameters.get(s)
for s in self.setting_keys
if self._parameters.get(s) is not None
}