Skip to content
Open
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
274 changes: 146 additions & 128 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pylint = "4.0.5"
pytest = "9.0.2"
pytest-asyncio = "1.3.0"
pytest-cov = "7.0.0"
pytest-mock = "3.15.1"
ruff = "0.13.3"
safety = "3.7.0"
types-cachetools = "^5.3.0"
Expand Down
5 changes: 5 additions & 0 deletions src/wled/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ class Segment(BaseModel):
~ to increment, ~- to decrement, or "r" for random.
"""

freeze: bool = field(default=False, metadata=field_options(alias="frz"))
"""
Freezes the segment.
"""

intensity: int | str = field(default=0, metadata=field_options(alias="ix"))
"""Intensity of the segment.

Expand Down
3 changes: 3 additions & 0 deletions src/wled/wled.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ async def segment( # noqa: PLR0912, PLR0913
color_secondary: tuple[int, int, int, int] | tuple[int, int, int] | None = None,
color_tertiary: tuple[int, int, int, int] | tuple[int, int, int] | None = None,
effect: int | str | None = None,
freeze: bool | None = None,
individual: Sequence[
int | Sequence[int] | tuple[int, int, int, int] | tuple[int, int, int]
]
Expand Down Expand Up @@ -340,6 +341,7 @@ async def segment( # noqa: PLR0912, PLR0913
color_secondary: The secondary color of this segment.
color_tertiary: The tertiary color of this segment.
effect: The effect number (or name) to use on this segment.
freeze: Freezes this segment.
individual: A list of colors to use for each LED in the segment.
intensity: The effect intensity to use on this segment.
length: The length of this segment.
Expand Down Expand Up @@ -375,6 +377,7 @@ async def segment( # noqa: PLR0912, PLR0913
"bri": brightness,
"cln": clones,
"fx": effect,
"frz": freeze,
"i": individual,
"ix": intensity,
"len": length,
Expand Down
209 changes: 209 additions & 0 deletions tests/fixtures/test_device_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
{
"state": {
"on": false,
"bri": 30,
"transition": 7,
"ps": -1,
"pl": -1,
"ledmap": 0,
"AudioReactive": {
"on": true
},
"nl": {
"on": false,
"dur": 60,
"mode": 1,
"tbri": 0,
"rem": -1
},
"udpn": {
"send": false,
"recv": true,
"sgrp": 1,
"rgrp": 1
},
"lor": 0,
"mainseg": 0,
"seg": [
{
"id": 0,
"start": 0,
"stop": 70,
"len": 70,
"grp": 1,
"spc": 0,
"of": 7,
"on": true,
"frz": true,
"bri": 255,
"cct": 0,
"set": 0,
"col": [
[0, 221, 255, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
],
"fx": 110,
"sx": 100,
"ix": 128,
"pal": 4,
"c1": 128,
"c2": 128,
"c3": 16,
"sel": true,
"rev": false,
"mi": true,
"o1": false,
"o2": false,
"o3": false,
"si": 0,
"m12": 1
},
{
"id": 1,
"start": 0,
"stop": 17,
"len": 17,
"grp": 1,
"spc": 0,
"of": 0,
"on": false,
"frz": false,
"bri": 255,
"cct": 127,
"set": 0,
"col": [
[8, 255, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
],
"fx": 0,
"sx": 128,
"ix": 128,
"pal": 0,
"c1": 128,
"c2": 128,
"c3": 16,
"sel": false,
"rev": false,
"mi": false,
"o1": false,
"o2": false,
"o3": false,
"si": 0,
"m12": 0
},
{
"id": 2,
"start": 17,
"stop": 31,
"len": 14,
"grp": 1,
"spc": 0,
"of": 0,
"on": false,
"bri": 255,
"cct": 127,
"set": 0,
"col": [
[0, 0, 255, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
],
"fx": 0,
"sx": 128,
"ix": 128,
"pal": 0,
"c1": 128,
"c2": 128,
"c3": 16,
"sel": false,
"rev": false,
"mi": false,
"o1": false,
"o2": false,
"o3": false,
"si": 0,
"m12": 0
},
{
"id": 3,
"start": 31,
"stop": 70,
"len": 39,
"grp": 1,
"spc": 0,
"of": 0,
"on": false,
"bri": 255,
"cct": 127,
"set": 0,
"col": [
[255, 0, 255, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
],
"fx": 0,
"sx": 128,
"ix": 128,
"pal": 0,
"c1": 128,
"c2": 128,
"c3": 16,
"sel": false,
"rev": false,
"mi": false,
"o1": false,
"o2": false,
"o3": false,
"si": 0,
"m12": 0
}
]
},
"info": {
"arch": "esp32",
"core": "v3.3.6-16",
"vid": 1234567,
"fxcount": 123,
"fs": {
"u": 12,
"t": 123,
"pmt": 1234567890
},
"freeheap": 123456,
"leds": {
"count": 70,
"pwr": 120,
"fps": 0,
"maxpwr": 5000,
"maxseg": 32,
"bootps": 0,
"seglc": [7, 7, 7, 7],
"lc": 7,
"rgbw": true,
"wv": 2,
"cct": 4
},
"lip": "",
"lm": "",
"mac": "1aa3bbb0c620",
"palcount": 12,
"udpport": 12345,
"ver": "0.15.3",
"ws": 1,
"wifi": {
"bssid": "11:22:33:44:55:66",
"rssi": -12,
"signal": 12,
"channel": 1,
"ap": false
}
},
"effects": [
"Solid"
],
"palettes": [
"Default"
]
}
53 changes: 53 additions & 0 deletions tests/test_freeze.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Tests for WLED models with and without the freeze property applied."""
import json
import aiohttp
import pytest
from pathlib import Path

from wled.models import State, Device
from wled import WLED
from aresponses import ResponsesMockServer


FIXTURE_DIR = Path(__file__).parent / "fixtures"

def load_fixture(file_name: str) -> dict:
"""Load a fixture file from the fixtures directory."""
fixture_path = FIXTURE_DIR / file_name
return json.loads(fixture_path.read_text())

def test_read_freeze_state() -> None:
"""Test frozen segment is parsed correctly."""
data = load_fixture("test_device_data.json")

model_instance = State.from_dict(data["state"])

assert model_instance.segments[0].freeze is True
# assert explicit frz: false
assert model_instance.segments[1].freeze is False
# assert default freeze=false from models.py
assert model_instance.segments[2].freeze is False
assert model_instance.segments[3].freeze is False

@pytest.mark.asyncio
async def test_write_freeze_state(mocker, aresponses: ResponsesMockServer) -> None:
"""Test WLED.segment sends correct freeze payload"""
data = load_fixture("test_device_data.json")
aresponses.add(
"example.com",
"/json/state",
"POST",
aresponses.Response(status=200, text="OK"),
)
async with aiohttp.ClientSession() as session:
wled = WLED("example.com", session=session)
request_spy = mocker.spy(wled, 'request')

wled._device = Device.from_dict(data)
response = await wled.segment(0, freeze=True)

request_spy.assert_called_once()
args, kwargs = request_spy.call_args
assert isinstance(kwargs["data"]["seg"][0]["frz"], bool)
assert kwargs["data"]["seg"][0]["frz"] is True
assert response is None