From ed1a70fe0375c50ba0a6a9b0f774cf98c9184e01 Mon Sep 17 00:00:00 2001 From: Hamish Date: Fri, 2 Oct 2026 17:50:36 +0930 Subject: [PATCH] indi_allsky: Simplify and fix keogram and startrail fetching (#183987) --- homeassistant/components/indi_allsky/image.py | 21 +++++++++---------- tests/components/indi_allsky/test_image.py | 8 +++---- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/indi_allsky/image.py b/homeassistant/components/indi_allsky/image.py index a713bcd42d46..3b22a84c2ad3 100644 --- a/homeassistant/components/indi_allsky/image.py +++ b/homeassistant/components/indi_allsky/image.py @@ -31,7 +31,7 @@ class IndiAllSkyImageEntityDescription(ImageEntityDescription): """Class describing INDI Allsky image entities.""" media_fn: Callable[[IndiAllSkyData], MediaData | None] - fallback_filename: str + image_filename: str IMAGE_DESCRIPTIONS: tuple[IndiAllSkyImageEntityDescription, ...] = ( @@ -39,13 +39,13 @@ IMAGE_DESCRIPTIONS: tuple[IndiAllSkyImageEntityDescription, ...] = ( key="latest_keogram", translation_key="latest_keogram", media_fn=lambda data: data.latest_keogram, - fallback_filename="latestkeogram", + image_filename="latestkeogram", ), IndiAllSkyImageEntityDescription( key="latest_startrail", translation_key="latest_startrail", media_fn=lambda data: data.latest_startrail, - fallback_filename="lateststartrail", + image_filename="lateststartrail", ), ) @@ -97,20 +97,19 @@ class IndiAllSkyImageEntity(IndiAllSkyEntity, ImageEntity): @override async def async_image(self) -> bytes | None: """Return bytes of the image.""" - media = self.entity_description.media_fn(self.coordinator.data) - filename = ( - media.filename - if (media and media.filename) - else self.entity_description.fallback_filename - ) try: - image_bytes = await self.coordinator.client.fetch_image(filename) + image_bytes = await self.coordinator.client.fetch_image( + self.entity_description.image_filename + ) except IndiAllSkyError: return None else: if content_type := infer_image_type(image_bytes): self._attr_content_type = content_type - if media is None and self._last_fetched is None: + if ( + self.entity_description.media_fn(self.coordinator.data) is None + and self._last_fetched is None + ): self._last_fetched = dt_util.utcnow() self.async_write_ha_state() return image_bytes diff --git a/tests/components/indi_allsky/test_image.py b/tests/components/indi_allsky/test_image.py index ca0b549db16e..803df5c35d5c 100644 --- a/tests/components/indi_allsky/test_image.py +++ b/tests/components/indi_allsky/test_image.py @@ -86,7 +86,7 @@ async def test_image_events_and_fetching( mock_indi_allsky_client.fetch_image.return_value = b"\xff\xd8\xff\xe0keogram_bytes" img = await image.async_get_image(hass, "image.indi_allsky_latest_keogram") assert img.content == b"\xff\xd8\xff\xe0keogram_bytes" - mock_indi_allsky_client.fetch_image.assert_called_with("keogram_20260813.jpg") + mock_indi_allsky_client.fetch_image.assert_called_with("latestkeogram") state = hass.states.get("image.indi_allsky_latest_star_trail") assert state is not None @@ -97,7 +97,7 @@ async def test_image_events_and_fetching( ) img = await image.async_get_image(hass, "image.indi_allsky_latest_star_trail") assert img.content == b"\xff\xd8\xff\xe0startrail_bytes" - mock_indi_allsky_client.fetch_image.assert_called_with("startrail_20260813.jpg") + mock_indi_allsky_client.fetch_image.assert_called_with("lateststartrail") async def test_image_fetch_error( @@ -120,12 +120,12 @@ async def test_image_fetch_error( await image.async_get_image(hass, "image.indi_allsky_latest_keogram") -async def test_image_fallback_fetching_before_events( +async def test_image_fetching_before_events( hass: HomeAssistant, mock_indi_allsky_client: AsyncMock, mock_config_entry: MockConfigEntry, ) -> None: - """Test fetching fallback alias images before any media event arrives.""" + """Test fetching images before any media event arrives.""" with patch("homeassistant.components.indi_allsky._PLATFORMS", [Platform.IMAGE]): await setup_integration(hass, mock_config_entry)