mirror of
https://github.com/home-assistant/core.git
synced 2026-10-06 06:15:47 -04:00
indi_allsky: Simplify and fix keogram and startrail fetching (#183987)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user