Enhance Samsung monitor detection logic and add unit tests for trigger candidate selection

This commit is contained in:
Lago
2026-03-27 15:16:56 +01:00
parent 37eb9e9fa0
commit c06799f7b9
3 changed files with 88 additions and 28 deletions
+55 -1
View File
@@ -4,6 +4,7 @@ from dataclasses import dataclass, field
from typing import Protocol
from monitorcontrol import get_monitors
from app.config import SUPPORTED_TARGET_PORTS
ALIENWARE_MODEL_TOKEN = "AW3423DWF"
@@ -86,7 +87,22 @@ class RealMonitorBackend:
elif not trigger_candidates:
errors.append("Samsung trigger monitor could not be identified through DDC/CI.")
else:
if samsung_named_candidates:
scanned_candidates: list[tuple[object, str, int | None]] = []
for monitor, description in trigger_candidates:
local_errors: list[str] = []
input_code = self._read_input_code(
monitor,
"Samsung trigger candidate monitor",
local_errors,
)
scanned_candidates.append((monitor, description, input_code))
selected_index = _select_trigger_candidate_index(
[(description, input_code) for _, description, input_code in scanned_candidates]
)
if selected_index is not None:
_, trigger_description, trigger_input_code = scanned_candidates[selected_index]
elif samsung_named_candidates:
errors.append("Multiple Samsung DDC monitors were detected; trigger monitor is ambiguous.")
else:
errors.append(
@@ -172,3 +188,41 @@ def _is_samsung_description(description: str) -> bool:
if normalized.startswith("SAM"):
return True
return " SAM " in f" {normalized} "
def _is_generic_description(description: str) -> bool:
normalized = description.upper().strip()
return "GENERIC" in normalized or "UNKNOWN" in normalized
def _trigger_input_codes() -> set[int]:
codes: set[int] = set()
for port_spec in SUPPORTED_TARGET_PORTS.values():
input_codes = port_spec.get("input_codes", set())
codes.update(int(code) for code in input_codes)
return codes
def _select_trigger_candidate_index(candidates: list[tuple[str, int | None]]) -> int | None:
if not candidates:
return None
trigger_codes = _trigger_input_codes()
matching = [
index
for index, (_, input_code) in enumerate(candidates)
if input_code is not None and int(input_code) in trigger_codes
]
if len(matching) == 1:
return matching[0]
if len(matching) > 1:
generic_matches = [index for index in matching if _is_generic_description(candidates[index][0])]
if len(generic_matches) == 1:
return generic_matches[0]
generic = [index for index, (description, _) in enumerate(candidates) if _is_generic_description(description)]
if len(generic) == 1:
return generic[0]
return None