"""Scenes (owner 2026-11-21, models/scene.py): which are on now, and telling what follows them. A scene is on when it was turned on and its `until` (if any) has not passed. When scenes change, the signage screens are told (their schedules may pick another playlist) and the prices are recomputed by whoever asks next (price rules read `active_ids` through the pricing context). """ from __future__ import annotations from datetime import datetime from core.timeutil import utcnow def _aware(dt): from datetime import timezone return dt if dt is None or dt.tzinfo else dt.replace(tzinfo=timezone.utc) def is_on(scene, now: datetime | None = None) -> bool: now = now or utcnow() return bool(scene.is_active) and (scene.until is None or _aware(scene.until) > _aware(now)) def active_ids(db, now: datetime | None = None) -> set[int]: from models.scene import Scene return {s.id for s in db.query(Scene).filter(Scene.is_active.is_(True)) if is_on(s, now)} def out(scene, now: datetime | None = None) -> dict: return {"id": scene.id, "name": scene.name, "color": scene.color, "description": scene.description, "active": is_on(scene, now), "activated_at": scene.activated_at, "until": scene.until, "sort_order": scene.sort_order} def changed(db) -> None: """Scenes changed: the signage screens look again; the dashboards refresh their scene lists.""" from services.ws_bus import broadcast_sync broadcast_sync("scenes.changed", {}) try: from routers.signage import tell_all_screens tell_all_screens(db) except Exception: # signage off or not loaded: nothing to tell pass