Add static color support, HAOS light entity, and real-time profile updates

- Add static_color capability to WLED and serial providers with native
  set_color() dispatch (WLED uses JSON API, serial uses idle client)
- Encapsulate device-specific logic in providers instead of device_type
  checks in ProcessorManager and API routes
- Add HAOS light entity for devices with brightness_control + static_color
  (Adalight/AmbiLED get light entity, WLED keeps number entity)
- Fix serial device brightness and turn-off: pass software_brightness
  through provider chain, clear device on color=null, re-send static
  color after brightness change
- Add global events WebSocket (events-ws.js) replacing per-tab WS,
  enabling real-time profile state updates on both dashboard and profiles tabs
- Fix profile activation: mark active when all targets already running,
  add asyncio.Lock to prevent concurrent evaluation races, skip process
  enumeration when no profile has conditions, trigger immediate evaluation
  on enable/create/update for instant target startup
- Add reliable server restart script (restart.ps1)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-19 14:23:47 +03:00
parent 6388e0defa
commit bef28ece5c
21 changed files with 410 additions and 78 deletions

View File

@@ -119,18 +119,28 @@ class SerialDeviceProvider(LEDDeviceProvider):
async def set_color(self, url: str, color: Tuple[int, int, int], **kwargs) -> None:
"""Send a solid color frame to the device.
Requires kwargs: led_count (int), baud_rate (int | None).
Accepts optional kwargs:
client: An already-connected LEDClient (e.g. cached idle client).
brightness (int): Software brightness 0-255 (default 255).
led_count (int), baud_rate (int | None).
"""
led_count = kwargs.get("led_count", 0)
baud_rate = kwargs.get("baud_rate")
if led_count <= 0:
raise ValueError(f"led_count is required to send color frame to {self.device_type} device")
client = self.create_client(url, led_count=led_count, baud_rate=baud_rate)
try:
await client.connect()
frame = np.full((led_count, 3), color, dtype=np.uint8)
await client.send_pixels(frame, brightness=255)
logger.info(f"{self.device_type} set_color: sent solid {color} to {url}")
finally:
await client.close()
brightness = kwargs.get("brightness", 255)
frame = np.full((led_count, 3), color, dtype=np.uint8)
existing_client = kwargs.get("client")
if existing_client:
await existing_client.send_pixels(frame, brightness=brightness)
else:
baud_rate = kwargs.get("baud_rate")
client = self.create_client(url, led_count=led_count, baud_rate=baud_rate)
try:
await client.connect()
await client.send_pixels(frame, brightness=brightness)
finally:
await client.close()
logger.info(f"{self.device_type} set_color: sent solid {color} to {url}")

View File

@@ -1,7 +1,7 @@
"""WLED device provider — consolidates all WLED-specific dispatch logic."""
import asyncio
from typing import List, Optional
from typing import List, Optional, Tuple
import httpx
from zeroconf import ServiceStateChange
@@ -30,7 +30,7 @@ class WLEDDeviceProvider(LEDDeviceProvider):
@property
def capabilities(self) -> set:
return {"brightness_control", "power_control", "standby_required"}
return {"brightness_control", "power_control", "standby_required", "static_color"}
def create_client(self, url: str, **kwargs) -> LEDClient:
from wled_controller.core.devices.wled_client import WLEDClient
@@ -186,3 +186,16 @@ class WLEDDeviceProvider(LEDDeviceProvider):
json={"on": on},
)
resp.raise_for_status()
async def set_color(self, url: str, color: Tuple[int, int, int], **kwargs) -> None:
"""Set WLED to a solid color using the native segment color API."""
url = url.rstrip("/")
async with httpx.AsyncClient(timeout=5.0) as http_client:
resp = await http_client.post(
f"{url}/json/state",
json={
"on": True,
"seg": [{"col": [[color[0], color[1], color[2]]], "fx": 0}],
},
)
resp.raise_for_status()

View File

