Public Access
Refactor Groq Cloud API integration: update setup methods, enhance error handling, and improve configuration options
This commit is contained in:
@@ -1,58 +1,55 @@
|
||||
"""Groq Cloud API integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import groq
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_API_KEY, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.httpx_client import get_async_client
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from .const import LOGGER
|
||||
from .const import DOMAIN, LOGGER
|
||||
|
||||
PLATFORMS = (Platform.CONVERSATION,)
|
||||
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
|
||||
|
||||
type GroqConfigEntry = ConfigEntry[groq.AsyncClient]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: GroqConfigEntry) -> bool:
|
||||
"""Load entry."""
|
||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
"""Set up Groq Cloud API."""
|
||||
return True
|
||||
|
||||
LOGGER.info("Setting up %s", entry)
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: GroqConfigEntry) -> bool:
|
||||
"""Set up Groq Cloud API from a config entry."""
|
||||
LOGGER.debug("Setting up %s", entry)
|
||||
|
||||
client = groq.AsyncGroq(
|
||||
api_key=entry.data[CONF_API_KEY],
|
||||
http_client=get_async_client(hass)
|
||||
http_client=get_async_client(hass),
|
||||
)
|
||||
|
||||
# TODO: I get async errors when trying to call the list function. But this would be the proper way to validate the 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
|
||||
|
||||
entry.runtime_data = client
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
entry.async_on_unload(entry.add_update_listener(async_update_listener))
|
||||
entry.async_on_unload(entry.add_update_listener(async_update_options))
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_update_listener(hass: HomeAssistant, entry: ConfigEntry):
|
||||
"""Update entry."""
|
||||
async def async_update_options(hass: HomeAssistant, entry: GroqConfigEntry) -> None:
|
||||
"""Update options."""
|
||||
await hass.config_entries.async_reload(entry.entry_id)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: GroqConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
|
||||
LOGGER.info("Unloading %s", entry)
|
||||
LOGGER.debug("Unloading %s", entry)
|
||||
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
||||
Reference in New Issue
Block a user