From 5ce54c2174fbf97ff152317fcba652bbc2311664 Mon Sep 17 00:00:00 2001 From: tronikos Date: Mon, 1 Jul 2024 08:48:12 -0700 Subject: [PATCH] Replace GoogleAPICallError with GoogleAPIError (#120902) --- .../google_generative_ai_conversation/__init__.py | 6 +++--- .../google_generative_ai_conversation/config_flow.py | 4 ++-- .../google_generative_ai_conversation/conversation.py | 4 ++-- .../google_generative_ai_conversation/test_conversation.py | 6 +++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/google_generative_ai_conversation/__init__.py b/homeassistant/components/google_generative_ai_conversation/__init__.py index f115f3923b6..a5c55c2099d 100644 --- a/homeassistant/components/google_generative_ai_conversation/__init__.py +++ b/homeassistant/components/google_generative_ai_conversation/__init__.py @@ -7,7 +7,7 @@ from pathlib import Path from google.ai import generativelanguage_v1beta from google.api_core.client_options import ClientOptions -from google.api_core.exceptions import ClientError, DeadlineExceeded, GoogleAPICallError +from google.api_core.exceptions import ClientError, DeadlineExceeded, GoogleAPIError import google.generativeai as genai import google.generativeai.types as genai_types import voluptuous as vol @@ -71,7 +71,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: try: response = await model.generate_content_async(prompt_parts) except ( - GoogleAPICallError, + GoogleAPIError, ValueError, genai_types.BlockedPromptException, genai_types.StopCandidateException, @@ -111,7 +111,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: await client.get_model( name=entry.options.get(CONF_CHAT_MODEL, RECOMMENDED_CHAT_MODEL), timeout=5.0 ) - except (GoogleAPICallError, ValueError) as err: + except (GoogleAPIError, ValueError) as err: if isinstance(err, ClientError) and err.reason == "API_KEY_INVALID": raise ConfigEntryAuthFailed(err) from err if isinstance(err, DeadlineExceeded): diff --git a/homeassistant/components/google_generative_ai_conversation/config_flow.py b/homeassistant/components/google_generative_ai_conversation/config_flow.py index 543deb926a0..ab23ac25f26 100644 --- a/homeassistant/components/google_generative_ai_conversation/config_flow.py +++ b/homeassistant/components/google_generative_ai_conversation/config_flow.py @@ -10,7 +10,7 @@ from typing import Any from google.ai import generativelanguage_v1beta from google.api_core.client_options import ClientOptions -from google.api_core.exceptions import ClientError, GoogleAPICallError +from google.api_core.exceptions import ClientError, GoogleAPIError import google.generativeai as genai import voluptuous as vol @@ -97,7 +97,7 @@ class GoogleGenerativeAIConfigFlow(ConfigFlow, domain=DOMAIN): if user_input is not None: try: await validate_input(self.hass, user_input) - except GoogleAPICallError as err: + except GoogleAPIError as err: if isinstance(err, ClientError) and err.reason == "API_KEY_INVALID": errors["base"] = "invalid_auth" else: diff --git a/homeassistant/components/google_generative_ai_conversation/conversation.py b/homeassistant/components/google_generative_ai_conversation/conversation.py index 8052ee66f40..127ca2cae95 100644 --- a/homeassistant/components/google_generative_ai_conversation/conversation.py +++ b/homeassistant/components/google_generative_ai_conversation/conversation.py @@ -6,7 +6,7 @@ import codecs from collections.abc import Callable from typing import Any, Literal -from google.api_core.exceptions import GoogleAPICallError +from google.api_core.exceptions import GoogleAPIError import google.generativeai as genai from google.generativeai import protos import google.generativeai.types as genai_types @@ -278,7 +278,7 @@ class GoogleGenerativeAIConversationEntity( try: chat_response = await chat.send_message_async(chat_request) except ( - GoogleAPICallError, + GoogleAPIError, ValueError, genai_types.BlockedPromptException, genai_types.StopCandidateException, diff --git a/tests/components/google_generative_ai_conversation/test_conversation.py b/tests/components/google_generative_ai_conversation/test_conversation.py index 30016335f3b..1e45c79a3b6 100644 --- a/tests/components/google_generative_ai_conversation/test_conversation.py +++ b/tests/components/google_generative_ai_conversation/test_conversation.py @@ -4,7 +4,7 @@ from unittest.mock import AsyncMock, MagicMock, patch from freezegun import freeze_time from google.ai.generativelanguage_v1beta.types.content import FunctionCall -from google.api_core.exceptions import GoogleAPICallError +from google.api_core.exceptions import GoogleAPIError import google.generativeai.types as genai_types import pytest from syrupy.assertion import SnapshotAssertion @@ -447,7 +447,7 @@ async def test_error_handling( with patch("google.generativeai.GenerativeModel") as mock_model: mock_chat = AsyncMock() mock_model.return_value.start_chat.return_value = mock_chat - mock_chat.send_message_async.side_effect = GoogleAPICallError("some error") + mock_chat.send_message_async.side_effect = GoogleAPIError("some error") result = await conversation.async_converse( hass, "hello", None, Context(), agent_id=mock_config_entry.entry_id ) @@ -455,7 +455,7 @@ async def test_error_handling( assert result.response.response_type == intent.IntentResponseType.ERROR, result assert result.response.error_code == "unknown", result assert result.response.as_dict()["speech"]["plain"]["speech"] == ( - "Sorry, I had a problem talking to Google Generative AI: None some error" + "Sorry, I had a problem talking to Google Generative AI: some error" )