Explorar el Código

don't harcode friendly names anymore

Noah Vogt hace 1 mes
padre
commit
d84a0031e7
Se han modificado 3 ficheros con 29 adiciones y 10 borrados
  1. 5 0
      README.md
  2. 1 0
      custom_components/pjlink2/const.py
  3. 23 10
      custom_components/pjlink2/media_player.py

+ 5 - 0
README.md

@@ -44,4 +44,9 @@ media_player:
     encoding: "utf-16"        # encoding for communication (optional, default is utf-8)
     password: "secret%123"    # password to establish connection (optional)
     timeout: 1.5              # timeout to establish connection in seconds (optional, default is 4 sec)
+    sources:
+      "31": "Smart TV"
+      "32": "Camera HDMI Out"
+      "11": "Laptop"
 ```
+If you omit the sources block, the integration will show raw codes like 31, 32, etc., and add new ones to the dropdown as you switch to them on the device.

+ 1 - 0
custom_components/pjlink2/const.py

@@ -3,6 +3,7 @@ from enum import StrEnum
 
 DOMAIN = "pjlink2"
 
+CONF_SOURCES = "sources"
 CONF_ENCODING = "encoding"
 DEFAULT_ENCODING = "utf-8"
 DEFAULT_PORT = 4352

+ 23 - 10
custom_components/pjlink2/media_player.py

@@ -40,6 +40,7 @@ import voluptuous as vol
 
 from .const import (
     DOMAIN,
+    CONF_SOURCES,
     CONF_ENCODING,
     DEFAULT_ENCODING,
     DEFAULT_PORT,
@@ -61,6 +62,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
         vol.Required(CONF_HOST): cv.string,
         vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
         vol.Optional(CONF_NAME): cv.string,
+        vol.Optional(CONF_SOURCES): vol.Schema({cv.string: cv.string}),
         vol.Optional(CONF_ENCODING, default=DEFAULT_ENCODING): cv.string,
         vol.Optional(CONF_PASSWORD): cv.string,
         vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_float,
@@ -79,8 +81,9 @@ async def async_setup_platform(
     password = config.get(CONF_PASSWORD)
     timeout = config.get(CONF_TIMEOUT)
     name = config.get(CONF_NAME)
+    sources = config.get(CONF_SOURCES)
     pjl = PJLink(host, port, password, timeout)
-    devices = [PJLink2MediaPlayer(pjl, name)]
+    devices = [PJLink2MediaPlayer(pjl, name, sources)]
     async_add_entities(devices, update_before_add=False)
 
 
@@ -92,7 +95,7 @@ class PJLink2MediaPlayer(MediaPlayerEntity):
         | MediaPlayerEntityFeature.SELECT_SOURCE
     )
 
-    def __init__(self, pjl, name):
+    def __init__(self, pjl, name, sources):
         super().__init__()
         self._projector = pjl
         self.attrs: dict[str, Any] = {}
@@ -106,14 +109,18 @@ class PJLink2MediaPlayer(MediaPlayerEntity):
         self._connectionErrorLogged = False
         self._current_source = None
 
-        self._source_mapping = {
-            "31": "HDMI 1",
-            "32": "HDMI 2",
-            "33": "HDMI 3",
-            "11": "Computer 1",
-        }
-        self._reverse_mapping = {v: k for k, v in self._source_mapping.items()}
-        self._source_list = list(self._source_mapping.values())
+        if sources:
+            self._source_mapping = sources
+            self._source_list = list(self._source_mapping.values())
+            self._reverse_mapping = {
+                v: k for k, v in self._source_mapping.items()
+            }
+            self._dynamic_sources = False
+        else:
+            self._source_mapping = {}
+            self._source_list = []
+            self._reverse_mapping = {}
+            self._dynamic_sources = True
 
     async def async_will_remove_from_hass(self) -> None:
         await super().async_will_remove_from_hass()
@@ -204,6 +211,12 @@ class PJLink2MediaPlayer(MediaPlayerEntity):
                     else:
                         raw_source = str(current)
 
+                    if (
+                        self._dynamic_sources
+                        and raw_source not in self._source_list
+                    ):
+                        self._source_list.append(raw_source)
+
                     self._current_source = self._source_mapping.get(
                         raw_source, raw_source
                     )