pyhOn/pyhon/commands.py

93 lines
2.9 KiB
Python
Raw Normal View History

2023-04-09 20:55:36 +02:00
from pyhon.parameter import (
HonParameterFixed,
HonParameterEnum,
HonParameterRange,
HonParameterProgram,
)
2023-02-13 01:41:38 +01:00
class HonCommand:
2023-03-11 01:10:27 +01:00
def __init__(self, name, attributes, connector, device, multi=None, program=""):
2023-02-13 01:41:38 +01:00
self._connector = connector
self._device = device
self._name = name
2023-02-18 22:25:51 +01:00
self._multi = multi or {}
2023-03-11 01:10:27 +01:00
self._program = program
2023-02-13 01:41:38 +01:00
self._description = attributes.get("description", "")
self._parameters = self._create_parameters(attributes.get("parameters", {}))
2023-04-09 20:55:36 +02:00
self._ancillary_parameters = self._create_parameters(
attributes.get("ancillaryParameters", {})
)
2023-02-18 22:25:51 +01:00
def __repr__(self):
return f"{self._name} command"
2023-02-13 01:41:38 +01:00
def _create_parameters(self, parameters):
result = {}
for parameter, attributes in parameters.items():
2023-04-15 04:12:38 +02:00
if parameter == "zoneMap" and self._device.zone:
attributes["default"] = self._device.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-02-18 22:25:51 +01:00
if self._multi:
2023-02-19 03:30:48 +01:00
result["program"] = HonParameterProgram("program", self)
2023-02-13 01:41:38 +01:00
return result
@property
def parameters(self):
2023-03-06 19:45:46 +01:00
return self._parameters
2023-02-13 01:41:38 +01:00
@property
def ancillary_parameters(self):
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
async def send(self):
2023-04-09 20:55:36 +02:00
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 01:41:38 +01:00
2023-02-19 03:30:48 +01:00
def get_programs(self):
2023-02-13 01:41:38 +01:00
return self._multi
2023-02-19 03:30:48 +01:00
def set_program(self, program):
2023-02-13 01:41:38 +01:00
self._device.commands[self._name] = self._multi[program]
2023-02-18 22:25:51 +01:00
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):
if not self._multi:
return self._get_settings_keys()
2023-04-09 20:55:36 +02:00
result = [
key for cmd in self._multi.values() for key in self._get_settings_keys(cmd)
]
2023-02-18 22:25:51 +01:00
return list(set(result + ["program"]))
@property
def settings(self):
2023-03-04 22:19:48 +01:00
"""Parameters with typology enum and range"""
2023-04-09 20:55:36 +02:00
return {
s: self._parameters.get(s)
for s in self.setting_keys
if self._parameters.get(s) is not None
}