Implement hub and subenty approach based on telegram bot integration implementation
This commit is contained in:
@@ -5,8 +5,9 @@ from __future__ import annotations
|
||||
import logging
|
||||
|
||||
from homeassistant.components.text import TextEntity, TextMode
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.config_entries import ConfigEntry, ConfigSubentry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
@@ -14,7 +15,8 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
from .const import (
|
||||
ATTR_ALBUM_ID,
|
||||
ATTR_ALBUM_PROTECTED_URL,
|
||||
CONF_ALBUMS,
|
||||
CONF_ALBUM_ID,
|
||||
CONF_ALBUM_NAME,
|
||||
DOMAIN,
|
||||
)
|
||||
from .coordinator import AlbumData, ImmichAlbumWatcherCoordinator
|
||||
@@ -28,14 +30,19 @@ async def async_setup_entry(
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up Immich Album Watcher text entities from a config entry."""
|
||||
coordinator: ImmichAlbumWatcherCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
album_ids = entry.options.get(CONF_ALBUMS, [])
|
||||
# Iterate through all album subentries
|
||||
for subentry_id, subentry in entry.subentries.items():
|
||||
subentry_data = hass.data[DOMAIN][entry.entry_id]["subentries"].get(subentry_id)
|
||||
if not subentry_data:
|
||||
_LOGGER.error("Subentry data not found for %s", subentry_id)
|
||||
continue
|
||||
|
||||
entities: list[TextEntity] = []
|
||||
for album_id in album_ids:
|
||||
entities.append(ImmichAlbumProtectedPasswordText(coordinator, entry, album_id))
|
||||
coordinator = subentry_data.coordinator
|
||||
|
||||
async_add_entities(entities)
|
||||
async_add_entities(
|
||||
[ImmichAlbumProtectedPasswordText(coordinator, entry, subentry)],
|
||||
config_subentry_id=subentry_id,
|
||||
)
|
||||
|
||||
|
||||
class ImmichAlbumProtectedPasswordText(
|
||||
@@ -53,27 +60,27 @@ class ImmichAlbumProtectedPasswordText(
|
||||
self,
|
||||
coordinator: ImmichAlbumWatcherCoordinator,
|
||||
entry: ConfigEntry,
|
||||
album_id: str,
|
||||
subentry: ConfigSubentry,
|
||||
) -> None:
|
||||
"""Initialize the text entity."""
|
||||
super().__init__(coordinator)
|
||||
self._album_id = album_id
|
||||
self._entry = entry
|
||||
self._attr_unique_id = f"{entry.entry_id}_{album_id}_protected_password_edit"
|
||||
self._subentry = subentry
|
||||
self._album_id = subentry.data[CONF_ALBUM_ID]
|
||||
self._album_name = subentry.data.get(CONF_ALBUM_NAME, "Unknown Album")
|
||||
self._attr_unique_id = f"{subentry.subentry_id}_protected_password_edit"
|
||||
|
||||
@property
|
||||
def _album_data(self) -> AlbumData | None:
|
||||
"""Get the album data from coordinator."""
|
||||
if self.coordinator.data is None:
|
||||
return None
|
||||
return self.coordinator.data.get(self._album_id)
|
||||
return self.coordinator.data
|
||||
|
||||
@property
|
||||
def translation_placeholders(self) -> dict[str, str]:
|
||||
"""Return translation placeholders."""
|
||||
if self._album_data:
|
||||
return {"album_name": self._album_data.name}
|
||||
return {"album_name": f"Album {self._album_id[:8]}"}
|
||||
return {"album_name": self._album_name}
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
@@ -84,23 +91,24 @@ class ImmichAlbumProtectedPasswordText(
|
||||
if not self.coordinator.last_update_success or self._album_data is None:
|
||||
return False
|
||||
# Only available if there's a protected link to edit
|
||||
return self.coordinator.get_album_protected_link_id(self._album_id) is not None
|
||||
return self.coordinator.get_protected_link_id() is not None
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Return device info."""
|
||||
"""Return device info - one device per album."""
|
||||
return DeviceInfo(
|
||||
identifiers={(DOMAIN, self._entry.entry_id)},
|
||||
name="Immich Album Watcher",
|
||||
identifiers={(DOMAIN, self._subentry.subentry_id)},
|
||||
name=self._album_name,
|
||||
manufacturer="Immich",
|
||||
entry_type="service",
|
||||
entry_type=DeviceEntryType.SERVICE,
|
||||
via_device=(DOMAIN, self._entry.entry_id),
|
||||
)
|
||||
|
||||
@property
|
||||
def native_value(self) -> str | None:
|
||||
"""Return the current password value."""
|
||||
if self._album_data:
|
||||
return self.coordinator.get_album_protected_password(self._album_id)
|
||||
return self.coordinator.get_protected_password()
|
||||
return None
|
||||
|
||||
@property
|
||||
@@ -113,7 +121,7 @@ class ImmichAlbumProtectedPasswordText(
|
||||
ATTR_ALBUM_ID: self._album_data.id,
|
||||
}
|
||||
|
||||
protected_url = self.coordinator.get_album_protected_url(self._album_id)
|
||||
protected_url = self.coordinator.get_protected_url()
|
||||
if protected_url:
|
||||
attrs[ATTR_ALBUM_PROTECTED_URL] = protected_url
|
||||
|
||||
@@ -121,7 +129,7 @@ class ImmichAlbumProtectedPasswordText(
|
||||
|
||||
async def async_set_value(self, value: str) -> None:
|
||||
"""Set the password for the protected shared link."""
|
||||
link_id = self.coordinator.get_album_protected_link_id(self._album_id)
|
||||
link_id = self.coordinator.get_protected_link_id()
|
||||
if not link_id:
|
||||
_LOGGER.error(
|
||||
"Cannot set password: no protected link found for album %s",
|
||||
|
||||
Reference in New Issue
Block a user