Add async_get_image helper to Image integration (#152465)

This commit is contained in:
Paulus Schoutsen
2025-09-17 04:35:55 -04:00
committed by GitHub
parent 04c0bb20d6
commit b10a9721a7
4 changed files with 66 additions and 16 deletions

View File

@@ -187,7 +187,11 @@ async def test_generate_data_mixed_attachments(
patch(
"homeassistant.components.camera.async_get_image",
return_value=Image(content_type="image/jpeg", content=b"fake_camera_jpeg"),
) as mock_get_image,
) as mock_get_camera_image,
patch(
"homeassistant.components.image.async_get_image",
return_value=Image(content_type="image/jpeg", content=b"fake_image_jpeg"),
) as mock_get_image_image,
patch(
"homeassistant.components.media_source.async_resolve_media",
return_value=media_source.PlayMedia(
@@ -207,6 +211,10 @@ async def test_generate_data_mixed_attachments(
"media_content_id": "media-source://camera/camera.front_door",
"media_content_type": "image/jpeg",
},
{
"media_content_id": "media-source://image/image.floorplan",
"media_content_type": "image/jpeg",
},
{
"media_content_id": "media-source://media_player/video.mp4",
"media_content_type": "video/mp4",
@@ -215,7 +223,8 @@ async def test_generate_data_mixed_attachments(
)
# Verify both methods were called
mock_get_image.assert_called_once_with(hass, "camera.front_door")
mock_get_camera_image.assert_called_once_with(hass, "camera.front_door")
mock_get_image_image.assert_called_once_with(hass, "image.floorplan")
mock_resolve_media.assert_called_once_with(
hass, "media-source://media_player/video.mp4", None
)
@@ -224,7 +233,7 @@ async def test_generate_data_mixed_attachments(
assert len(mock_ai_task_entity.mock_generate_data_tasks) == 1
task = mock_ai_task_entity.mock_generate_data_tasks[0]
assert task.attachments is not None
assert len(task.attachments) == 2
assert len(task.attachments) == 3
# Check camera attachment
camera_attachment = task.attachments[0]
@@ -240,6 +249,18 @@ async def test_generate_data_mixed_attachments(
content = await hass.async_add_executor_job(camera_attachment.path.read_bytes)
assert content == b"fake_camera_jpeg"
# Check image attachment
image_attachment = task.attachments[1]
assert image_attachment.media_content_id == "media-source://image/image.floorplan"
assert image_attachment.mime_type == "image/jpeg"
assert isinstance(image_attachment.path, Path)
assert image_attachment.path.suffix == ".jpg"
# Verify image snapshot content
assert image_attachment.path.exists()
content = await hass.async_add_executor_job(image_attachment.path.read_bytes)
assert content == b"fake_image_jpeg"
# Trigger clean up
async_fire_time_changed(
hass,
@@ -249,9 +270,10 @@ async def test_generate_data_mixed_attachments(
# Verify the temporary file cleaned up
assert not camera_attachment.path.exists()
assert not image_attachment.path.exists()
# Check regular media attachment
media_attachment = task.attachments[1]
media_attachment = task.attachments[2]
assert media_attachment.media_content_id == "media-source://media_player/video.mp4"
assert media_attachment.mime_type == "video/mp4"
assert media_attachment.path == Path("/media/test.mp4")