Skip to content

Commit 8c731f0

Browse files
authored
feat: get metadata attributes and add to image entity (#4)
1 parent 5cfa738 commit 8c731f0

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

custom_components/immich/hub.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ async def get_my_user_info(self) -> dict:
6767
_LOGGER.error("Error connecting to the API: %s", exception)
6868
raise CannotConnect from exception
6969

70+
async def get_asset_info(self, asset_id: str) -> dict | None:
71+
"""Get asset info."""
72+
try:
73+
async with aiohttp.ClientSession() as session:
74+
url = urljoin(self.host, f"/api/assets/{asset_id}")
75+
headers = {"Accept": "application/json", _HEADER_API_KEY: self.api_key}
76+
77+
async with session.get(url=url, headers=headers) as response:
78+
if response.status != 200:
79+
raw_result = await response.text()
80+
_LOGGER.error("Error from API: body=%s", raw_result)
81+
raise ApiError()
82+
83+
asset_info: dict = await response.json()
84+
85+
return asset_info
86+
except aiohttp.ClientError as exception:
87+
_LOGGER.error("Error connecting to the API: %s", exception)
88+
raise CannotConnect from exception
89+
7090
async def download_asset(self, asset_id: str) -> bytes | None:
7191
"""Download the asset."""
7292
try:

custom_components/immich/image.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ def __init__(self, hass: HomeAssistant, hub: ImmichHub) -> None:
7575
self.hub = hub
7676
self.hass = hass
7777

78+
self._attr_extra_state_attributes = {}
79+
7880
async def async_update(self) -> None:
7981
"""Force a refresh of the image."""
8082
await self._load_and_cache_next_image()
@@ -128,8 +130,21 @@ async def _load_and_cache_next_image(self) -> None:
128130
await asyncio.sleep(1)
129131
continue
130132

133+
asset_info = await self.hub.get_asset_info(asset_id)
134+
135+
self._attr_extra_state_attributes["media_filename"] = (
136+
asset_info.get("originalFileName") or ""
137+
)
138+
self._attr_extra_state_attributes["media_exif"] = (
139+
asset_info.get("exifInfo") or ""
140+
)
141+
self._attr_extra_state_attributes["media_localdatetime"] = (
142+
asset_info.get("localDateTime") or ""
143+
)
144+
131145
self._current_image_bytes = asset_bytes
132146
self._attr_image_last_updated = datetime.now()
147+
self.async_write_ha_state()
133148

134149

135150
class ImmichImageFavorite(BaseImmichImage):

0 commit comments

Comments
 (0)