- Sequences v2, bridges and sim-controller changes from the working tree. - Records the led-driver, led-tool and led-simulator commits above. - Includes dmx-driver/ and graphify-out/. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
123 lines
4.3 KiB
Python
123 lines
4.3 KiB
Python
"""Device groups (main's controllers/group.py) and combined brightness."""
|
|
|
|
import pytest
|
|
|
|
from helpers import find_id_by_field, new_id, unique
|
|
|
|
MAC = "aabbccddee01" # made up: no driver answers it
|
|
|
|
|
|
@pytest.fixture
|
|
def device(c):
|
|
c.delete(f"/devices/{MAC}")
|
|
assert c.post("/devices", json={"name": "pytest-g", "mac": MAC, "output_brightness": 128}).status_code == 201
|
|
yield MAC
|
|
c.delete(f"/devices/{MAC}")
|
|
|
|
|
|
@pytest.fixture
|
|
def group(c, device):
|
|
resp = c.post("/groups", json={"name": unique("pytest-group"), "devices": ["AA:BB:CC:DD:EE:01"],
|
|
"output_brightness": 128})
|
|
assert resp.status_code == 201
|
|
gid = str(resp.json()["id"])
|
|
yield gid
|
|
c.delete(f"/groups/{gid}")
|
|
|
|
|
|
def test_crud(c):
|
|
name = unique("pytest-group")
|
|
resp = c.post("/groups", json={"name": name})
|
|
assert resp.status_code == 201
|
|
assert resp.json()["devices"] == []
|
|
assert resp.json()["output_brightness"] == 255
|
|
gid = find_id_by_field(c.get("/groups").json(), "name", name)
|
|
resp = c.get(f"/groups/{gid}")
|
|
assert resp.status_code == 200
|
|
assert resp.json()["name"] == name
|
|
resp = c.put(f"/groups/{gid}", json={"brightness": 10})
|
|
assert resp.status_code == 200
|
|
assert resp.json()["brightness"] == 10
|
|
assert c.delete(f"/groups/{gid}").status_code == 200
|
|
assert c.get(f"/groups/{gid}").status_code == 404
|
|
|
|
|
|
def test_device_macs_normalized(c, group):
|
|
assert c.get(f"/groups/{group}").json()["devices"] == [MAC]
|
|
|
|
|
|
def test_profile_scoped_group(c, profile):
|
|
resp = c.post("/groups", json={"name": unique("pytest-scoped"), "profile_scoped": True})
|
|
gid = str(resp.json()["id"])
|
|
assert resp.json()["profile_id"] == profile
|
|
other = new_id(c.post("/profiles", json={"name": unique("pytest-other")}))
|
|
c.post(f"/profiles/{other}/apply")
|
|
assert gid not in c.get("/groups").json()
|
|
assert c.get(f"/groups/{gid}").status_code == 404
|
|
c.post(f"/profiles/{profile}/apply")
|
|
assert gid in c.get("/groups").json()
|
|
c.delete(f"/groups/{gid}")
|
|
c.delete(f"/profiles/{other}")
|
|
|
|
|
|
def test_resolve_brightness(c, group):
|
|
# global 255 x group 128 x device 128 (x zone 255) -> round(255 * 128/255 * 128/255) = 64
|
|
resp = c.post("/devices/resolve-brightness", json={"macs": [MAC], "zone_brightness": 255})
|
|
assert resp.status_code == 200
|
|
assert resp.json()["values"] == {MAC: 64}
|
|
|
|
|
|
def test_group_max_brightness_caps(c, group):
|
|
assert c.get(f"/groups/{group}").json()["max_brightness"] == 255
|
|
assert c.put(f"/groups/{group}", json={"max_brightness": 20}).status_code == 200
|
|
resp = c.post("/devices/resolve-brightness", json={"macs": [MAC], "zone_brightness": 255})
|
|
assert resp.json()["values"] == {MAC: 20}
|
|
|
|
|
|
def test_resolve_brightness_needs_list(c):
|
|
assert c.post("/devices/resolve-brightness", json={"macs": "x"}).status_code == 400
|
|
|
|
|
|
def test_group_brightness(c, group):
|
|
resp = c.post(f"/groups/{group}/brightness")
|
|
assert resp.status_code == 200
|
|
assert resp.json()["sent"] == 1
|
|
|
|
|
|
def test_group_identify(c, group):
|
|
resp = c.post(f"/groups/{group}/identify")
|
|
assert resp.status_code == 200
|
|
assert resp.json()["sent"] == 1
|
|
|
|
|
|
def test_group_identify_needs_devices(c):
|
|
gid = str(c.post("/groups", json={"name": unique("pytest-empty")}).json()["id"])
|
|
assert c.post(f"/groups/{gid}/identify").status_code == 400
|
|
c.delete(f"/groups/{gid}")
|
|
|
|
|
|
def test_group_driver_config(c, group):
|
|
assert c.post(f"/groups/{group}/driver-config", json={}).status_code == 400
|
|
resp = c.post(f"/groups/{group}/driver-config", json={"wifi_driver_num_leds": 60})
|
|
assert resp.status_code == 200
|
|
assert resp.json()["sent"] == 1
|
|
|
|
|
|
def test_device_brightness_and_config(c, device):
|
|
resp = c.post(f"/devices/{device}/brightness", json={"zone_brightness": 255})
|
|
assert resp.status_code == 200
|
|
assert resp.json()["brightness"] == 128
|
|
assert c.post(f"/devices/{device}/driver-config", json={}).status_code == 400
|
|
assert c.post(f"/devices/{device}/driver-config", json={"num_leds": 30}).status_code == 200
|
|
|
|
|
|
def test_update_groups_push(c):
|
|
resp = c.post("/devices/groups")
|
|
espnow = [d for d in c.get("/devices").json().values() if d.get("transport") == "espnow"]
|
|
if not espnow:
|
|
assert resp.status_code == 400
|
|
return
|
|
data = resp.json()
|
|
assert data["total"] == len(espnow)
|
|
assert data["sent"] + data["failed"] == data["total"]
|