1 """Config flow for Anthropic integration."""
3 from __future__
import annotations
6 from types
import MappingProxyType
10 import voluptuous
as vol
37 RECOMMENDED_CHAT_MODEL,
38 RECOMMENDED_MAX_TOKENS,
39 RECOMMENDED_TEMPERATURE,
42 _LOGGER = logging.getLogger(__name__)
44 STEP_USER_DATA_SCHEMA = vol.Schema(
46 vol.Required(CONF_API_KEY): str,
50 RECOMMENDED_OPTIONS = {
51 CONF_RECOMMENDED:
True,
52 CONF_LLM_HASS_API: llm.LLM_API_ASSIST,
53 CONF_PROMPT: llm.DEFAULT_INSTRUCTIONS_PROMPT,
58 """Validate the user input allows us to connect.
60 Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
62 client = anthropic.AsyncAnthropic(api_key=data[CONF_API_KEY])
63 await client.messages.create(
64 model=
"claude-3-haiku-20240307",
66 messages=[{
"role":
"user",
"content":
"Hi"}],
72 """Handle a config flow for Anthropic."""
77 self, user_input: dict[str, Any] |
None =
None
78 ) -> ConfigFlowResult:
79 """Handle the initial step."""
82 if user_input
is not None:
85 except anthropic.APITimeoutError:
86 errors[
"base"] =
"timeout_connect"
87 except anthropic.APIConnectionError:
88 errors[
"base"] =
"cannot_connect"
89 except anthropic.APIStatusError
as e:
90 errors[
"base"] =
"unknown"
92 isinstance(e.body, dict)
93 and (error := e.body.get(
"error"))
94 and error.get(
"type") ==
"authentication_error"
96 errors[
"base"] =
"authentication_error"
98 _LOGGER.exception(
"Unexpected exception")
99 errors[
"base"] =
"unknown"
104 options=RECOMMENDED_OPTIONS,
108 step_id=
"user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
or None
113 config_entry: ConfigEntry,
115 """Create the options flow."""
120 """Anthropic config flow options handler."""
122 def __init__(self, config_entry: ConfigEntry) ->
None:
123 """Initialize options flow."""
125 CONF_RECOMMENDED,
False
129 self, user_input: dict[str, Any] |
None =
None
130 ) -> ConfigFlowResult:
131 """Manage the options."""
134 if user_input
is not None:
136 if user_input[CONF_LLM_HASS_API] ==
"none":
137 user_input.pop(CONF_LLM_HASS_API)
144 CONF_RECOMMENDED: user_input[CONF_RECOMMENDED],
145 CONF_PROMPT: user_input[CONF_PROMPT],
146 CONF_LLM_HASS_API: user_input[CONF_LLM_HASS_API],
149 suggested_values = options.copy()
150 if not suggested_values.get(CONF_PROMPT):
151 suggested_values[CONF_PROMPT] = llm.DEFAULT_INSTRUCTIONS_PROMPT
166 options: dict[str, Any] | MappingProxyType[str, Any],
168 """Return a schema for Anthropic completion options."""
169 hass_apis: list[SelectOptionDict] = [
180 for api
in llm.async_get_apis(hass)
189 CONF_RECOMMENDED, default=options.get(CONF_RECOMMENDED,
False)
193 if options.get(CONF_RECOMMENDED):
200 default=RECOMMENDED_CHAT_MODEL,
204 default=RECOMMENDED_MAX_TOKENS,
208 default=RECOMMENDED_TEMPERATURE,
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
OptionsFlow async_get_options_flow(ConfigEntry config_entry)
None __init__(self, ConfigEntry config_entry)
last_rendered_recommended
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
ConfigEntry config_entry(self)
None config_entry(self, ConfigEntry value)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
dict anthropic_config_option_schema(HomeAssistant hass, dict[str, Any]|MappingProxyType[str, Any] options)
None validate_input(HomeAssistant hass, dict[str, Any] data)