From 1ed81c2a77c128ed6fb1b29d88cb496dc2ba895f Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Mon, 24 Jul 2023 02:25:24 +0200 Subject: [PATCH] Simplify get favorites --- pyhon/appliance.py | 2 +- pyhon/command_loader.py | 55 ++++++++++++++++++++++++++++------------- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/pyhon/appliance.py b/pyhon/appliance.py index 03c94a4..996c463 100644 --- a/pyhon/appliance.py +++ b/pyhon/appliance.py @@ -20,7 +20,7 @@ if TYPE_CHECKING: _LOGGER = logging.getLogger(__name__) -T = TypeVar('T') +T = TypeVar("T") # pylint: disable=too-many-public-methods,too-many-instance-attributes diff --git a/pyhon/command_loader.py b/pyhon/command_loader.py index adf34b8..94b0277 100644 --- a/pyhon/command_loader.py +++ b/pyhon/command_loader.py @@ -184,23 +184,44 @@ class HonCommandLoader: def _add_favourites(self) -> None: """Patch program categories with favourites""" for favourite in self._favourites: - name = favourite.get("favouriteName", {}) - command = favourite.get("command", {}) - command_name = command.get("commandName", "") - program_name = self._clean_name(command.get("programName", "")) - if not (base := self.commands[command_name].categories.get(program_name)): + name, command_name, base = self._get_favourite_info(favourite) + if not base: continue base_command: HonCommand = copy(base) - for data in command.values(): - if isinstance(data, str): + self._update_base_command_with_data(base_command, favourite) + self._update_base_command_with_favourite(base_command) + self._update_program_categories(command_name, name, base_command) + + def _get_favourite_info( + self, favourite: Dict[str, Any] + ) -> tuple[str, str, HonCommand | None]: + name: str = favourite.get("favouriteName", {}) + command = favourite.get("command", {}) + command_name: str = command.get("commandName", "") + program_name = self._clean_name(command.get("programName", "")) + base_command = self.commands[command_name].categories.get(program_name) + return name, command_name, base_command + + def _update_base_command_with_data( + self, base_command: HonCommand, command: Dict[str, Any] + ) -> None: + for data in command.values(): + if isinstance(data, str): + continue + for key, value in data.items(): + if not (parameter := base_command.parameters.get(key)): continue - for key, value in data.items(): - if parameter := base_command.parameters.get(key): - with suppress(ValueError): - parameter.value = value - extra_param = HonParameterFixed("favourite", {"fixedValue": "1"}, "custom") - base_command.parameters.update(favourite=extra_param) - program = base_command.parameters["program"] - if isinstance(program, HonParameterProgram): - program.set_value(name) - self.commands[command_name].categories[name] = base_command + with suppress(ValueError): + parameter.value = value + + def _update_base_command_with_favourite(self, base_command: HonCommand) -> None: + extra_param = HonParameterFixed("favourite", {"fixedValue": "1"}, "custom") + base_command.parameters.update(favourite=extra_param) + + def _update_program_categories( + self, command_name: str, name: str, base_command: HonCommand + ) -> None: + program = base_command.parameters["program"] + if isinstance(program, HonParameterProgram): + program.set_value(name) + self.commands[command_name].categories[name] = base_command