Replace isinstance checks with polymorphic methods on PictureTarget hierarchy: register_with_manager, sync_with_manager, update_fields, and has_picture_source property. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
63 lines
2.3 KiB
Python
63 lines
2.3 KiB
Python
"""Picture target base data model."""
|
|
|
|
from dataclasses import dataclass
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
|
|
@dataclass
|
|
class PictureTarget:
|
|
"""Base class for picture targets."""
|
|
|
|
id: str
|
|
name: str
|
|
target_type: str # "wled", "key_colors", ...
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
description: Optional[str] = None
|
|
|
|
def register_with_manager(self, manager) -> None:
|
|
"""Register this target with the processor manager. Subclasses override."""
|
|
pass
|
|
|
|
def sync_with_manager(self, manager, *, settings_changed: bool, source_changed: bool, device_changed: bool) -> None:
|
|
"""Push changed fields to a running processor. Subclasses override."""
|
|
pass
|
|
|
|
def update_fields(self, *, name=None, device_id=None, picture_source_id=None,
|
|
settings=None, key_colors_settings=None, description=None) -> None:
|
|
"""Apply mutable field updates. Base handles common fields; subclasses handle type-specific ones."""
|
|
if name is not None:
|
|
self.name = name
|
|
if description is not None:
|
|
self.description = description
|
|
|
|
@property
|
|
def has_picture_source(self) -> bool:
|
|
"""Whether this target type uses a picture source."""
|
|
return False
|
|
|
|
def to_dict(self) -> dict:
|
|
"""Convert to dictionary."""
|
|
return {
|
|
"id": self.id,
|
|
"name": self.name,
|
|
"target_type": self.target_type,
|
|
"description": self.description,
|
|
"created_at": self.created_at.isoformat(),
|
|
"updated_at": self.updated_at.isoformat(),
|
|
}
|
|
|
|
@classmethod
|
|
def from_dict(cls, data: dict) -> "PictureTarget":
|
|
"""Create from dictionary, dispatching to the correct subclass."""
|
|
target_type = data.get("target_type", "wled")
|
|
# "wled" and "led" both map to WledPictureTarget (backward compat)
|
|
if target_type in ("wled", "led"):
|
|
from wled_controller.storage.wled_picture_target import WledPictureTarget
|
|
return WledPictureTarget.from_dict(data)
|
|
if target_type == "key_colors":
|
|
from wled_controller.storage.key_colors_picture_target import KeyColorsPictureTarget
|
|
return KeyColorsPictureTarget.from_dict(data)
|
|
raise ValueError(f"Unknown target type: {target_type}")
|