Skip to content

Commit

Permalink
* Consolidate param operations in param module
Browse files Browse the repository at this point in the history
* Convert param retrieval functions to property
  • Loading branch information
crowbarz committed Dec 6, 2024
1 parent 2bd26b9 commit 2c7bc3f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 56 deletions.
29 changes: 9 additions & 20 deletions aiopioneer/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,10 @@ async def cli_main(args: argparse.Namespace):
_LOGGER.error("could not connect to AVR: %s: %s", type(exc).__name__, exc.args)
return False

set_log_level("debug")
params = pioneer.get_user_params()
params[PARAM_DEBUG_LISTENER] = True
print(f"Setting default params to: {params}")
pioneer.set_user_params(params)

await pioneer.query_device_model()
set_log_level("debug")
print(f"Using default params: {pioneer.user_params}")
pioneer.set_user_param(PARAM_DEBUG_LISTENER, True)
if args.query_zones:
await pioneer.query_zones()
_LOGGER.info("AVR zones discovered: %s", pioneer.zones)
Expand Down Expand Up @@ -130,9 +127,9 @@ async def cli_main(args: argparse.Namespace):
f"Available listening modes: {json.dumps(pioneer.get_listening_modes())}"
)
elif cmd == "get_params":
print(f"Params: {json.dumps(pioneer.get_params())}")
print(f"Params: {json.dumps(pioneer.params_all)}")
elif cmd == "get_user_params":
print(f"User Params: {json.dumps(pioneer.get_user_params())}")
print(f"User Params: {json.dumps(pioneer.user_params)}")
elif cmd == "set_user_params":
try:
params = json.loads(arg)
Expand Down Expand Up @@ -166,28 +163,20 @@ async def cli_main(args: argparse.Namespace):

elif cmd == "debug_listener":
arg_bool = get_bool_arg(arg)
params = pioneer.get_user_params()
params[PARAM_DEBUG_LISTENER] = arg_bool
print(f"Setting debug_listener to: {arg_bool}")
pioneer.set_user_params(params)
pioneer.set_user_param(PARAM_DEBUG_LISTENER, arg_bool)
elif cmd == "debug_responder":
arg_bool = get_bool_arg(arg)
params = pioneer.get_user_params()
params[PARAM_DEBUG_RESPONDER] = arg_bool
print(f"Setting debug_responder to: {arg_bool}")
pioneer.set_user_params(params)
pioneer.set_user_param(PARAM_DEBUG_RESPONDER, arg_bool)
elif cmd == "debug_updater":
arg_bool = get_bool_arg(arg)
params = pioneer.get_user_params()
params[PARAM_DEBUG_UPDATER] = arg_bool
print(f"Setting debug_updater to: {arg_bool}")
pioneer.set_user_params(params)
pioneer.set_user_param(PARAM_DEBUG_UPDATER, arg_bool)
elif cmd == "debug_command":
arg_bool = get_bool_arg(arg)
params = pioneer.get_user_params()
params[PARAM_DEBUG_COMMAND] = arg_bool
print(f"Setting debug_command to: {arg_bool}")
pioneer.set_user_params(params)
pioneer.set_user_param(PARAM_DEBUG_COMMAND, arg_bool)
elif cmd == "set_scan_interval":
try:
scan_interval = float(arg)
Expand Down
63 changes: 34 additions & 29 deletions aiopioneer/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ def __init__(self, params: dict[str, str] = None):
self.set_user_params(params)

## Parameter management functions
def update_params(self) -> None:
def _update_params(self) -> None:
"""Set current parameters."""
self._params = {}
merge(self._params, self._default_params)
Expand All @@ -707,14 +707,7 @@ def update_params(self) -> None:
del self._system_params[PARAM_TUNER_AM_FREQ_STEP]
merge(self._params, self._system_params)

def set_user_params(self, params: dict[str, Any] = None) -> None:
"""Set user parameters and update current parameters."""
_LOGGER.debug(">> PioneerAVR.set_user_params(%s)", params)
self._user_params = copy.deepcopy(params) if params is not None else {}
self.update_params()
self._update_listening_modes()

def _set_default_params_model(self) -> None:
def set_default_params_model(self) -> None:
"""Set default parameters based on device model."""
model = self.model
self._default_params = PARAM_DEFAULTS
Expand All @@ -727,35 +720,47 @@ def _set_default_params_model(self) -> None:
model_regex,
)
merge(self._default_params, model_params, force_overwrite=True)
self.update_params()
self._update_params()

def get_param(self, param_name: str) -> Any:
"""Get the value of the specified parameter."""
return self._params.get(param_name)
def set_user_params(self, params: dict[str, Any] = None) -> None:
"""Set user parameters and update current parameters."""
_LOGGER.debug(">> PioneerAVR.set_user_params(%s)", params)
self._user_params = copy.deepcopy(params) if params is not None else {}
self._update_params()
self.update_listening_modes()

