diff --git a/custom_components/ha_text_ai/__init__.py b/custom_components/ha_text_ai/__init__.py index 284fe14..a02649e 100644 --- a/custom_components/ha_text_ai/__init__.py +++ b/custom_components/ha_text_ai/__init__.py @@ -9,7 +9,6 @@ The HA Text AI integration. from __future__ import annotations import logging -from datetime import datetime from typing import Any, Dict import asyncio @@ -22,6 +21,7 @@ from homeassistant.core import HomeAssistant, ServiceCall, SupportsResponse from homeassistant.exceptions import ConfigEntryNotReady, HomeAssistantError from homeassistant.helpers import config_validation as cv from homeassistant.helpers import aiohttp_client +from homeassistant.util import dt as dt_util from .coordinator import HATextAICoordinator from .api_client import APIClient @@ -140,7 +140,7 @@ async def async_setup(hass: HomeAssistant, config: Dict[str, Any]) -> bool: "model_used": call.data.get("model", ""), "instance": call.data["instance"], "question": call.data["question"], - "timestamp": datetime.now().isoformat(), + "timestamp": dt_util.utcnow().isoformat(), "success": False, "error": "Service call failed" } @@ -266,7 +266,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: endpoint = validate_endpoint(raw_endpoint) except ValueError as err: _LOGGER.error("Invalid API endpoint %s: %s", raw_endpoint, err) - raise ConfigEntryNotReady(f"Invalid API endpoint: {err}") + raise ConfigEntryNotReady(f"Invalid API endpoint: {err}") from err # API key can now be updated via options api_key = config.get(CONF_API_KEY, entry.data.get(CONF_API_KEY)) instance_name = entry.data.get(CONF_NAME, entry.entry_id) diff --git a/custom_components/ha_text_ai/api_client.py b/custom_components/ha_text_ai/api_client.py index b1a4c22..a0931c7 100644 --- a/custom_components/ha_text_ai/api_client.py +++ b/custom_components/ha_text_ai/api_client.py @@ -138,6 +138,8 @@ class APIClient: raise await asyncio.sleep(2 ** attempt) + raise HomeAssistantError("API request failed after all retries") + async def create( self, model: str, diff --git a/custom_components/ha_text_ai/config_flow.py b/custom_components/ha_text_ai/config_flow.py index 9042680..1ff6916 100644 --- a/custom_components/ha_text_ai/config_flow.py +++ b/custom_components/ha_text_ai/config_flow.py @@ -306,7 +306,7 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): vol.Required(CONF_API_ENDPOINT, default=input_copy.get(CONF_API_ENDPOINT, get_default_endpoint(self._provider))): str, # Other fields remain the same }), - errors={"base": str(e)} + errors={"base": "unknown"} ) # All validation passed, create the entry @@ -378,7 +378,7 @@ class HATextAIConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): if response.status == 401: self._errors["base"] = "invalid_auth" return False - elif response.status not in [200, 404]: + elif response.status != 200: self._errors["base"] = "cannot_connect" return False return True @@ -514,7 +514,7 @@ class OptionsFlowHandler(config_entries.OptionsFlow): if response.status == 401: self._errors["base"] = "invalid_auth" return False - elif response.status not in [200, 404]: + elif response.status != 200: self._errors["base"] = "cannot_connect" return False return True diff --git a/custom_components/ha_text_ai/const.py b/custom_components/ha_text_ai/const.py index bc02155..73597d9 100644 --- a/custom_components/ha_text_ai/const.py +++ b/custom_components/ha_text_ai/const.py @@ -9,7 +9,7 @@ Constants for the HA text AI integration. import os import json from typing import Final -from homeassistant.const import Platform, CONF_API_KEY, CONF_NAME +from homeassistant.const import Platform import logging _LOGGER = logging.getLogger(__name__) diff --git a/custom_components/ha_text_ai/manifest.json b/custom_components/ha_text_ai/manifest.json index 0ead9ea..c800da7 100644 --- a/custom_components/ha_text_ai/manifest.json +++ b/custom_components/ha_text_ai/manifest.json @@ -12,7 +12,8 @@ "loggers": ["custom_components.ha_text_ai"], "requirements": [ "aiofiles>=23.0.0", - "aiohttp>=3.8.0" + "aiohttp>=3.8.0", + "google-genai>=1.16.0" ], "single_config_entry": false, "version": "2.4.0"