Skip to content

Commit

Permalink
Replace numeric input names with user-defined names when available
Browse files Browse the repository at this point in the history
  • Loading branch information
Jalle19 committed Sep 13, 2024
1 parent 6f51127 commit 0615161
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
22 changes: 12 additions & 10 deletions custom_components/extron/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
logger = logging.getLogger(__name__)


def make_numeric_source_bidict(num_sources: int) -> bidict:
def make_source_bidict(num_sources: int, input_names: list[str]) -> bidict:
bd = bidict()

for i in range(num_sources):
bd.put(i, str(i))
for i in range(1, num_sources + 1):
# Use user-defined input name for the source when available
bd.put(i, input_names[i - 1] if i - 1 < len(input_names) else str(i))

return bd

Expand Down Expand Up @@ -122,9 +123,8 @@ def source(self):
def source_list(self):
return list(self._source_bidict.values())

@staticmethod
def make_source_bidict() -> bidict:
return make_numeric_source_bidict(5)
def make_source_bidict(self) -> bidict:
return make_source_bidict(5, self._input_names)

async def async_select_source(self, source):
await self._ssp.select_input(self._source_bidict.inverse.get(source))
Expand Down Expand Up @@ -175,13 +175,15 @@ def make_source_bidict(self) -> bidict:
sw = model_name.split(" ")[0]

if sw == "SW2":
return make_numeric_source_bidict(2)
num_sources = 2
elif sw == "SW4":
return make_numeric_source_bidict(4)
num_sources = 4
elif sw == "SW6":
return make_numeric_source_bidict(6)
num_sources = 6
else:
return make_numeric_source_bidict(8)
num_sources = 8

return make_source_bidict(num_sources, self._input_names)

async def async_select_source(self, source: str):
await self._hdmi_switcher.select_input(int(source))
Expand Down
26 changes: 26 additions & 0 deletions tests/test_media_player.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from unittest import TestCase

from custom_components.extron.media_player import make_source_bidict


class TestSourceBidict(TestCase):
def test_make_source_bidict(self):
# No input names specified
bd = make_source_bidict(4, [])
self.assertEqual(4, len(bd.values()))
self.assertEqual("1", bd.get(1))
self.assertEqual("4", bd.get(4))

# First two input names specified
bd = make_source_bidict(4, ["foo", "bar"])
self.assertEqual(4, len(bd.values()))
self.assertEqual("foo", bd.get(1))
self.assertEqual("bar", bd.get(2))
self.assertEqual("3", bd.get(3))
self.assertEqual("4", bd.get(4))

# Define one more input name than there are sources
bd = make_source_bidict(2, ["foo", "bar", "baz"])
self.assertEqual(2, len(bd.values()))
self.assertEqual("foo", bd.get(1))
self.assertEqual("bar", bd.get(2))

0 comments on commit 0615161

Please sign in to comment.