ca36b15af1
- Python CLI (click): discover, status, on/off/toggle, brightness, temperature, info - mDNS discovery via zeroconf for automatic light detection - Shared config at ~/.config/elgato-cli/config.json - KDE Plasma 6 plasmoid with system tray icon, power/brightness/temperature controls - Plasmoid uses XMLHttpRequest directly to light API (no CLI dependency) - Unit tests for API client with mocked HTTP responses
101 lines
3.3 KiB
Python
101 lines
3.3 KiB
Python
"""Tests for elgato_cli.api."""
|
|
|
|
from unittest.mock import patch, MagicMock
|
|
|
|
from elgato_cli.api import ElgatoLight, LightState, mired_to_kelvin, kelvin_to_mired
|
|
|
|
|
|
def _mock_response(json_data, status_code=200):
|
|
resp = MagicMock()
|
|
resp.json.return_value = json_data
|
|
resp.status_code = status_code
|
|
resp.raise_for_status.return_value = None
|
|
return resp
|
|
|
|
|
|
LIGHT_RESPONSE = {
|
|
"numberOfLights": 1,
|
|
"lights": [{"on": 1, "brightness": 50, "temperature": 200}],
|
|
}
|
|
|
|
|
|
class TestMiredKelvin:
|
|
def test_mired_to_kelvin(self):
|
|
assert mired_to_kelvin(143) == 6993
|
|
assert mired_to_kelvin(344) == 2907
|
|
assert mired_to_kelvin(200) == 5000
|
|
|
|
def test_kelvin_to_mired(self):
|
|
assert kelvin_to_mired(5000) == 200
|
|
assert kelvin_to_mired(7000) == 143
|
|
assert kelvin_to_mired(2900) == 345
|
|
|
|
def test_roundtrip(self):
|
|
for mired in [143, 200, 250, 300, 344]:
|
|
assert abs(kelvin_to_mired(mired_to_kelvin(mired)) - mired) <= 1
|
|
|
|
|
|
class TestElgatoLight:
|
|
@patch("elgato_cli.api.requests.get")
|
|
def test_get_state(self, mock_get):
|
|
mock_get.return_value = _mock_response(LIGHT_RESPONSE)
|
|
light = ElgatoLight("192.168.1.100")
|
|
state = light.get_state()
|
|
assert state == LightState(on=True, brightness=50, temperature=200)
|
|
mock_get.assert_called_once_with(
|
|
"http://192.168.1.100:9123/elgato/lights", timeout=5
|
|
)
|
|
|
|
@patch("elgato_cli.api.requests.put")
|
|
@patch("elgato_cli.api.requests.get")
|
|
def test_set_state_partial(self, mock_get, mock_put):
|
|
mock_get.return_value = _mock_response(LIGHT_RESPONSE)
|
|
updated = {
|
|
"numberOfLights": 1,
|
|
"lights": [{"on": 1, "brightness": 80, "temperature": 200}],
|
|
}
|
|
mock_put.return_value = _mock_response(updated)
|
|
|
|
light = ElgatoLight("192.168.1.100")
|
|
state = light.set_state(brightness=80)
|
|
|
|
assert state.brightness == 80
|
|
call_args = mock_put.call_args
|
|
payload = call_args[1]["json"]
|
|
assert payload["lights"][0]["brightness"] == 80
|
|
assert payload["lights"][0]["on"] == 1 # preserved from current state
|
|
|
|
@patch("elgato_cli.api.requests.put")
|
|
@patch("elgato_cli.api.requests.get")
|
|
def test_toggle(self, mock_get, mock_put):
|
|
mock_get.return_value = _mock_response(LIGHT_RESPONSE) # on=1
|
|
toggled = {
|
|
"numberOfLights": 1,
|
|
"lights": [{"on": 0, "brightness": 50, "temperature": 200}],
|
|
}
|
|
mock_put.return_value = _mock_response(toggled)
|
|
|
|
light = ElgatoLight("192.168.1.100")
|
|
state = light.toggle()
|
|
assert state.on is False
|
|
|
|
@patch("elgato_cli.api.requests.get")
|
|
def test_get_info(self, mock_get):
|
|
info_resp = {
|
|
"productName": "Elgato Key Light",
|
|
"serialNumber": "AB12CD34",
|
|
"firmwareVersion": "1.0.3",
|
|
}
|
|
mock_get.return_value = _mock_response(info_resp)
|
|
|
|
light = ElgatoLight("192.168.1.100")
|
|
info = light.get_info()
|
|
assert info["productName"] == "Elgato Key Light"
|
|
mock_get.assert_called_once_with(
|
|
"http://192.168.1.100:9123/elgato/accessory-info", timeout=5
|
|
)
|
|
|
|
def test_custom_port(self):
|
|
light = ElgatoLight("10.0.0.5", port=8080)
|
|
assert light.base_url == "http://10.0.0.5:8080/elgato"
|