Add current image_id to an attribute (#183958)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
karwosts
2026-10-02 07:24:50 -07:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 4466779efd
commit 8170fa846e
3 changed files with 12 additions and 2 deletions
@@ -213,6 +213,7 @@ class CollectionImageImageEntity(ImageEntity):
try:
resolved = await async_resolve_media(self.hass, image_id, self.entity_id)
except Unresolvable as err:
self._current_image_id = image_id
self._clear_image()
raise HomeAssistantError(
translation_domain=DOMAIN,
@@ -222,8 +223,7 @@ class CollectionImageImageEntity(ImageEntity):
"id": image_id,
},
) from err
finally:
self._current_image_id = image_id
self._current_image_id = image_id
try:
valid_image_content_type(resolved.mime_type)
@@ -250,6 +250,12 @@ class CollectionImageImageEntity(ImageEntity):
self._attr_image_last_updated = dt_util.utcnow()
self.async_write_ha_state()
@property
@override
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes."""
return {"current_media_id": self._current_image_id}
@override
async def async_added_to_hass(self) -> None:
"""Initialize the first image after entity has been created."""
@@ -73,6 +73,7 @@ async def test_image(
state = hass.states.get(DEFAULT_ENTITY_ID)
assert state and state.state == TEST_TIME
assert state.attributes["current_media_id"] == MOCK_MEDIA_IMAGE_URI_1
await _verify_path_image(hass, hass_client)
@@ -300,6 +301,7 @@ async def test_unresolvable(
state = hass.states.get(DEFAULT_ENTITY_ID)
assert state and state.state == STATE_UNKNOWN
assert state.attributes["current_media_id"] == MOCK_MEDIA_IMAGE_URI_1
await hass.async_block_till_done(wait_background_tasks=True)
@@ -254,6 +254,7 @@ async def test_select_image(
assert args[1] == MOCK_MEDIA_IMAGE_URI_2
state = hass.states.get(DEFAULT_ENTITY_ID)
assert state and state.state not in (STATE_UNAVAILABLE, STATE_UNKNOWN)
assert state.attributes["current_media_id"] == MOCK_MEDIA_IMAGE_URI_2
async def test_select_image_invalid_media_type(
@@ -287,3 +288,4 @@ async def test_select_image_invalid_media_type(
assert args[1] == MOCK_MEDIA_IMAGE_URI_2
state = hass.states.get(DEFAULT_ENTITY_ID)
assert state and state.state == STATE_UNKNOWN
assert state.attributes["current_media_id"] == MOCK_MEDIA_IMAGE_URI_2