@@ -597,18 +597,31 @@ class ProcessorManager:
return None
async def send_static_color(self, device_id: str, color: Tuple[int, int, int]) -> None:
"""Send a solid color to a device via the cached idle client."""
import numpy as np
"""Send a solid color to a device via its provider."""
ds = self._devices.get(device_id)
if not ds:
raise ValueError(f"Device {device_id} not found")
try:
provider = get_provider(ds.device_type)
client = await self._get_idle_client(device_id)
frame = np.full((ds.led_count, 3), color, dtype=np.uint8)
await client.send_pixels(frame)
await provider.set_color(
ds.device_url, color,
led_count=ds.led_count, baud_rate=ds.baud_rate, client=client,
brightness=ds.software_brightness,
)
except Exception as e:
logger.error(f"Failed to send static color for {device_id}: {e}")
async def clear_device(self, device_id: str) -> None:
"""Clear LED output on a device (send black / power off)."""
ds = self._devices.get(device_id)
if not ds:
raise ValueError(f"Device {device_id} not found")
try:
await self._send_clear_pixels(device_id)
except Exception as e:
logger.error(f"Failed to clear device {device_id}: {e}")
async def _restore_device_idle_state(self, device_id: str) -> None:
"""Restore a device to its idle state when all targets stop.

View File

@@ -21,6 +21,7 @@ class ProfileEngine:
self._poll_interval = poll_interval
self._detector = PlatformDetector()
self._task: Optional[asyncio.Task] = None
self._eval_lock = asyncio.Lock()
# Runtime state (not persisted)
# profile_id → set of target_ids that THIS profile started
@@ -64,6 +65,10 @@ class ProfileEngine:
pass
async def _evaluate_all(self) -> None:
async with self._eval_lock:
await self._evaluate_all_locked()
async def _evaluate_all_locked(self) -> None:
profiles = self._store.get_all_profiles()
if not profiles:
# No profiles — deactivate any stale state
@@ -71,9 +76,18 @@ class ProfileEngine:
await self._deactivate_profile(pid)
return
# Gather platform state once per cycle
running_procs = await self._detector.get_running_processes()
topmost_proc = await self._detector.get_topmost_process()
# Only enumerate processes when at least one enabled profile has conditions
needs_detection = any(
p.enabled and len(p.conditions) > 0
for p in profiles
)
if needs_detection:
running_procs = await self._detector.get_running_processes()
topmost_proc = await self._detector.get_topmost_process()
else:
running_procs = set()
topmost_proc = None
active_profile_ids = set()
@@ -139,6 +153,7 @@ class ProfileEngine:
async def _activate_profile(self, profile: Profile) -> None:
started: Set[str] = set()
failed = False
for target_id in profile.target_ids:
try:
# Skip targets that are already running (manual or other profile)
@@ -150,15 +165,17 @@ class ProfileEngine:
started.add(target_id)
logger.info(f"Profile '{profile.name}' started target {target_id}")
except Exception as e:
failed = True
logger.warning(f"Profile '{profile.name}' failed to start target {target_id}: {e}")
if started:
if started or not failed:
# Active: either we started targets, or all were already running
self._active_profiles[profile.id] = started
self._last_activated[profile.id] = datetime.now(timezone.utc)
self._fire_event(profile.id, "activated", list(started))
logger.info(f"Profile '{profile.name}' activated ({len(started)} targets)")
logger.info(f"Profile '{profile.name}' activated ({len(started)} targets started)")
else:
logger.debug(f"Profile '{profile.name}' matched but no targets started — will retry")
logger.debug(f"Profile '{profile.name}' matched but targets failed to start — will retry")
async def _deactivate_profile(self, profile_id: str) -> None:
owned = self._active_profiles.pop(profile_id, set())
@@ -210,6 +227,13 @@ class ProfileEngine:
result[profile.id] = self.get_profile_state(profile.id)
return result
async def trigger_evaluate(self) -> None:
"""Run a single evaluation cycle immediately (used after enabling a profile)."""
try:
await self._evaluate_all()
except Exception as e:
logger.error(f"Immediate profile evaluation error: {e}", exc_info=True)
async def deactivate_if_active(self, profile_id: str) -> None:
"""Deactivate a profile immediately (used when disabling/deleting)."""
if profile_id in self._active_profiles: