Files
ha-text-ai/custom_components/ha_text_ai/__init__.py
T

90 lines
3.0 KiB
Python
Raw Normal View History

2024-11-19 14:35:45 +03:00
"""The HA Text AI integration."""
2024-11-18 00:43:28 +03:00
import logging
from typing import Any
2024-11-14 18:32:41 +03:00
2024-11-18 00:43:28 +03:00
from homeassistant.config_entries import ConfigEntry
2024-11-19 12:45:26 +03:00
from homeassistant.const import CONF_API_KEY
2024-11-19 14:35:45 +03:00
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import aiohttp_client
2024-11-14 18:32:41 +03:00
2024-11-19 14:35:45 +03:00
from .const import DOMAIN, PLATFORMS
from .coordinator import HATextAICoordinator
2024-11-14 18:32:41 +03:00
2024-11-18 00:43:28 +03:00
_LOGGER = logging.getLogger(__name__)
2024-11-14 18:32:41 +03:00
2024-11-18 11:35:23 +03:00
async def async_setup(hass: HomeAssistant, config: dict[str, Any]) -> bool:
2024-11-19 14:35:45 +03:00
"""Set up the HA Text AI component."""
2024-11-18 00:43:28 +03:00
hass.data.setdefault(DOMAIN, {})
return True
2024-11-18 11:35:23 +03:00
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
2024-11-19 14:35:45 +03:00
"""Set up HA Text AI from a config entry."""
2024-11-18 11:35:23 +03:00
try:
2024-11-19 14:35:45 +03:00
session = aiohttp_client.async_get_clientsession(hass)
2024-11-18 11:35:23 +03:00
coordinator = HATextAICoordinator(
hass,
api_key=entry.data[CONF_API_KEY],
2024-11-19 14:35:45 +03:00
endpoint=entry.data.get("api_endpoint", "https://api.openai.com/v1"),
model=entry.data.get("model", "gpt-3.5-turbo"),
temperature=entry.data.get("temperature", 0.7),
max_tokens=entry.data.get("max_tokens", 1000),
request_interval=entry.data.get("request_interval", 1.0),
session=session,
2024-11-18 11:35:23 +03:00
)
2024-11-19 14:35:45 +03:00
try:
await coordinator.async_config_entry_first_refresh()
except Exception as refresh_ex:
_LOGGER.error("Failed to refresh coordinator: %s", str(refresh_ex))
return False
if not coordinator.last_update_success:
_LOGGER.error("Failed to communicate with OpenAI API")
return False
2024-11-18 11:35:23 +03:00
hass.data[DOMAIN][entry.entry_id] = coordinator
2024-11-19 14:35:45 +03:00
try:
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
except Exception as setup_ex:
_LOGGER.error("Failed to setup platforms: %s", str(setup_ex))
return False
_LOGGER.info(
"Successfully set up HA Text AI with model: %s",
entry.data.get("model", "gpt-3.5-turbo")
)
return True
2024-11-18 11:35:23 +03:00
except Exception as ex:
2024-11-19 14:35:45 +03:00
_LOGGER.exception("Unexpected error setting up entry: %s", str(ex))
return False
2024-11-18 11:35:23 +03:00
2024-11-18 00:43:28 +03:00
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
2024-11-19 14:35:45 +03:00
try:
if entry.entry_id not in hass.data.get(DOMAIN, {}):
return True
2024-11-19 14:00:33 +03:00
2024-11-19 14:35:45 +03:00
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
coordinator = hass.data[DOMAIN].pop(entry.entry_id)
await coordinator.async_shutdown()
2024-11-18 00:43:28 +03:00
2024-11-19 14:35:45 +03:00
return unload_ok
2024-11-18 11:35:23 +03:00
2024-11-19 14:35:45 +03:00
except Exception as ex:
_LOGGER.exception("Error unloading entry: %s", str(ex))
return False
async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Reload config entry."""
try:
await async_unload_entry(hass, entry)
await async_setup_entry(hass, entry)
except Exception as ex:
_LOGGER.exception("Error reloading entry: %s", str(ex))