From 61a4c974ab2c184a17e22585d76f67f05f929f2b Mon Sep 17 00:00:00 2001 From: Daniel Fox Date: Thu, 16 Oct 2025 13:59:52 -0700 Subject: [PATCH 1/8] Fix a typo --- monitorcontrol/__main__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monitorcontrol/__main__.py b/monitorcontrol/__main__.py index aefc33f..4ac9a35 100644 --- a/monitorcontrol/__main__.py +++ b/monitorcontrol/__main__.py @@ -23,12 +23,12 @@ def get_parser() -> argparse.ArgumentParser: group = parser.add_mutually_exclusive_group(required=True) group.add_argument( - "--set-luminance", type=int, help="Set the lumianance of all monitors." + "--set-luminance", type=int, help="Set the luminance of all monitors." ) group.add_argument( "--get-luminance", action="store_true", - help="Get the lumianance of the first monitor.", + help="Get the luminance of the first monitor.", ) group.add_argument( "--get-power-mode", From 25008163356eb3e37f6294ad920f1f6a9e8d3081 Mon Sep 17 00:00:00 2001 From: Daniel Fox Date: Thu, 16 Oct 2025 14:01:05 -0700 Subject: [PATCH 2/8] Replace sys.stdout.write with print --- monitorcontrol/__main__.py | 18 +++++++++--------- tests/test_cli.py | 5 +++-- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/monitorcontrol/__main__.py b/monitorcontrol/__main__.py index 4ac9a35..5c2fc89 100644 --- a/monitorcontrol/__main__.py +++ b/monitorcontrol/__main__.py @@ -101,19 +101,19 @@ def main(argv: Optional[List[str]] = None): monitor_index = args.monitor - 1 if args.version: - sys.stdout.write(version + "\n") + print(version) return elif args.get_luminance: monitor_obj = get_monitors()[monitor_index] with monitor_obj: luminance = monitor_obj.get_luminance() - sys.stdout.write(str(luminance) + "\n") + print(str(luminance)) return elif args.get_power_mode: monitor_obj = get_monitors()[monitor_index] with monitor_obj: power = monitor_obj.get_power_mode() - sys.stdout.write(str(power.name) + "\n") + print(str(power.name)) return elif args.set_luminance is not None: if args.monitor is None: @@ -139,7 +139,7 @@ def main(argv: Optional[List[str]] = None): monitor_obj = get_monitors()[monitor_index] with monitor_obj: input_source = monitor_obj.get_input_source() - sys.stdout.write(str(input_source) + "\n") + print(str(input_source)) return elif args.set_input_source is not None: if args.monitor is None: @@ -158,14 +158,14 @@ def main(argv: Optional[List[str]] = None): current_input = monitor_obj.get_input_source() model = monitors_dict["model"] inputs = monitors_dict["inputs"] - sys.stdout.write(f"Monitor {monitor_index + 1}: {model}" + "\n") - sys.stdout.write("Available Inputs:\n") + print(f"Monitor {monitor_index + 1}: {model}") + print("Available Inputs:") for i in inputs: - sys.stdout.write(f"\t{i}") + print(f"\t{i}", end="") if i == current_input: - sys.stdout.write("*\n") + print("*") else: - sys.stdout.write("\n") + print() return else: diff --git a/tests/test_cli.py b/tests/test_cli.py index e804690..5b50d62 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,6 +1,8 @@ from .test_monitorcontrol import UnitTestVCP from monitorcontrol import Monitor +import monitorcontrol.__main__ from monitorcontrol.__main__ import main, count_to_level + from unittest import mock import sys import monitorcontrol @@ -12,9 +14,8 @@ return_value=[Monitor(UnitTestVCP({}))], ) - def test_version(): - with mock.patch.object(sys.stdout, "write") as stdout_mock: + with mock.patch('builtins.print') as stdout_mock: main(["--version"]) stdout_mock.assert_called_once() From f6397cbf7b2b816d4828071e07ff2b0b92e0b164 Mon Sep 17 00:00:00 2001 From: Daniel Fox Date: Thu, 16 Oct 2025 15:30:58 -0700 Subject: [PATCH 3/8] Print the input name if we have it --- monitorcontrol/__main__.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/monitorcontrol/__main__.py b/monitorcontrol/__main__.py index 5c2fc89..89ba70f 100644 --- a/monitorcontrol/__main__.py +++ b/monitorcontrol/__main__.py @@ -1,4 +1,4 @@ -from . import get_monitors, PowerMode +from . import get_monitors, PowerMode, InputSource from typing import List, Optional import argparse import importlib.metadata @@ -161,11 +161,15 @@ def main(argv: Optional[List[str]] = None): print(f"Monitor {monitor_index + 1}: {model}") print("Available Inputs:") for i in inputs: - print(f"\t{i}", end="") + try: + input_name = InputSource(i).name + except ValueError: + input_name = f"UNKNOWN (code {i:#04x})" if i == current_input: - print("*") + current = "*" else: - print() + current = " " + print(f" {current} {input_name}") return else: From b3eb989c3889d42bd0464951707d27499feb1aba Mon Sep 17 00:00:00 2001 From: Daniel Fox Date: Thu, 16 Oct 2025 15:44:28 -0700 Subject: [PATCH 4/8] Make it testable --- monitorcontrol/__init__.py | 1 + monitorcontrol/__main__.py | 3 ++- monitorcontrol/monitorcontrol.py | 19 ++++++++++++++++++- tests/test_monitorcontrol.py | 7 +++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/monitorcontrol/__init__.py b/monitorcontrol/__init__.py index 8876d01..729e7f9 100644 --- a/monitorcontrol/__init__.py +++ b/monitorcontrol/__init__.py @@ -2,6 +2,7 @@ from .vcp import vcp_codes, VCPError, VCPIOError, VCPPermissionError # noqa: F401 from .monitorcontrol import ( # noqa: F401 get_monitors, + get_input_name, Monitor, PowerMode, InputSource, diff --git a/monitorcontrol/__main__.py b/monitorcontrol/__main__.py index 89ba70f..2d104ab 100644 --- a/monitorcontrol/__main__.py +++ b/monitorcontrol/__main__.py @@ -1,4 +1,4 @@ -from . import get_monitors, PowerMode, InputSource +from . import get_monitors, get_input_name, PowerMode, InputSource from typing import List, Optional import argparse import importlib.metadata @@ -161,6 +161,7 @@ def main(argv: Optional[List[str]] = None): print(f"Monitor {monitor_index + 1}: {model}") print("Available Inputs:") for i in inputs: + input_name = get_input_name(i) try: input_name = InputSource(i).name except ValueError: diff --git a/monitorcontrol/monitorcontrol.py b/monitorcontrol/monitorcontrol.py index ebbb5cb..a242308 100644 --- a/monitorcontrol/monitorcontrol.py +++ b/monitorcontrol/monitorcontrol.py @@ -458,7 +458,24 @@ def set_input_source(self, value: Union[int, str, InputSource]): self._set_vcp_feature(vcp_codes.input_select, mode_value) -def get_vcps() -> List[Type[vcp.VCP]]: +def get_input_name(input_code: int) -> str: + """ + Returns the input name for a given input code. + + Args: + input_code: an integer representing a known (standard) input identifier + + Returns: + A string containing the input name, or "UNKNOWN" plus the unknown code as hex + """ + try: + input_name = InputSource(input_code).name + except ValueError: + input_name = f"UNKNOWN (code {input_code:#04x})" + return input_name + + +def get_vcps() -> List[vcp.VCP]: """ Discovers virtual control panels. diff --git a/tests/test_monitorcontrol.py b/tests/test_monitorcontrol.py index 7255ea2..e727d37 100644 --- a/tests/test_monitorcontrol.py +++ b/tests/test_monitorcontrol.py @@ -2,6 +2,7 @@ from monitorcontrol.monitorcontrol import ( InputSource, get_monitors, + get_input_name, get_vcps, Monitor, _convert_to_dict, @@ -60,6 +61,12 @@ def test_context_manager_assert(): def test_get_vcps(): get_vcps() +def test_get_input_name(): + for param in list(InputSource): + assert get_input_name(param) == param.name + +def test_unknown_input_name(): + assert get_input_name(0x420) == "UNKNOWN (code 0x420)" def test_get_monitors(): get_monitors() From 7ccc2919c0edd094d376c4cf012d8a93d0734be3 Mon Sep 17 00:00:00 2001 From: Daniel Fox Date: Thu, 16 Oct 2025 15:45:56 -0700 Subject: [PATCH 5/8] Remove unneeded import --- tests/test_cli.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 5b50d62..478560c 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4,7 +4,6 @@ from monitorcontrol.__main__ import main, count_to_level from unittest import mock -import sys import monitorcontrol import pytest From 95cab33446d3902273e8aa18ed8912b3c33bd9f0 Mon Sep 17 00:00:00 2001 From: Daniel Fox Date: Thu, 16 Oct 2025 15:46:37 -0700 Subject: [PATCH 6/8] Reformat --- monitorcontrol/monitorcontrol.py | 4 ++-- tests/test_cli.py | 3 ++- tests/test_monitorcontrol.py | 3 +++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/monitorcontrol/monitorcontrol.py b/monitorcontrol/monitorcontrol.py index a242308..d2259db 100644 --- a/monitorcontrol/monitorcontrol.py +++ b/monitorcontrol/monitorcontrol.py @@ -461,10 +461,10 @@ def set_input_source(self, value: Union[int, str, InputSource]): def get_input_name(input_code: int) -> str: """ Returns the input name for a given input code. - + Args: input_code: an integer representing a known (standard) input identifier - + Returns: A string containing the input name, or "UNKNOWN" plus the unknown code as hex """ diff --git a/tests/test_cli.py b/tests/test_cli.py index 478560c..d0dfd22 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -13,8 +13,9 @@ return_value=[Monitor(UnitTestVCP({}))], ) + def test_version(): - with mock.patch('builtins.print') as stdout_mock: + with mock.patch("builtins.print") as stdout_mock: main(["--version"]) stdout_mock.assert_called_once() diff --git a/tests/test_monitorcontrol.py b/tests/test_monitorcontrol.py index e727d37..764d7fa 100644 --- a/tests/test_monitorcontrol.py +++ b/tests/test_monitorcontrol.py @@ -61,13 +61,16 @@ def test_context_manager_assert(): def test_get_vcps(): get_vcps() + def test_get_input_name(): for param in list(InputSource): assert get_input_name(param) == param.name + def test_unknown_input_name(): assert get_input_name(0x420) == "UNKNOWN (code 0x420)" + def test_get_monitors(): get_monitors() From ace2e4d7c7097e60459829c4529325f58e5b6945 Mon Sep 17 00:00:00 2001 From: Daniel Fox Date: Thu, 16 Oct 2025 15:48:40 -0700 Subject: [PATCH 7/8] Remove redundant code --- monitorcontrol/__main__.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/monitorcontrol/__main__.py b/monitorcontrol/__main__.py index 2d104ab..f4bcc0a 100644 --- a/monitorcontrol/__main__.py +++ b/monitorcontrol/__main__.py @@ -162,10 +162,6 @@ def main(argv: Optional[List[str]] = None): print("Available Inputs:") for i in inputs: input_name = get_input_name(i) - try: - input_name = InputSource(i).name - except ValueError: - input_name = f"UNKNOWN (code {i:#04x})" if i == current_input: current = "*" else: From dcd75a6a09eeec428f230f3aed7c0c834983fc01 Mon Sep 17 00:00:00 2001 From: Daniel Fox Date: Thu, 16 Oct 2025 15:50:31 -0700 Subject: [PATCH 8/8] Remove another unused import --- monitorcontrol/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monitorcontrol/__main__.py b/monitorcontrol/__main__.py index f4bcc0a..8040995 100644 --- a/monitorcontrol/__main__.py +++ b/monitorcontrol/__main__.py @@ -1,4 +1,4 @@ -from . import get_monitors, get_input_name, PowerMode, InputSource +from . import get_monitors, get_input_name, PowerMode from typing import List, Optional import argparse import importlib.metadata