refactor: replace type-dispatch if/elif chains with registry patterns and handler maps
Lint & Test / test (push) Failing after 30s
Lint & Test / test (push) Failing after 30s
Backend: add registry dicts (_CONDITION_MAP, _VALUE_SOURCE_MAP, _PICTURE_SOURCE_MAP) and per-subclass from_dict() methods to eliminate ~300 lines of if/elif in factory functions. Convert automation engine dispatch (condition eval, match_mode, match_type, deactivation_mode) to dict-based lookup. Frontend: extract CSS_CARD_RENDERERS, CSS_SECTION_MAP, CSS_TYPE_SETUP, CONDITION_PILL_RENDERERS, and PICTURE_SOURCE_CARD_RENDERERS handler maps to replace scattered type-check chains in color-strips.ts, automations.ts, and streams.ts.
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime, timezone
|
||||
from typing import List, Optional
|
||||
from typing import Dict, List, Optional, Type
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -16,25 +16,12 @@ class Condition:
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: dict) -> "Condition":
|
||||
"""Factory: dispatch to the correct subclass."""
|
||||
"""Factory: dispatch to the correct subclass via registry."""
|
||||
ct = data.get("condition_type", "")
|
||||
if ct == "always":
|
||||
return AlwaysCondition.from_dict(data)
|
||||
if ct == "application":
|
||||
return ApplicationCondition.from_dict(data)
|
||||
if ct == "time_of_day":
|
||||
return TimeOfDayCondition.from_dict(data)
|
||||
if ct == "system_idle":
|
||||
return SystemIdleCondition.from_dict(data)
|
||||
if ct == "display_state":
|
||||
return DisplayStateCondition.from_dict(data)
|
||||
if ct == "mqtt":
|
||||
return MQTTCondition.from_dict(data)
|
||||
if ct == "webhook":
|
||||
return WebhookCondition.from_dict(data)
|
||||
if ct == "startup":
|
||||
return StartupCondition.from_dict(data)
|
||||
raise ValueError(f"Unknown condition type: {ct}")
|
||||
subcls = _CONDITION_MAP.get(ct)
|
||||
if subcls is None:
|
||||
raise ValueError(f"Unknown condition type: {ct}")
|
||||
return subcls.from_dict(data)
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -190,6 +177,18 @@ class StartupCondition(Condition):
|
||||
return cls()
|
||||
|
||||
|
||||
_CONDITION_MAP: Dict[str, Type[Condition]] = {
|
||||
"always": AlwaysCondition,
|
||||
"application": ApplicationCondition,
|
||||
"time_of_day": TimeOfDayCondition,
|
||||
"system_idle": SystemIdleCondition,
|
||||
"display_state": DisplayStateCondition,
|
||||
"mqtt": MQTTCondition,
|
||||
"webhook": WebhookCondition,
|
||||
"startup": StartupCondition,
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class Automation:
|
||||
"""Automation that activates a scene preset based on conditions."""
|
||||
|
||||
Reference in New Issue
Block a user