Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ 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`
- `InputSource`

### Removed
- Removed support for end-of-life python version 3.8.
- Removed `InputSourceValueError`.

## [3.1.0] - 2023-10-10
### Added
Expand Down
43 changes: 5 additions & 38 deletions monitorcontrol/monitorcontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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

Expand All @@ -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]):
"""
Expand Down
12 changes: 0 additions & 12 deletions tests/test_monitorcontrol.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from monitorcontrol import vcp, vcp_codes
from monitorcontrol.monitorcontrol import (
InputSource,
InputSourceValueError,
get_monitors,
get_vcps,
Monitor,
Expand Down Expand Up @@ -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"
)
Expand Down