Refactor HAOS integration to use shared core library (Phase 2)
Some checks failed
Validate / Hassfest (push) Has been cancelled

Wire the integration to delegate all HA-independent logic to
immich-watcher-core, eliminating ~2300 lines of duplicated code.

Changes:
- const.py: Import shared constants from core, keep HA-specific ones
- storage.py: Create HAStorageBackend adapter wrapping HA's Store,
  use core TelegramFileCache and NotificationQueue via adapter
- coordinator.py: Delegate to core ImmichClient for API calls,
  detect_album_changes() for change detection, and asset_utils
  for filtering/sorting/URL building. Keep HA-specific event firing.
- sensor.py: Replace ~1300 lines of Telegram code with 15-line
  delegation to core TelegramClient. Keep entity classes unchanged.
- __init__.py: Use factory functions for creating core instances
  with HA storage backends
- manifest.json: Add immich-watcher-core dependency

Integration line count: 3600 -> 1295 lines (-64%)
Zero behavior changes for end users.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-19 12:47:18 +03:00
parent d0783d0b6a
commit b107cfe67f
8 changed files with 502 additions and 2804 deletions

View File

@@ -0,0 +1,75 @@
# Phase 2: Wire Core Library into HAOS Integration
**Status**: In progress
**Parent**: [primary-plan.md](primary-plan.md)
---
## Goal
Refactor the HAOS integration to delegate to `immich-watcher-core` for all HA-independent logic, reducing duplication and preparing for the standalone server.
---
## Important: HACS Compatibility
HACS requires `custom_components/<domain>/` at the repository root. We **cannot** move it to `packages/haos/`. Instead:
- `custom_components/` stays at repo root
- The integration imports from `immich_watcher_core` (the core library)
- `manifest.json` lists `immich-watcher-core` in `requirements` (for future PyPI publish)
- During development, `pip install -e packages/core` makes imports work
- For HACS distribution, we'll publish the core to PyPI
---
## Tasks
### 1. Update manifest.json `[ ]`
- Add `immich-watcher-core` to requirements list
- Do NOT bump version (only plans/core changed, not integration content yet)
### 2. Refactor const.py `[ ]`
- Import shared constants from `immich_watcher_core.constants`
- Keep HA-specific constants (DOMAIN, CONF_*, PLATFORMS, SERVICE_*) local
- Re-export shared constants for backward compatibility with other integration files
### 3. Refactor storage.py `[ ]`
- Create `HAStorageBackend` adapter wrapping `homeassistant.helpers.storage.Store`
that satisfies `StorageBackend` protocol from core
- Replace `TelegramFileCache` with core's version using `HAStorageBackend`
- Replace `NotificationQueue` with core's version using `HAStorageBackend`
- Keep `ImmichAlbumStorage` as-is (HA-specific album state management)
### 4. Refactor coordinator.py `[ ]`
- Remove dataclass definitions (SharedLinkInfo, AssetInfo, AlbumData, AlbumChange) — import from core
- Replace Immich API methods with `ImmichClient` from core
- Replace `_detect_change()` with `detect_album_changes()` from core
- Replace `_build_asset_detail()` and URL helpers with `asset_utils` from core
- Replace `async_get_assets()` filtering/sorting with `filter_assets()` + `sort_assets()` from core
- Keep HA-specific: `DataUpdateCoordinator` subclass, `_fire_events()`, `async_get_clientsession()`
### 5. Refactor sensor.py `[ ]`
- Remove Telegram constants, helper functions, and all `_send_telegram_*` methods
- Replace with `TelegramClient` from core in `_execute_telegram_notification()`
- Keep HA-specific: entity classes, service registration, platform setup
### 6. Update __init__.py `[ ]`
- Update imports for new storage classes (HAStorageBackend adapter)
- Create TelegramFileCache instances using core class + HA adapter
### 7. Verify `[ ]`
- All existing entities, services, and events work identically
- Telegram notifications work with caching
- Quiet hours queueing works
- No HA import in core library (verify with grep)
---
## Acceptance Criteria
- [ ] Integration imports and delegates to `immich_watcher_core`
- [ ] Zero behavior changes for end users
- [ ] No duplicated logic between core and integration
- [ ] Core library has no HA imports (verified)
- [ ] `ImmichAlbumStorage` is the only storage class still HA-native

View File

@@ -179,7 +179,7 @@ async def _execute_telegram_notification(self, ...):
- Write unit tests for all extracted modules
- **Subplan**: `plans/phase-1-core-library.md`
### Phase 2: Wire Core into HAOS Integration `[ ]`
### Phase 2: Wire Core into HAOS Integration `[x]`
- Move integration to `packages/haos/`
- Refactor coordinator.py, sensor.py, storage.py to use core library
- Update manifest.json, hacs.json for new structure