Skip to content

Commit

Permalink
Adding GroqCloud API
Browse files Browse the repository at this point in the history
  • Loading branch information
fabio-garavini committed Aug 30, 2024
1 parent 6d90b4a commit c03f40c
Show file tree
Hide file tree
Showing 8 changed files with 391 additions and 103 deletions.
50 changes: 37 additions & 13 deletions custom_components/openai_whisper_cloud/__init__.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
"""OpenAI Whisper Cloud integration."""

import logging

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, Platform
from homeassistant.const import (
CONF_API_KEY,
CONF_MODEL,
CONF_NAME,
CONF_SOURCE,
Platform,
)
from homeassistant.core import HomeAssistant

from .const import DEFAULT_NAME
from .const import _LOGGER, CONF_PROMPT, CONF_TEMPERATURE

PLATFORMS = [Platform.STT]

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Load entry."""

_LOGGER.info("Setting up %s", entry)
_LOGGER.info("Setting up %s", entry.entry_id)

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

entry.async_on_unload(entry.add_update_listener(async_update_listener))

return True


async def async_update_listener(hass: HomeAssistant, entry: ConfigEntry):
"""Update entry."""
await hass.config_entries.async_reload(entry.entry_id)
Expand All @@ -34,26 +37,47 @@ async def async_update_listener(hass: HomeAssistant, entry: ConfigEntry):
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""

_LOGGER.info("Unloading %s", entry)
_LOGGER.info("Unloading %s", entry.entry_id)

return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)


async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry):
"""Migrate old entry."""
_LOGGER.debug("Migrating configuration from version %s.%s", config_entry.version, config_entry.minor_version)
_LOGGER.info(
f"Migration of {config_entry.entry_id} from version: {config_entry.version}.{config_entry.minor_version}"
)

if config_entry.version > 1:
# This means the user has downgraded from a future version
return False

if config_entry.version == 0:

new_data = {**config_entry.data}

new_data[CONF_NAME] = DEFAULT_NAME
new_data[CONF_NAME] = "OpenAI Whisper"

hass.config_entries.async_update_entry(
config_entry, data=new_data, minor_version=0, version=1
)

if config_entry.version == 1 and config_entry.minor_version == 0:
new_data = {
CONF_SOURCE: 0,
CONF_NAME: config_entry.data[CONF_NAME],
CONF_API_KEY: config_entry.data[CONF_API_KEY],
}

new_options = {
CONF_MODEL: 0,
CONF_TEMPERATURE: config_entry.data[CONF_TEMPERATURE],
CONF_PROMPT: config_entry.data[CONF_PROMPT],
}

hass.config_entries.async_update_entry(config_entry, data=new_data, minor_version=0, version=1)
hass.config_entries.async_update_entry(
config_entry, data=new_data, options=new_options, minor_version=1, version=1
)

_LOGGER.debug("Migration to configuration version %s.%s successful", config_entry.version, config_entry.minor_version)
_LOGGER.info(f"Migration of {config_entry.entry_id} successful")

return True
Loading

0 comments on commit c03f40c

Please sign in to comment.