Files
project-work/sources/REFACTORING_PLAN.md
2026-11-21 19:05:37 -05:00

57 lines
3.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Template Light Refactoring Plan: 3-5 Turn Breakdown
## Turn 1: Data Structure & Handler Definitions
**Goal:** Add `ColorModeHandler` dataclass and `COLOR_MODE_HANDLERS` list
- Create `@dataclass ColorModeHandler` with all fields
- Define module-level `COLOR_MODE_HANDLERS: list[ColorModeHandler]` with 6 entries (Brightness, ColorTemp, HS, RGB, RGBW, RGBWW)
- Include: script/template config keys, state attr, property name, update callback, attr key, priority, clears list, build_script_vars lambda
- **Test:** Import succeeds, no behavior change
## Turn 2: Refactor `__init__` - Loop-Based Initialization
**Goal:** Replace 7× script/template/state init blocks with loop over handlers
- Loop `COLOR_MODE_HANDLERS`: create handler instance, init script if config has action, store template, init state attr to None
- Handle legacy `CONF_COLOR_ACTION`/`CONF_COLOR_TEMPLATE` → map to HS handler
- Build `supported_color_modes` from handlers that have scripts
- **Test:** All 130 tests pass
## Turn 3: Refactor `_async_setup_templates` - Loop-Based Registration
**Goal:** Replace 7× `add_template_attribute` blocks with loop
- Loop handlers: if handler.template exists, register with `add_template_attribute` using handler's state_attr and update_callback
- Keep special cases explicit (effect, min/max mireds, transition)
- **Test:** All 130 tests pass
## Turn 4: Refactor `async_turn_on` - Priority-Based Dispatch
**Goal:** Replace massive if/elif chain with priority-sorted loop
- **Optimistic state:** Loop handlers by priority; if `handler.template is None and handler.attr_key in kwargs` → set `_color_mode`, set state attr, clear `clears_on_optimistic_set` attrs, `optimistic_set=True`, break
- **Script dispatch:** Loop handlers by priority; if `handler.attr_key in kwargs and handler.script` → build vars via `handler.build_script_vars(kwargs)`, merge common_params, run script, break
- Fallback: brightness script → on script
- **Test:** All 130 tests pass (highest risk turn)
## Turn 5: Cleanup & Verify
**Goal:** Remove now-unused code, verify edge cases
- Delete individual `_update_*` callbacks? **No** - keep them (called by template system via string name)
- Verify legacy `set_color`/`color_template` still works
- Verify priority order: ColorTemp (20) > HS (30) > RGB (40) > RGBW (50) > RGBWW (60)
- Run full test suite, spot-check manual scenarios
- **Test:** All 130 tests pass
---
## Turn Mapping to Conversation
| Turn | My Response | Your Action |
|------|-------------|-------------|
| 1 | Implement dataclass + handlers | Review, pick, continue |
| 2 | Implement `__init__` loop | Review, pick, continue |
| 3 | Implement `_async_setup_templates` loop | Review, pick, continue |
| 4 | Implement `async_turn_on` dispatch | Review, pick, continue |
| 5 | Cleanup + final verify | Final pick, finish |
---
## Risk Mitigation
- **Turn 4 is highest risk** - If tests fail, we debug in that turn before proceeding
- Each turn is independently testable (run `pytest tests/components/template/test_light.py`)
- No turn requires changes to test file
- Legacy behavior preserved via explicit mapping in Turn 2