Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Google Assistant SDK integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 import logging
7 from typing import Any
8 
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import (
12  SOURCE_REAUTH,
13  ConfigEntry,
14  ConfigFlowResult,
15  OptionsFlow,
16 )
17 from homeassistant.core import callback
18 from homeassistant.helpers import config_entry_oauth2_flow
19 
20 from .const import CONF_LANGUAGE_CODE, DEFAULT_NAME, DOMAIN, SUPPORTED_LANGUAGE_CODES
21 from .helpers import default_language_code
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 
27  config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=DOMAIN
28 ):
29  """Config flow to handle Google Assistant SDK OAuth2 authentication."""
30 
31  DOMAIN = DOMAIN
32 
33  @property
34  def logger(self) -> logging.Logger:
35  """Return logger."""
36  return logging.getLogger(__name__)
37 
38  @property
39  def extra_authorize_data(self) -> dict[str, Any]:
40  """Extra data that needs to be appended to the authorize url."""
41  return {
42  "scope": "https://www.googleapis.com/auth/assistant-sdk-prototype",
43  # Add params to ensure we get back a refresh token
44  "access_type": "offline",
45  "prompt": "consent",
46  }
47 
48  async def async_step_reauth(
49  self, entry_data: Mapping[str, Any]
50  ) -> ConfigFlowResult:
51  """Perform reauth upon an API authentication error."""
52  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
53 
55  self, user_input: dict[str, Any] | None = None
56  ) -> ConfigFlowResult:
57  """Confirm reauth dialog."""
58  if user_input is None:
59  return self.async_show_form(step_id="reauth_confirm")
60  return await self.async_step_user()
61 
62  async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResult:
63  """Create an entry for the flow, or update existing entry."""
64  if self.sourcesource == SOURCE_REAUTH:
65  return self.async_update_reload_and_abort(
66  self._get_reauth_entry(), data=data
67  )
68 
69  return self.async_create_entry(
70  title=DEFAULT_NAME,
71  data=data,
72  options={
73  CONF_LANGUAGE_CODE: default_language_code(self.hass),
74  },
75  )
76 
77  @staticmethod
78  @callback
80  config_entry: ConfigEntry,
81  ) -> OptionsFlow:
82  """Create the options flow."""
83  return OptionsFlowHandler()
84 
85 
87  """Google Assistant SDK options flow."""
88 
89  async def async_step_init(
90  self, user_input: dict[str, Any] | None = None
91  ) -> ConfigFlowResult:
92  """Manage the options."""
93  if user_input is not None:
94  return self.async_create_entryasync_create_entry(title="", data=user_input)
95 
96  return self.async_show_formasync_show_form(
97  step_id="init",
98  data_schema=vol.Schema(
99  {
100  vol.Required(
101  CONF_LANGUAGE_CODE,
102  default=self.config_entryconfig_entryconfig_entry.options.get(CONF_LANGUAGE_CODE),
103  ): vol.In(SUPPORTED_LANGUAGE_CODES),
104  }
105  ),
106  )
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:56
ConfigFlowResult async_oauth_create_entry(self, dict[str, Any] data)
Definition: config_flow.py:62
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:50
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:91
None config_entry(self, ConfigEntry value)
_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)