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
37 changes: 35 additions & 2 deletions monitorcontrol/monitorcontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ class InputSource(enum.Enum):
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 @@ -354,12 +369,30 @@ def get_input_source(self) -> InputSource:
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)

Raises:
VCPError: Failed to get input source from the VCP.
InputSourceValueError:
Input source value is not within the MCCS defined inputs.
"""
code = vcp.VCPCode("input_select")
value = self._get_vcp_feature(code) & 0xFF
return InputSource(value)
try:
return InputSource(value)
except ValueError:
raise InputSourceValueError(
f"{value} is not a valid InputSource", value
)

def set_input_source(self, value: Union[int, str, InputSource]):
"""
Expand All @@ -385,7 +418,7 @@ def set_input_source(self, value: Union[int, str, InputSource]):
if isinstance(value, str):
mode_value = getattr(InputSource, value.upper()).value
elif isinstance(value, int):
mode_value = InputSource(value).value
mode_value = value
elif isinstance(value, InputSource):
mode_value = value.value
else:
Expand Down
Loading