Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix source mapping in Onkyo #129716

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 39 additions & 25 deletions homeassistant/components/onkyo/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,27 @@

type InputLibValue = str | tuple[str, ...]

_cmds: dict[str, InputLibValue] = {
k: v["name"]
for k, v in {
**PYEISCP_COMMANDS["main"]["SLI"]["values"],
**PYEISCP_COMMANDS["zone2"]["SLZ"]["values"],
}.items()
}

def _input_lib_cmds(zone: str) -> dict[InputSource, InputLibValue]:
match zone:
case "main":
cmds = PYEISCP_COMMANDS["main"]["SLI"]
case "zone2":
cmds = PYEISCP_COMMANDS["zone2"]["SLZ"]
case "zone3":
cmds = PYEISCP_COMMANDS["zone3"]["SL3"]
case "zone4":
cmds = PYEISCP_COMMANDS["zone4"]["SL4"]

Check warning on line 141 in homeassistant/components/onkyo/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/onkyo/media_player.py#L133-L141

Added lines #L133 - L141 were not covered by tests

result: dict[InputSource, InputLibValue] = {}
for k, v in cmds["values"].items():
arturpragacz marked this conversation as resolved.
Show resolved Hide resolved
try:
source = InputSource(k)
except ValueError:
continue
result[source] = v["name"]

Check warning on line 149 in homeassistant/components/onkyo/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/onkyo/media_player.py#L143-L149

Added lines #L143 - L149 were not covered by tests

return result

Check warning on line 151 in homeassistant/components/onkyo/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/onkyo/media_player.py#L151

Added line #L151 was not covered by tests


async def async_setup_platform(
Expand All @@ -147,16 +161,13 @@
host = config.get(CONF_HOST)

source_mapping: dict[str, InputSource] = {}
for value, source_lib in _cmds.items():
try:
source = InputSource(value)
except ValueError:
continue
if isinstance(source_lib, str):
source_mapping.setdefault(source_lib, source)
else:
for source_lib_single in source_lib:
source_mapping.setdefault(source_lib_single, source)
for zone in ZONES:
for source, source_lib in _input_lib_cmds(zone).items():
if isinstance(source_lib, str):
source_mapping.setdefault(source_lib, source)

Check warning on line 167 in homeassistant/components/onkyo/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/onkyo/media_player.py#L164-L167

Added lines #L164 - L167 were not covered by tests
else:
for source_lib_single in source_lib:
source_mapping.setdefault(source_lib_single, source)

Check warning on line 170 in homeassistant/components/onkyo/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/onkyo/media_player.py#L169-L170

Added lines #L169 - L170 were not covered by tests

sources: dict[InputSource, str] = {}
for source_lib_single, source_name in config[CONF_SOURCES].items():
Expand Down Expand Up @@ -340,9 +351,12 @@
self._volume_resolution = volume_resolution
self._max_volume = max_volume

self._source_mapping = sources
self._reverse_mapping = {value: key for key, value in sources.items()}
self._lib_mapping = {_cmds[source.value]: source for source in InputSource}
self._name_mapping = sources
self._reverse_name_mapping = {value: key for key, value in sources.items()}
self._lib_mapping = _input_lib_cmds(zone)
self._reverse_lib_mapping = {

Check warning on line 357 in homeassistant/components/onkyo/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/onkyo/media_player.py#L354-L357

Added lines #L354 - L357 were not covered by tests
value: key for key, value in self._lib_mapping.items()
}

self._attr_source_list = list(sources.values())
self._attr_extra_state_attributes = {}
Expand Down Expand Up @@ -414,7 +428,7 @@
async def async_select_source(self, source: str) -> None:
"""Select input source."""
if self.source_list and source in self.source_list:
source_lib = _cmds[self._reverse_mapping[source].value]
source_lib = self._lib_mapping[self._reverse_name_mapping[source]]

Check warning on line 431 in homeassistant/components/onkyo/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/onkyo/media_player.py#L431

Added line #L431 was not covered by tests
if isinstance(source_lib, str):
source_lib_single = source_lib
else:
Expand All @@ -432,7 +446,7 @@
) -> None:
"""Play radio station by preset number."""
if self.source is not None:
source = self._reverse_mapping[self.source]
source = self._reverse_name_mapping[self.source]

Check warning on line 449 in homeassistant/components/onkyo/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/onkyo/media_player.py#L449

Added line #L449 was not covered by tests
if media_type.lower() == "radio" and source in DEFAULT_PLAYABLE_SOURCES:
self._update_receiver("preset", media_id)

Expand Down Expand Up @@ -505,9 +519,9 @@

@callback
def _parse_source(self, source_lib: InputLibValue) -> None:
source = self._lib_mapping[source_lib]
if source in self._source_mapping:
self._attr_source = self._source_mapping[source]
source = self._reverse_lib_mapping[source_lib]
if source in self._name_mapping:
self._attr_source = self._name_mapping[source]

Check warning on line 524 in homeassistant/components/onkyo/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/onkyo/media_player.py#L522-L524

Added lines #L522 - L524 were not covered by tests
return

source_meaning = source.value_meaning
Expand Down