Home Assistant Unofficial Reference 2024.12.1
api.py
Go to the documentation of this file.
1 """API for Neato Botvac bound to Home Assistant OAuth."""
2 
3 from __future__ import annotations
4 
5 from asyncio import run_coroutine_threadsafe
6 from typing import Any
7 
8 import pybotvac
9 
10 from homeassistant import config_entries, core
11 from homeassistant.components.application_credentials import AuthImplementation
12 from homeassistant.helpers import config_entry_oauth2_flow
13 
14 
15 class ConfigEntryAuth(pybotvac.OAuthSession): # type: ignore[misc]
16  """Provide Neato Botvac authentication tied to an OAuth2 based config entry."""
17 
18  def __init__(
19  self,
20  hass: core.HomeAssistant,
21  config_entry: config_entries.ConfigEntry,
22  implementation: config_entry_oauth2_flow.AbstractOAuth2Implementation,
23  ) -> None:
24  """Initialize Neato Botvac Auth."""
25  self.hasshass = hass
26  self.sessionsession = config_entry_oauth2_flow.OAuth2Session(
27  hass, config_entry, implementation
28  )
29  super().__init__(self.sessionsession.token, vendor=pybotvac.Neato())
30 
31  def refresh_tokens(self) -> str:
32  """Refresh and return new Neato Botvac tokens."""
33  run_coroutine_threadsafe(
34  self.sessionsession.async_ensure_token_valid(), self.hasshass.loop
35  ).result()
36 
37  return self.sessionsession.token["access_token"] # type: ignore[no-any-return]
38 
39 
41  """Neato implementation of LocalOAuth2Implementation.
42 
43  We need this class because we have to add client_secret
44  and scope to the authorization request.
45  """
46 
47  @property
48  def extra_authorize_data(self) -> dict[str, Any]:
49  """Extra data that needs to be appended to the authorize url."""
50  return {"client_secret": self.client_secret}
51 
52  async def async_generate_authorize_url(self, flow_id: str) -> str:
53  """Generate a url for the user to authorize.
54 
55  We must make sure that the plus signs are not encoded.
56  """
57  url = await super().async_generate_authorize_url(flow_id)
58  return f"{url}&scope=public_profile+control_robots+maps"
None __init__(self, core.HomeAssistant hass, config_entries.ConfigEntry config_entry, config_entry_oauth2_flow.AbstractOAuth2Implementation implementation)
Definition: api.py:23
str async_generate_authorize_url(self, str flow_id)
Definition: api.py:52