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
1 change: 1 addition & 0 deletions monitorcontrol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 13 additions & 12 deletions monitorcontrol/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from . import get_monitors, PowerMode
from . import get_monitors, get_input_name, PowerMode
from typing import List, Optional
import argparse
import importlib.metadata
Expand All @@ -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",
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -158,14 +158,15 @@ 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}")
input_name = get_input_name(i)
if i == current_input:
sys.stdout.write("*\n")
current = "*"
else:
sys.stdout.write("\n")
current = " "
print(f" {current} {input_name}")

return
else:
Expand Down
19 changes: 18 additions & 1 deletion monitorcontrol/monitorcontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
5 changes: 3 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
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
import pytest

Expand All @@ -14,7 +15,7 @@


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()

Expand Down
10 changes: 10 additions & 0 deletions tests/test_monitorcontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from monitorcontrol.monitorcontrol import (
InputSource,
get_monitors,
get_input_name,
get_vcps,
Monitor,
_convert_to_dict,
Expand Down Expand Up @@ -61,6 +62,15 @@ 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()

Expand Down