Simplify get favorites

This commit is contained in:
Andre Basche 2023-07-24 02:25:24 +02:00
parent e4dc3cb1d0
commit 1ed81c2a77
2 changed files with 39 additions and 18 deletions

View file

@ -20,7 +20,7 @@ if TYPE_CHECKING:
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
T = TypeVar('T') T = TypeVar("T")
# pylint: disable=too-many-public-methods,too-many-instance-attributes # pylint: disable=too-many-public-methods,too-many-instance-attributes

View file

@ -184,23 +184,44 @@ class HonCommandLoader:
def _add_favourites(self) -> None: def _add_favourites(self) -> None:
"""Patch program categories with favourites""" """Patch program categories with favourites"""
for favourite in self._favourites: for favourite in self._favourites:
name = favourite.get("favouriteName", {}) name, command_name, base = self._get_favourite_info(favourite)
command = favourite.get("command", {}) if not base:
command_name = command.get("commandName", "")
program_name = self._clean_name(command.get("programName", ""))
if not (base := self.commands[command_name].categories.get(program_name)):
continue continue
base_command: HonCommand = copy(base) base_command: HonCommand = copy(base)
for data in command.values(): self._update_base_command_with_data(base_command, favourite)
if isinstance(data, str): 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 continue
for key, value in data.items(): with suppress(ValueError):
if parameter := base_command.parameters.get(key): parameter.value = value
with suppress(ValueError):
parameter.value = value def _update_base_command_with_favourite(self, base_command: HonCommand) -> None:
extra_param = HonParameterFixed("favourite", {"fixedValue": "1"}, "custom") extra_param = HonParameterFixed("favourite", {"fixedValue": "1"}, "custom")
base_command.parameters.update(favourite=extra_param) base_command.parameters.update(favourite=extra_param)
program = base_command.parameters["program"]
if isinstance(program, HonParameterProgram): def _update_program_categories(
program.set_value(name) self, command_name: str, name: str, base_command: HonCommand
self.commands[command_name].categories[name] = base_command ) -> None:
program = base_command.parameters["program"]
if isinstance(program, HonParameterProgram):
program.set_value(name)
self.commands[command_name].categories[name] = base_command