def get_params(self) -> dict[str, Any]:
"""Get a copy of all current parameters."""
## NOTE: can't use MappingProxyTypeType because of mutable dict values
return copy.deepcopy(self._params)
def set_user_param(self, param: str, value: Any) -> None:
"""Set a user parameter."""
self._user_params[param] = value
self._update_params()
self.update_listening_modes()

def get_user_params(self) -> dict[str, Any]:
"""Get a copy of user parameters."""
return copy.deepcopy(self._user_params)
def set_system_param(self, param: str, value: bool) -> None:
"""Set system parameter."""
self._system_params[param] = value
self._update_params()

def get_default_params(self) -> dict[str, Any]:
@property
def default_params(self) -> dict[str, Any]:
"""Get a copy of current default parameters."""
return copy.deepcopy(self._default_params)

@property
def query_sources(self) -> bool:
"""Whether sources have been queried from AVR."""
return self._system_params.get(PARAM_QUERY_SOURCES)
def user_params(self) -> dict[str, Any]:
"""Get a copy of user parameters."""
return copy.deepcopy(self._user_params)

@property
def params_all(self) -> dict[str, Any]:
"""Get a copy of all current parameters."""
## NOTE: can't use MappingProxyTypeType because of mutable dict values
return copy.deepcopy(self._params)

def _set_query_sources(self, value: bool) -> None:
self._system_params[PARAM_QUERY_SOURCES] = value
self.update_params()
def get_param(self, param_name: str) -> Any:
"""Get the value of the specified parameter."""
return self._params.get(param_name)

def _update_listening_modes(self) -> None:
def update_listening_modes(self) -> None:
"""Update list of valid listening modes for AVR."""
all_listening_modes = LISTENING_MODES | self._params.get(
PARAM_EXTRA_LISTENING_MODES, {}
Expand All @@ -781,4 +786,4 @@ def _update_listening_modes(self) -> None:
_LOGGER.info("determining available listening modes")
self._system_params[PARAM_ALL_LISTENING_MODES] = all_listening_modes
self._system_params[PARAM_AVAILABLE_LISTENING_MODES] = available_listening_modes
self.update_params()
self._update_params()
10 changes: 5 additions & 5 deletions aiopioneer/pioneer_avr.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
PARAM_ENABLED_FUNCTIONS,
PARAM_DISABLE_AUTO_QUERY,
PARAM_TUNER_AM_FREQ_STEP,
PARAM_QUERY_SOURCES,
)
from .commands import PIONEER_COMMANDS
from .connection import PioneerAVRConnection
Expand Down Expand Up @@ -179,7 +180,7 @@ async def update_zones(self) -> None:
async def build_source_dict(self) -> None:
"""Generate source id<->name translation tables."""
timeouts = 0
self._set_query_sources(True)
self.set_system_param(PARAM_QUERY_SOURCES, True)
self._source_name_to_id = {}
self._source_id_to_name = {}
await self._command_queue_wait() ## wait for command queue to complete
Expand Down Expand Up @@ -220,8 +221,8 @@ async def query_device_model(self) -> None:
"""Query device model from Pioneer AVR."""
_LOGGER.info("querying device model")
if await self.send_command("query_model", ignore_error=True):
self._set_default_params_model() # Update default params for this model
self._update_listening_modes() # Update valid listening modes
self.set_default_params_model() # Update default params for this model
self.update_listening_modes() # Update valid listening modes

async def query_device_info(self) -> None:
"""Query device information from Pioneer AVR."""
Expand Down Expand Up @@ -996,8 +997,7 @@ async def _calculate_am_frequency_step(self) -> None:
)
return

self._system_params[PARAM_TUNER_AM_FREQ_STEP] = new_freq - current_freq
self.update_params()
self.set_system_param(PARAM_TUNER_AM_FREQ_STEP, new_freq - current_freq)
await self.send_command("decrease_tuner_frequency", ignore_error=True)

async def _step_tuner_frequency(self, band: str, frequency: float) -> None:
Expand Down
4 changes: 2 additions & 2 deletions aiopioneer/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from .commands import PIONEER_COMMANDS
from .const import Zones, MEDIA_CONTROL_COMMANDS
from .param import PioneerAVRParams, PARAM_ZONE_SOURCES
from .param import PioneerAVRParams, PARAM_ZONE_SOURCES, PARAM_QUERY_SOURCES


class PioneerAVRProperties(PioneerAVRParams):
Expand Down Expand Up @@ -42,7 +42,7 @@ def __init__(self, params: dict[str, str] = None):

def set_source_dict(self, sources: dict[str, str]) -> None:
"""Manually set source id<->name translation tables."""
self._set_query_sources(False)
self.set_system_param(PARAM_QUERY_SOURCES, False)
self._source_name_to_id = copy.deepcopy(sources)
self._source_id_to_name = {v: k for k, v in sources.items()}

Expand Down

0 comments on commit 2c7bc3f

Please sign in to comment.