Skip to content

Commit

Permalink
Simplify get favorites
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre0512 committed Jul 24, 2023
1 parent e4dc3cb commit 1ed81c2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pyhon/appliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

_LOGGER = logging.getLogger(__name__)

T = TypeVar('T')
T = TypeVar("T")


# pylint: disable=too-many-public-methods,too-many-instance-attributes
Expand Down
55 changes: 38 additions & 17 deletions pyhon/command_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,23 +184,44 @@ def _recover_last_command_states(self) -> None:
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

0 comments on commit 1ed81c2

Please sign in to comment.