mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Ensure file is correctly uploaded by the GenAI SDK (#140969)
Opened the file outside of the SDK
This commit is contained in:
parent
a600bc5e57
commit
d9cf2750d5
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import mimetypes
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from google import genai # type: ignore[attr-defined]
|
from google import genai # type: ignore[attr-defined]
|
||||||
@ -83,7 +84,12 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
)
|
)
|
||||||
if not Path(filename).exists():
|
if not Path(filename).exists():
|
||||||
raise HomeAssistantError(f"`{filename}` does not exist")
|
raise HomeAssistantError(f"`{filename}` does not exist")
|
||||||
prompt_parts.append(client.files.upload(file=filename))
|
mimetype = mimetypes.guess_type(filename)[0]
|
||||||
|
with open(filename, "rb") as file:
|
||||||
|
uploaded_file = client.files.upload(
|
||||||
|
file=file, config={"mime_type": mimetype}
|
||||||
|
)
|
||||||
|
prompt_parts.append(uploaded_file)
|
||||||
|
|
||||||
await hass.async_add_executor_job(append_files_to_prompt)
|
await hass.async_add_executor_job(append_files_to_prompt)
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
"""Tests for the Google Generative AI Conversation integration."""
|
"""Tests for the Google Generative AI Conversation integration."""
|
||||||
|
|
||||||
from unittest.mock import AsyncMock, Mock, patch
|
from unittest.mock import AsyncMock, Mock, mock_open, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from requests.exceptions import Timeout
|
from requests.exceptions import Timeout
|
||||||
@ -71,6 +71,8 @@ async def test_generate_content_service_with_image(
|
|||||||
),
|
),
|
||||||
patch("pathlib.Path.exists", return_value=True),
|
patch("pathlib.Path.exists", return_value=True),
|
||||||
patch.object(hass.config, "is_allowed_path", return_value=True),
|
patch.object(hass.config, "is_allowed_path", return_value=True),
|
||||||
|
patch("builtins.open", mock_open(read_data="this is an image")),
|
||||||
|
patch("mimetypes.guess_type", return_value=["image/jpeg"]),
|
||||||
):
|
):
|
||||||
response = await hass.services.async_call(
|
response = await hass.services.async_call(
|
||||||
"google_generative_ai_conversation",
|
"google_generative_ai_conversation",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user