feat(playlists): expose scene playlists as Home Assistant entities

One device per scene playlist (model "Scene Playlist"), each with:
- a switch (start/stop; the server cycles one playlist at a time, so starting one stops any other)
- a "Current Scene" sensor that resolves the active preset name from scene_presets while this playlist is cycling
- an "Items" diagnostic sensor (configured scene count; no state_class)

The coordinator reads scene_playlists plus the flat playlist_state from the /api/v1/snapshot payload and gains start_playlist()/stop_playlist(); __init__ registers and prunes per-playlist devices and reloads on playlist-id changes; the event listener also refreshes on the playlist_state_changed WS event. Shared device/lookup/running-state plumbing lives in a new LedGrabPlaylistEntity base (entity.py) used by both the switch and the sensors. Adds en/ru translation keys.

Note: this also lands the in-progress coordinator migration from the per-request fan-out to the single /api/v1/snapshot poll that was already present in the working tree.

Requires the led-grab server /api/v1/snapshot to emit scene_playlists + playlist_state (companion server change, tracked separately).
This commit is contained in:
2026-06-08 15:59:08 +03:00
parent a666d9eb9c
commit 705616f8b0
9 changed files with 404 additions and 289 deletions
+70
View File
@@ -12,6 +12,7 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN, DATA_COORDINATOR
from .coordinator import LedGrabCoordinator
from .entity import LedGrabPlaylistEntity
_LOGGER = logging.getLogger(__name__)
@@ -38,6 +39,11 @@ async def async_setup_entry(
LedGrabSyncClockSwitch(coordinator, clock["id"], entry.entry_id)
)
for playlist in coordinator.data.get("scene_playlists", []):
entities.append(
LedGrabPlaylistSwitch(coordinator, playlist["id"], entry.entry_id)
)
async_add_entities(entities)
@@ -164,3 +170,67 @@ class LedGrabSyncClockSwitch(CoordinatorEntity, SwitchEntity):
if clock.get("id") == self._clock_id:
return clock
return None
class LedGrabPlaylistSwitch(LedGrabPlaylistEntity, SwitchEntity):
"""Start/stop control for a scene playlist.
On = this playlist is cycling, off = stopped. The server runs at most one
playlist at a time, so turning one on stops any other that was running (the
other switches flip off on the next refresh).
Device/availability/lookup plumbing lives in :class:`LedGrabPlaylistEntity`
so it stays in sync with the playlist stats sensors.
"""
_attr_icon = "mdi:playlist-play"
def __init__(
self,
coordinator: LedGrabCoordinator,
playlist_id: str,
entry_id: str,
) -> None:
super().__init__(coordinator, playlist_id, entry_id)
self._attr_unique_id = f"{playlist_id}_active"
self._attr_translation_key = "playlist_active"
@property
def is_on(self) -> bool:
playlist = self._get_playlist()
if not playlist:
return False
return bool(playlist.get("is_running", False))
@property
def extra_state_attributes(self) -> dict[str, Any]:
playlist = self._get_playlist()
if not playlist:
return {}
attrs: dict[str, Any] = {
"playlist_id": self._playlist_id,
"item_count": len(playlist.get("items") or []),
"loop": playlist.get("loop"),
"shuffle": playlist.get("shuffle"),
"tags": playlist.get("tags") or [],
}
# Runtime cycling details only apply to whichever playlist is running.
state = self._running_state()
if state:
attrs["current_index"] = state.get("current_index")
attrs["current_preset_id"] = state.get("current_preset_id")
attrs["step_duration"] = state.get("step_duration")
attrs["started_at"] = state.get("started_at")
return attrs
async def async_turn_on(self, **kwargs: Any) -> None:
await self.coordinator.start_playlist(self._playlist_id)
async def async_turn_off(self, **kwargs: Any) -> None:
# Stop is global; only act if this playlist is the one actually cycling
# so toggling an already-idle switch can't stop a different playlist.
if self.is_on:
await self.coordinator.stop_playlist()