Encapsulate target-type dispatch via polymorphism (Phase 1)

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>
This commit is contained in:
2026-02-20 02:20:33 +03:00
parent 3101894ab5
commit 99f47fdbf9
6 changed files with 109 additions and 69 deletions

View File

@@ -74,6 +74,34 @@ class KeyColorsPictureTarget(PictureTarget):
picture_source_id: str = ""
settings: KeyColorsSettings = field(default_factory=KeyColorsSettings)
def register_with_manager(self, manager) -> None:
"""Register this KC target with the processor manager."""
manager.add_kc_target(
target_id=self.id,
picture_source_id=self.picture_source_id,
settings=self.settings,
)
def sync_with_manager(self, manager, *, settings_changed: bool, source_changed: bool, device_changed: bool) -> None:
"""Push changed fields to the processor manager."""
if settings_changed:
manager.update_target_settings(self.id, self.settings)
if source_changed:
manager.update_target_source(self.id, self.picture_source_id)
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 for KC targets."""
super().update_fields(name=name, description=description)
if picture_source_id is not None:
self.picture_source_id = picture_source_id
if key_colors_settings is not None:
self.settings = key_colors_settings
@property
def has_picture_source(self) -> bool:
return True
def to_dict(self) -> dict:
d = super().to_dict()
d["picture_source_id"] = self.picture_source_id

View File

@@ -16,6 +16,27 @@ class PictureTarget:
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 {

View File

@@ -190,24 +190,15 @@ class PictureTargetStore:
for other in self._targets.values():
if other.id != target_id and other.name == name:
raise ValueError(f"Picture target with name '{name}' already exists")
target.name = name
if description is not None:
target.description = description
if isinstance(target, WledPictureTarget):
if device_id is not None:
target.device_id = device_id
if picture_source_id is not None:
target.picture_source_id = picture_source_id
if settings is not None:
target.settings = settings
if isinstance(target, KeyColorsPictureTarget):
if picture_source_id is not None:
target.picture_source_id = picture_source_id
if key_colors_settings is not None:
target.settings = key_colors_settings
target.update_fields(
name=name,
device_id=device_id,
picture_source_id=picture_source_id,
settings=settings,
key_colors_settings=key_colors_settings,
description=description,
)
target.updated_at = datetime.utcnow()
self._save()
@@ -239,7 +230,7 @@ class PictureTargetStore:
def is_referenced_by_source(self, source_id: str) -> bool:
"""Check if any target references a picture source."""
for target in self._targets.values():
if isinstance(target, (WledPictureTarget, KeyColorsPictureTarget)) and target.picture_source_id == source_id:
if target.has_picture_source and target.picture_source_id == source_id:
return True
return False

View File

@@ -16,6 +16,40 @@ class WledPictureTarget(PictureTarget):
picture_source_id: str = ""
settings: ProcessingSettings = field(default_factory=ProcessingSettings)
def register_with_manager(self, manager) -> None:
"""Register this WLED target with the processor manager."""
if self.device_id:
manager.add_target(
target_id=self.id,
device_id=self.device_id,
settings=self.settings,
picture_source_id=self.picture_source_id,
)
def sync_with_manager(self, manager, *, settings_changed: bool, source_changed: bool, device_changed: bool) -> None:
"""Push changed fields to the processor manager."""
if settings_changed:
manager.update_target_settings(self.id, self.settings)
if source_changed:
manager.update_target_source(self.id, self.picture_source_id)
if device_changed:
manager.update_target_device(self.id, self.device_id)
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 for WLED targets."""
super().update_fields(name=name, description=description)
if device_id is not None:
self.device_id = device_id
if picture_source_id is not None:
self.picture_source_id = picture_source_id
if settings is not None:
self.settings = settings
@property
def has_picture_source(self) -> bool:
return True
def to_dict(self) -> dict:
"""Convert to dictionary."""
d = super().to_dict()