Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Anthropic integration."""
2 
3 from __future__ import annotations
4 
5 import anthropic
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import CONF_API_KEY, Platform
9 from homeassistant.core import HomeAssistant
10 from homeassistant.exceptions import ConfigEntryNotReady
11 from homeassistant.helpers import config_validation as cv
12 
13 from .const import DOMAIN, LOGGER
14 
15 PLATFORMS = (Platform.CONVERSATION,)
16 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
17 
18 type AnthropicConfigEntry = ConfigEntry[anthropic.AsyncClient]
19 
20 
21 async def async_setup_entry(hass: HomeAssistant, entry: AnthropicConfigEntry) -> bool:
22  """Set up Anthropic from a config entry."""
23  client = anthropic.AsyncAnthropic(api_key=entry.data[CONF_API_KEY])
24  try:
25  await client.messages.create(
26  model="claude-3-haiku-20240307",
27  max_tokens=1,
28  messages=[{"role": "user", "content": "Hi"}],
29  timeout=10.0,
30  )
31  except anthropic.AuthenticationError as err:
32  LOGGER.error("Invalid API key: %s", err)
33  return False
34  except anthropic.AnthropicError as err:
35  raise ConfigEntryNotReady(err) from err
36 
37  entry.runtime_data = client
38 
39  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
40 
41  return True
42 
43 
44 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
45  """Unload Anthropic."""
46  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, AnthropicConfigEntry entry)
Definition: __init__.py:21
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:44