From 1bb1dbc1ae2b605f068944b7097efec76f95337b Mon Sep 17 00:00:00 2001 From: Hunor Laczko Date: Sun, 25 Aug 2024 20:01:05 +0200 Subject: [PATCH] possible fix --- custom_components/groq_cloud_api/__init__.py | 14 ++--- .../groq_cloud_api/config_flow.py | 61 ++++++++++++++++--- .../groq_cloud_api/manifest.json | 2 +- 3 files changed, 62 insertions(+), 15 deletions(-) diff --git a/custom_components/groq_cloud_api/__init__.py b/custom_components/groq_cloud_api/__init__.py index fd49828..3c92754 100644 --- a/custom_components/groq_cloud_api/__init__.py +++ b/custom_components/groq_cloud_api/__init__.py @@ -20,13 +20,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: GroqConfigEntry) -> bool LOGGER.info("Setting up %s", entry) client = groq.AsyncGroq(api_key=entry.data[CONF_API_KEY]) - try: - await hass.async_add_executor_job(client.with_options(timeout=10.0).models.list) - except groq.AuthenticationError as err: - LOGGER.error("Invalid API key: %s", err) - return False - except groq.GroqError as err: - raise ConfigEntryNotReady(err) from err + # try: + # await hass.async_add_executor_job(client.with_options(timeout=10.0).models.list) + # except groq.AuthenticationError as err: + # LOGGER.error("Invalid API key: %s", err) + # return False + # except groq.GroqError as err: + # raise ConfigEntryNotReady(err) from err entry.runtime_data = client diff --git a/custom_components/groq_cloud_api/config_flow.py b/custom_components/groq_cloud_api/config_flow.py index 0c27853..8b3304a 100644 --- a/custom_components/groq_cloud_api/config_flow.py +++ b/custom_components/groq_cloud_api/config_flow.py @@ -2,11 +2,13 @@ from __future__ import annotations +import asyncio from typing import Any from types import MappingProxyType import groq +import requests import voluptuous as vol from homeassistant.core import HomeAssistant @@ -16,7 +18,8 @@ from homeassistant.config_entries import ( ConfigFlowResult, OptionsFlow, ) -from homeassistant.const import CONF_API_KEY, CONF_NAME, CONF_LLM_HASS_API +from homeassistant import exceptions +from homeassistant.const import CONF_API_KEY, CONF_NAME, CONF_LLM_HASS_API, CONF_MODEL from homeassistant.helpers import llm import homeassistant.helpers.config_validation as cv from homeassistant.helpers.selector import ( @@ -61,12 +64,40 @@ RECOMMENDED_OPTIONS = { async def validate_input(hass: HomeAssistant, data: dict[str, Any]): - """Validate the user input allows us to connect. + """Validate the user input.""" - Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user. - """ - client = groq.AsyncGroq(api_key=data[CONF_API_KEY]) - await hass.async_add_executor_job(client.with_options(timeout=10.0).models.list) + obscured_api_key = data.get(CONF_API_KEY) + data[CONF_API_KEY] = "" + LOGGER.debug("User validation got: %s", data) + data[CONF_API_KEY] = obscured_api_key + + response = await asyncio.to_thread( + requests.get, + url="https://api.groq.com/openai/v1/models", + headers={ + "Authorization": f"Bearer {data.get(CONF_API_KEY)}", + "Content-Type": "application/json" + }, + ) + + LOGGER.debug("Models request took %f s and returned %d - %s", response.elapsed.seconds, response.status_code, response.reason) + + if response.status_code == 401: + raise InvalidAPIKey + + if response.status_code == 403: + raise UnauthorizedError + + if response.status_code != 200: + raise UnknownError + + for model in response.json().get("data", []): + if model.get("id") == data.get(CONF_MODEL): + break + if model == response.json().get("data")[-1]: + raise ModelNotFound + + LOGGER.debug("User validation successful") class GroqConfigFlow(ConfigFlow, domain=DOMAIN): @@ -221,4 +252,20 @@ def groq_config_option_schema( ): NumberSelector(NumberSelectorConfig(min=0, max=2, step=0.05)), } ) - return schema \ No newline at end of file + return schema + + +class UnknownError(exceptions.HomeAssistantError): + """Unknown error.""" + + +class UnauthorizedError(exceptions.HomeAssistantError): + """API key valid but doesn't have the rights to use Whisper.""" + + +class InvalidAPIKey(exceptions.HomeAssistantError): + """Invalid api_key error.""" + + +class ModelNotFound(exceptions.HomeAssistantError): + """Model can't be found in the GroqCloud model's list.""" \ No newline at end of file diff --git a/custom_components/groq_cloud_api/manifest.json b/custom_components/groq_cloud_api/manifest.json index 65536c2..754f80b 100644 --- a/custom_components/groq_cloud_api/manifest.json +++ b/custom_components/groq_cloud_api/manifest.json @@ -1,7 +1,7 @@ { "domain": "groq_cloud_api", "name": "Groq Cloud API", - "version": "0.0.4", + "version": "0.0.5", "codeowners": [ "@HunorLaczko" ],