Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Ollama integration."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 import logging
7 
8 import httpx
9 import ollama
10 
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import CONF_URL, Platform
13 from homeassistant.core import HomeAssistant
14 from homeassistant.exceptions import ConfigEntryNotReady
15 from homeassistant.helpers import config_validation as cv
16 from homeassistant.util.ssl import get_default_context
17 
18 from .const import (
19  CONF_KEEP_ALIVE,
20  CONF_MAX_HISTORY,
21  CONF_MODEL,
22  CONF_NUM_CTX,
23  CONF_PROMPT,
24  DEFAULT_TIMEOUT,
25  DOMAIN,
26 )
27 
28 _LOGGER = logging.getLogger(__name__)
29 
30 __all__ = [
31  "CONF_URL",
32  "CONF_PROMPT",
33  "CONF_MODEL",
34  "CONF_MAX_HISTORY",
35  "CONF_NUM_CTX",
36  "CONF_KEEP_ALIVE",
37  "DOMAIN",
38 ]
39 
40 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
41 PLATFORMS = (Platform.CONVERSATION,)
42 
43 
44 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
45  """Set up Ollama from a config entry."""
46  settings = {**entry.data, **entry.options}
47  client = ollama.AsyncClient(host=settings[CONF_URL], verify=get_default_context())
48  try:
49  async with asyncio.timeout(DEFAULT_TIMEOUT):
50  await client.list()
51  except (TimeoutError, httpx.ConnectError) as err:
52  raise ConfigEntryNotReady(err) from err
53 
54  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = client
55 
56  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
57  return True
58 
59 
60 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
61  """Unload Ollama."""
62  if not await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
63  return False
64  hass.data[DOMAIN].pop(entry.entry_id)
65  return True
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:60
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:44
ssl.SSLContext get_default_context()
Definition: ssl.py:118