0f5850ef80
Extends the icon-plate work from devices and output targets to every
remaining card type — 18 new entities, 20 in total. Users can now pick
a curated icon (with optional colour override) for any card on any tab,
and the picker reuses the same modal, recent-strip, search, and
category tabs introduced for the device picker.
Foundation:
- icon-picker.ts — replace the hardcoded 2-entry adapter record with a
Map<EntityType, EntityTypeAdapter> and expose
registerIconEntityType() + makeSimpleIconAdapter() so each feature
module owns its own adapter (~6 lines per type).
- bodyExtras hook on adapters, keyed off id, lets discriminated routes
(output-targets target_type, picture-sources stream_type, audio /
value / color-strip-sources source_type) accept icon-only PUTs.
- core/card-icon.ts — new makeCardIconFields(type, id, entity) helper
spreads iconHtml / iconColor / iconAttrs into a mod-card head in one
line.
- _onDocumentClick now accepts any registered type instead of a
hardcoded device/target check.
Backend (purely additive — no migrations needed thanks to JSON-blob
storage):
- 18 dataclasses gained icon: str = "" + icon_color: str = "" with
emit-when-truthy serialisation and "" defaults on load.
- All matching Create / Update / Response Pydantic schemas gained the
fields with the standard Optional[str] + max_length=64/32 +
description set.
- All routes' response builders use
getattr(entity, "icon", "") or "" so existing rows render unchanged.
- ValueSource and CSS handle icon/icon_color on the base class so all
source-type subclasses inherit them automatically.
Frontend wiring (12 modules):
- streams.ts — picture sources, capture templates, PP templates,
CSPT, audio sources, audio templates, gradients (built-in
gradients keep no plate).
- automations, scene-presets, sync-clocks, weather-sources,
value-sources, mqtt-sources, home-assistant-sources,
game-integration, audio-processing-templates, assets,
color-strips/cards.
- pattern-templates skipped — uses the legacy wrapCard({content,
actions}) string API, separate migration.
Dashboard cards now also display the chosen icon:
- Targets already had it (with device inheritance for LED targets).
- Sync clocks, automations, and scene presets gained the same plate
via a shared _dashboardIconPlate helper that mirrors the mod-card
layout (mod-head--with-icon class flips on when present).
i18n: 20 new device.icon.entity.<type> labels in en/ru/zh.
Verification:
- ruff check src/ tests/ — clean.
- npx tsc --noEmit — clean.
- npm run build — 2.6 MB bundle.
- pytest tests/ --no-cov — 949 passed (no regressions).
Pending: manual smoke test on each card type — open picker, save, and
confirm the channel-color preview matches the live card.
165 lines
5.6 KiB
Python
165 lines
5.6 KiB
Python
"""Gradient routes: CRUD for reusable gradient definitions."""
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
|
|
from ledgrab.api.auth import AuthRequired
|
|
from ledgrab.api.dependencies import (
|
|
fire_entity_event,
|
|
get_color_strip_store,
|
|
get_gradient_store,
|
|
)
|
|
from ledgrab.api.schemas.gradients import (
|
|
GradientCreate,
|
|
GradientListResponse,
|
|
GradientResponse,
|
|
GradientUpdate,
|
|
)
|
|
from ledgrab.storage.gradient import Gradient
|
|
from ledgrab.storage.gradient_store import GradientStore
|
|
from ledgrab.storage.color_strip_store import ColorStripStore
|
|
from ledgrab.storage.base_store import EntityNotFoundError
|
|
from ledgrab.utils import get_logger
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
def _to_response(gradient: Gradient) -> GradientResponse:
|
|
return GradientResponse(
|
|
id=gradient.id,
|
|
name=gradient.name,
|
|
stops=[{"position": s["position"], "color": s["color"]} for s in gradient.stops],
|
|
is_builtin=gradient.is_builtin,
|
|
description=gradient.description,
|
|
tags=gradient.tags,
|
|
created_at=gradient.created_at,
|
|
updated_at=gradient.updated_at,
|
|
icon=getattr(gradient, "icon", "") or "",
|
|
icon_color=getattr(gradient, "icon_color", "") or "",
|
|
)
|
|
|
|
|
|
@router.get("/api/v1/gradients", response_model=GradientListResponse, tags=["Gradients"])
|
|
async def list_gradients(
|
|
_auth: AuthRequired,
|
|
store: GradientStore = Depends(get_gradient_store),
|
|
):
|
|
"""List all gradients (built-in + user-created)."""
|
|
gradients = store.get_all_gradients()
|
|
return GradientListResponse(
|
|
gradients=[_to_response(g) for g in gradients],
|
|
count=len(gradients),
|
|
)
|
|
|
|
|
|
@router.post(
|
|
"/api/v1/gradients", response_model=GradientResponse, status_code=201, tags=["Gradients"]
|
|
)
|
|
async def create_gradient(
|
|
data: GradientCreate,
|
|
_auth: AuthRequired,
|
|
store: GradientStore = Depends(get_gradient_store),
|
|
):
|
|
"""Create a new user-defined gradient."""
|
|
try:
|
|
gradient = store.create_gradient(
|
|
name=data.name,
|
|
stops=[s.model_dump() for s in data.stops],
|
|
description=data.description,
|
|
tags=data.tags,
|
|
icon=data.icon,
|
|
icon_color=data.icon_color,
|
|
)
|
|
fire_entity_event("gradient", "created", gradient.id)
|
|
return _to_response(gradient)
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
|
|
@router.get("/api/v1/gradients/{gradient_id}", response_model=GradientResponse, tags=["Gradients"])
|
|
async def get_gradient(
|
|
gradient_id: str,
|
|
_auth: AuthRequired,
|
|
store: GradientStore = Depends(get_gradient_store),
|
|
):
|
|
"""Get a gradient by ID."""
|
|
try:
|
|
gradient = store.get_gradient(gradient_id)
|
|
return _to_response(gradient)
|
|
except (ValueError, EntityNotFoundError) as e:
|
|
raise HTTPException(status_code=404, detail=str(e))
|
|
|
|
|
|
@router.put("/api/v1/gradients/{gradient_id}", response_model=GradientResponse, tags=["Gradients"])
|
|
async def update_gradient(
|
|
gradient_id: str,
|
|
data: GradientUpdate,
|
|
_auth: AuthRequired,
|
|
store: GradientStore = Depends(get_gradient_store),
|
|
):
|
|
"""Update a gradient (built-in gradients are read-only)."""
|
|
try:
|
|
stops = [s.model_dump() for s in data.stops] if data.stops is not None else None
|
|
gradient = store.update_gradient(
|
|
gradient_id=gradient_id,
|
|
name=data.name,
|
|
stops=stops,
|
|
description=data.description,
|
|
tags=data.tags,
|
|
icon=data.icon,
|
|
icon_color=data.icon_color,
|
|
)
|
|
fire_entity_event("gradient", "updated", gradient_id)
|
|
return _to_response(gradient)
|
|
except (ValueError, EntityNotFoundError) as e:
|
|
status = 404 if "not found" in str(e).lower() else 400
|
|
raise HTTPException(status_code=status, detail=str(e))
|
|
|
|
|
|
@router.post(
|
|
"/api/v1/gradients/{gradient_id}/clone",
|
|
response_model=GradientResponse,
|
|
status_code=201,
|
|
tags=["Gradients"],
|
|
)
|
|
async def clone_gradient(
|
|
gradient_id: str,
|
|
_auth: AuthRequired,
|
|
store: GradientStore = Depends(get_gradient_store),
|
|
):
|
|
"""Clone a gradient (useful for customizing built-in gradients)."""
|
|
try:
|
|
original = store.get_gradient(gradient_id)
|
|
clone = store.create_gradient(
|
|
name=f"{original.name} (copy)",
|
|
stops=original.stops,
|
|
description=original.description,
|
|
tags=original.tags,
|
|
)
|
|
fire_entity_event("gradient", "created", clone.id)
|
|
return _to_response(clone)
|
|
except (ValueError, EntityNotFoundError) as e:
|
|
status = 404 if "not found" in str(e).lower() else 400
|
|
raise HTTPException(status_code=status, detail=str(e))
|
|
|
|
|
|
@router.delete("/api/v1/gradients/{gradient_id}", status_code=204, tags=["Gradients"])
|
|
async def delete_gradient(
|
|
gradient_id: str,
|
|
_auth: AuthRequired,
|
|
store: GradientStore = Depends(get_gradient_store),
|
|
css_store: ColorStripStore = Depends(get_color_strip_store),
|
|
):
|
|
"""Delete a gradient (fails if built-in or referenced by sources)."""
|
|
try:
|
|
# Check references
|
|
for source in css_store.get_all_sources():
|
|
if getattr(source, "gradient_id", None) == gradient_id:
|
|
raise ValueError(f"Cannot delete: referenced by color strip source '{source.name}'")
|
|
store.delete_gradient(gradient_id)
|
|
fire_entity_event("gradient", "deleted", gradient_id)
|
|
except (ValueError, EntityNotFoundError) as e:
|
|
status = 404 if "not found" in str(e).lower() else 400
|
|
raise HTTPException(status_code=status, detail=str(e))
|