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

Replace duplicate renamed sources with their original names #319

Merged
merged 1 commit into from
Nov 28, 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
38 changes: 38 additions & 0 deletions denonavr/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ class DenonAVRInput(DenonAVRFoundation):
converter=attr.converters.optional(str), default=None
)

_renamed_sources_warnings: Set[Tuple[str, str]] = attr.ib(
validator=attr.validators.deep_iterable(
attr.validators.instance_of(tuple), attr.validators.instance_of(set)
),
default=attr.Factory(set),
)

# Update tags for attributes
# AppCommand.xml interface
appcommand_attrs = {AppCommands.GetAllZoneSource: None}
Expand Down Expand Up @@ -516,6 +523,8 @@ async def async_get_changed_sources_appcommand(
except AttributeError:
continue

self._replace_duplicate_sources(renamed_sources)

return (renamed_sources, deleted_sources)

async def async_get_changed_sources_status_xml(
Expand Down Expand Up @@ -604,6 +613,8 @@ async def async_get_changed_sources_status_xml(
except IndexError:
_LOGGER.error("List of deleted sources incomplete, continuing anyway")

self._replace_duplicate_sources(renamed_sources)

return (renamed_sources, deleted_sources)

async def async_update_inputfuncs(
Expand Down Expand Up @@ -897,6 +908,33 @@ def _unset_media_state(self) -> None:
self._station = None
self._image_url = None

def _replace_duplicate_sources(self, sources: Dict[str, str]) -> None:
"""Replace duplicate renamed sources (values) with their original names."""
seen_values = set()
duplicate_values = set()

for value in sources.values():
if value in seen_values:
duplicate_values.add(value)
seen_values.add(value)

for duplicate in duplicate_values:
for key, value in sources.items():
if value == duplicate:
sources[key] = key

if (key, value) not in self._renamed_sources_warnings:
_LOGGER.warning(
(
"Input source '%s' is renamed to non-unique name '%s'. "
"Using original name. Please choose unique names in "
"your receiver's web-interface"
),
key,
value,
)
self._renamed_sources_warnings.add((key, value))

##############
# Properties #
##############
Expand Down