From 0ef4d63c76ae198a24ec1974cf37c3ad3af2faa6 Mon Sep 17 00:00:00 2001 From: Alex Martens Date: Mon, 26 May 2025 10:45:44 -0700 Subject: [PATCH] Change get_input_source to return an int --- CHANGELOG.md | 3 +++ monitorcontrol/monitorcontrol.py | 43 ++++---------------------------- tests/test_monitorcontrol.py | 12 --------- 3 files changed, 8 insertions(+), 50 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ef004a..f04394f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ as of version 2.1.1. ### Changed - Changed the build system from poetry-core to setuptools. - Changed the VCPCode type to use class attributes instead of property functions. +- Changed `get_input_source` to return an `int` instead of an `InputSource`. + - This simplifies code for users with USB Type-C monitors. - Changed enums from `enum.Enum` to `enum.IntEnum`: - `ColorPreset` - `PowerMode` @@ -20,6 +22,7 @@ as of version 2.1.1. ### Removed - Removed support for end-of-life python version 3.8. +- Removed `InputSourceValueError`. ## [3.1.0] - 2023-10-10 ### Added diff --git a/monitorcontrol/monitorcontrol.py b/monitorcontrol/monitorcontrol.py index d5157e9..6af9f08 100644 --- a/monitorcontrol/monitorcontrol.py +++ b/monitorcontrol/monitorcontrol.py @@ -63,21 +63,6 @@ class InputSource(enum.IntEnum): HDMI2 = 0x12 -class InputSourceValueError(ValueError): - """ - Raised upon an invalid (out of spec) input source value. - - https://github.com/newAM/monitorcontrol/issues/93 - - Attributes: - value (int): The value of the input source that was invalid. - """ - - def __init__(self, message: str, value: int): - super().__init__(message) - self.value = value - - class Monitor: """ A physical monitor attached to a Virtual Control Panel (VCP). @@ -418,7 +403,7 @@ def set_power_mode(self, value: Union[int, str, PowerMode]): self._set_vcp_feature(vcp_codes.display_power_mode, mode_value) - def get_input_source(self) -> InputSource: + def get_input_source(self) -> int: """ Gets the monitors input source @@ -428,35 +413,17 @@ def get_input_source(self) -> InputSource: Example: Basic Usage:: - from monitorcontrol import get_monitors + from monitorcontrol import get_monitors, InputSource for monitor in get_monitors(): with monitor: - print(monitor.get_input_source()) - - Handling out-of-spec inputs (observed for USB type-C inputs):: - - from monitorcontrol import get_monitors, InputSourceValueError - - for monitor in get_monitors(): - with monitor: - try: - print(monitor.get_input_source()) - except InputSourceValueError as e: - print(e.value) + input_source_raw: int = monitor.get_input_source() + print(InputSource(input_source_raw).name) Raises: VCPError: Failed to get input source from the VCP. - InputSourceValueError: - Input source value is not within the MCCS defined inputs. """ - value = self._get_vcp_feature(vcp_codes.input_select) & 0xFF - try: - return InputSource(value) - except ValueError: - raise InputSourceValueError( - f"{value} is not a valid InputSource", value - ) from None + return self._get_vcp_feature(vcp_codes.input_select) & 0xFF def set_input_source(self, value: Union[int, str, InputSource]): """ diff --git a/tests/test_monitorcontrol.py b/tests/test_monitorcontrol.py index 5b6ce95..62fa18f 100644 --- a/tests/test_monitorcontrol.py +++ b/tests/test_monitorcontrol.py @@ -1,7 +1,6 @@ from monitorcontrol import vcp, vcp_codes from monitorcontrol.monitorcontrol import ( InputSource, - InputSourceValueError, get_monitors, get_vcps, Monitor, @@ -204,17 +203,6 @@ def test_input_source( assert read_source == mode -@pytest.mark.skipif(USE_ATTACHED_MONITORS, reason="This is mocked") -def test_get_input_source_type_c(monitor: Monitor): - type_c_input = 27 - with mock.patch.object(monitor, "_get_vcp_feature", return_value=type_c_input): - try: - monitor.get_input_source() - assert 0, "Did not raise InputSourceValueError" - except InputSourceValueError as e: - assert e.value == type_c_input - - @pytest.mark.skipif( USE_ATTACHED_MONITORS, reason="No value in testing this with real monitors" )