Skip to content

Commit 0b4e8ad

Browse files
authored
Add handling of live override mode (#298)
1 parent 953dd6b commit 0b4e8ad

4 files changed

Lines changed: 95 additions & 1 deletion

File tree

src/wled/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Effect,
66
Info,
77
Leds,
8+
Live,
89
Nightlight,
910
Palette,
1011
Segment,
@@ -24,6 +25,7 @@
2425
"Effect",
2526
"Info",
2627
"Leds",
28+
"Live",
2729
"Nightlight",
2830
"Palette",
2931
"Segment",

src/wled/models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import annotations
33

44
from dataclasses import dataclass
5+
from enum import IntEnum
56
from typing import Any
67

78
from .exceptions import WLEDError
@@ -320,6 +321,7 @@ class State:
320321
segments: list[Segment]
321322
sync: Sync
322323
transition: int
324+
lor: Live
323325

324326
@property
325327
def playlist_active(self) -> bool:
@@ -355,6 +357,7 @@ def from_dict(
355357
"""
356358
brightness = data.get("bri", 1)
357359
on = data.get("on", False)
360+
lor = data.get("lor", 0)
358361

359362
segments = [
360363
Segment.from_dict(
@@ -377,6 +380,7 @@ def from_dict(
377380
segments=segments,
378381
sync=Sync.from_dict(data),
379382
transition=data.get("transition", 0),
383+
lor=Live(lor),
380384
)
381385

382386

@@ -439,3 +443,11 @@ def update_from_dict(self, data: dict) -> Device:
439443
self.state = State.from_dict(_state, self.effects, self.palettes)
440444

441445
return self
446+
447+
448+
class Live(IntEnum):
449+
"""Enumeration representing live override mode from WLED."""
450+
451+
OFF = 0
452+
ON = 1
453+
OFF_UNTIL_REBOOT = 2

src/wled/wled.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
WLEDEmptyResponseError,
2121
WLEDError,
2222
)
23-
from .models import Device
23+
from .models import Device, Live
2424

2525

2626
@dataclass
@@ -462,6 +462,14 @@ async def preset(self, preset: int) -> None:
462462
"""
463463
await self.request("state", method="POST", data={"ps": preset})
464464

465+
async def live(self, live: Live) -> None:
466+
"""Set the live override mode on a WLED device.
467+
468+
Args:
469+
live: The live override mode to set on this WLED device.
470+
"""
471+
await self.request("state", method="POST", data={"lor": live.value})
472+
465473
async def playlist(self, playlist: int) -> None:
466474
"""Set a running playlist on a WLED device.
467475

tests/test_wled.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,3 +568,75 @@ async def test_info_contains_no_wv(aresponses):
568568
wled = WLED("example.com", session=session)
569569
device = await wled.update()
570570
assert device.info.leds.wv
571+
572+
573+
@pytest.mark.asyncio
574+
async def test_live_override_state_off(aresponses):
575+
"""Test request of current WLED live override mode."""
576+
aresponses.add(
577+
"example.com",
578+
"/json/",
579+
"GET",
580+
aresponses.Response(
581+
status=200,
582+
headers={"Content-Type": "application/json"},
583+
text=(
584+
'{"state": {"on": true, "lor": 0},'
585+
'"effects": [], "palettes": [],'
586+
'"info": {"ver": "0.10.0"}}'
587+
),
588+
),
589+
)
590+
591+
async with aiohttp.ClientSession() as session:
592+
wled = WLED("example.com", session=session)
593+
device = await wled.update()
594+
assert device.state.lor == 0
595+
596+
597+
@pytest.mark.asyncio
598+
async def test_live_override_state_on(aresponses):
599+
"""Test request of current WLED live override mode."""
600+
aresponses.add(
601+
"example.com",
602+
"/json/",
603+
"GET",
604+
aresponses.Response(
605+
status=200,
606+
headers={"Content-Type": "application/json"},
607+
text=(
608+
'{"state": {"on": true, "lor": 1},'
609+
'"effects": [], "palettes": [],'
610+
'"info": {"ver": "0.10.0"}}'
611+
),
612+
),
613+
)
614+
615+
async with aiohttp.ClientSession() as session:
616+
wled = WLED("example.com", session=session)
617+
device = await wled.update()
618+
assert device.state.lor == 1
619+
620+
621+
@pytest.mark.asyncio
622+
async def test_live_override_state_off_until_reboot(aresponses):
623+
"""Test request of current WLED live override mode."""
624+
aresponses.add(
625+
"example.com",
626+
"/json/",
627+
"GET",
628+
aresponses.Response(
629+
status=200,
630+
headers={"Content-Type": "application/json"},
631+
text=(
632+
'{"state": {"on": true, "lor": 2},'
633+
'"effects": [], "palettes": [],'
634+
'"info": {"ver": "0.10.0"}}'
635+
),
636+
),
637+
)
638+
639+
async with aiohttp.ClientSession() as session:
640+
wled = WLED("example.com", session=session)
641+
device = await wled.update()
642+
assert device.state.lor == 2

0 commit comments

Comments
 (0)