Add service to OpenAI to Generate an image (#97018)
Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
parent
5caa1969c5
commit
aad281db18
5 changed files with 176 additions and 8 deletions
|
@ -2,10 +2,12 @@
|
|||
from unittest.mock import patch
|
||||
|
||||
from openai import error
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components import conversation
|
||||
from homeassistant.core import Context, HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import area_registry as ar, device_registry as dr, intent
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
@ -158,3 +160,69 @@ async def test_conversation_agent(
|
|||
mock_config_entry.entry_id
|
||||
)
|
||||
assert agent.supported_languages == "*"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("service_data", "expected_args"),
|
||||
[
|
||||
(
|
||||
{"prompt": "Picture of a dog"},
|
||||
{"prompt": "Picture of a dog", "size": "512x512"},
|
||||
),
|
||||
(
|
||||
{"prompt": "Picture of a dog", "size": "256"},
|
||||
{"prompt": "Picture of a dog", "size": "256x256"},
|
||||
),
|
||||
(
|
||||
{"prompt": "Picture of a dog", "size": "1024"},
|
||||
{"prompt": "Picture of a dog", "size": "1024x1024"},
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_generate_image_service(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_init_component,
|
||||
service_data,
|
||||
expected_args,
|
||||
) -> None:
|
||||
"""Test generate image service."""
|
||||
service_data["config_entry"] = mock_config_entry.entry_id
|
||||
expected_args["api_key"] = mock_config_entry.data["api_key"]
|
||||
expected_args["n"] = 1
|
||||
|
||||
with patch(
|
||||
"openai.Image.acreate", return_value={"data": [{"url": "A"}]}
|
||||
) as mock_create:
|
||||
response = await hass.services.async_call(
|
||||
"openai_conversation",
|
||||
"generate_image",
|
||||
service_data,
|
||||
blocking=True,
|
||||
return_response=True,
|
||||
)
|
||||
|
||||
assert response == {"url": "A"}
|
||||
assert len(mock_create.mock_calls) == 1
|
||||
assert mock_create.mock_calls[0][2] == expected_args
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_init_component")
|
||||
async def test_generate_image_service_error(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test generate image service handles errors."""
|
||||
with patch(
|
||||
"openai.Image.acreate", side_effect=error.ServiceUnavailableError("Reason")
|
||||
), pytest.raises(HomeAssistantError, match="Error generating image: Reason"):
|
||||
await hass.services.async_call(
|
||||
"openai_conversation",
|
||||
"generate_image",
|
||||
{
|
||||
"config_entry": mock_config_entry.entry_id,
|
||||
"prompt": "Image of an epic fail",
|
||||
},
|
||||
blocking=True,
|
||||
return_response=True,
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue