Skip to content

Commit

Permalink
Replace duplicate renamed sources with their original names
Browse files Browse the repository at this point in the history
  • Loading branch information
ol-iver committed Nov 28, 2024
1 parent 3a346ae commit a2e8481
Showing 1 changed file with 38 additions and 0 deletions.
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

0 comments on commit a2e8481

Please sign in to comment.