Add entity picker support
This commit is contained in:
@@ -3,14 +3,11 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, ServiceCall, ServiceResponse, SupportsResponse
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import (
|
||||
ATTR_ALBUM_ID,
|
||||
CONF_ALBUMS,
|
||||
CONF_API_KEY,
|
||||
CONF_IMMICH_URL,
|
||||
@@ -18,25 +15,11 @@ from .const import (
|
||||
DEFAULT_SCAN_INTERVAL,
|
||||
DOMAIN,
|
||||
PLATFORMS,
|
||||
SERVICE_GET_RECENT_ASSETS,
|
||||
SERVICE_REFRESH,
|
||||
)
|
||||
from .coordinator import ImmichAlbumWatcherCoordinator
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
# Service schemas
|
||||
SERVICE_REFRESH_SCHEMA = vol.Schema({})
|
||||
|
||||
SERVICE_GET_RECENT_ASSETS_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(ATTR_ALBUM_ID): cv.string,
|
||||
vol.Optional("count", default=10): vol.All(
|
||||
vol.Coerce(int), vol.Range(min=1, max=100)
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Set up Immich Album Watcher from a config entry."""
|
||||
@@ -66,9 +49,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
# Register update listener for options changes
|
||||
entry.async_on_unload(entry.add_update_listener(async_update_options))
|
||||
|
||||
# Register services (only once)
|
||||
await async_setup_services(hass)
|
||||
|
||||
_LOGGER.info(
|
||||
"Immich Album Watcher set up successfully, watching %d albums",
|
||||
len(album_ids),
|
||||
@@ -77,46 +57,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_services(hass: HomeAssistant) -> None:
|
||||
"""Set up services for Immich Album Watcher."""
|
||||
if hass.services.has_service(DOMAIN, SERVICE_REFRESH):
|
||||
return # Services already registered
|
||||
|
||||
async def handle_refresh(call: ServiceCall) -> None:
|
||||
"""Handle the refresh service call."""
|
||||
for coordinator in hass.data[DOMAIN].values():
|
||||
if isinstance(coordinator, ImmichAlbumWatcherCoordinator):
|
||||
await coordinator.async_refresh_now()
|
||||
|
||||
async def handle_get_recent_assets(call: ServiceCall) -> ServiceResponse:
|
||||
"""Handle the get_recent_assets service call."""
|
||||
album_id = call.data[ATTR_ALBUM_ID]
|
||||
count = call.data.get("count", 10)
|
||||
|
||||
for coordinator in hass.data[DOMAIN].values():
|
||||
if isinstance(coordinator, ImmichAlbumWatcherCoordinator):
|
||||
if coordinator.data and album_id in coordinator.data:
|
||||
assets = await coordinator.async_get_recent_assets(album_id, count)
|
||||
return {"assets": assets}
|
||||
|
||||
return {"assets": [], "error": f"Album {album_id} not found"}
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN,
|
||||
SERVICE_REFRESH,
|
||||
handle_refresh,
|
||||
schema=SERVICE_REFRESH_SCHEMA,
|
||||
)
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN,
|
||||
SERVICE_GET_RECENT_ASSETS,
|
||||
handle_get_recent_assets,
|
||||
schema=SERVICE_GET_RECENT_ASSETS_SCHEMA,
|
||||
supports_response=SupportsResponse.ONLY,
|
||||
)
|
||||
|
||||
|
||||
async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||
"""Handle options update."""
|
||||
coordinator: ImmichAlbumWatcherCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
@@ -137,9 +77,4 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
if unload_ok:
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
# Unregister services if no more entries
|
||||
if not hass.data[DOMAIN]:
|
||||
hass.services.async_remove(DOMAIN, SERVICE_REFRESH)
|
||||
hass.services.async_remove(DOMAIN, SERVICE_GET_RECENT_ASSETS)
|
||||
|
||||
return unload_ok
|
||||
|
||||
Reference in New Issue
Block a user