From 8170fa846e70fa9420a5b5fd43ccce537ce12c1a Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Fri, 2 Oct 2026 07:24:50 -0700 Subject: [PATCH] Add current image_id to an attribute (#183958) Co-authored-by: Claude Opus 5.5 --- homeassistant/components/collection_image/image.py | 10 ++++++++-- tests/components/collection_image/test_image.py | 2 ++ tests/components/collection_image/test_services.py | 2 ++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/collection_image/image.py b/homeassistant/components/collection_image/image.py index 7ad03f5bb137..1dd60c7f4a2d 100644 --- a/homeassistant/components/collection_image/image.py +++ b/homeassistant/components/collection_image/image.py @@ -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.""" diff --git a/tests/components/collection_image/test_image.py b/tests/components/collection_image/test_image.py index 09bd6efb380a..5f0ebb5dea0d 100644 --- a/tests/components/collection_image/test_image.py +++ b/tests/components/collection_image/test_image.py @@ -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) diff --git a/tests/components/collection_image/test_services.py b/tests/components/collection_image/test_services.py index 152ec0ab9cbc..5b1652aad2b5 100644 --- a/tests/components/collection_image/test_services.py +++ b/tests/components/collection_image/test_services.py @@ -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