Home Assistant Unofficial Reference 2024.12.1
api.py
Go to the documentation of this file.
1 """API for Ondilo ICO bound to Home Assistant OAuth."""
2 
3 from asyncio import run_coroutine_threadsafe
4 import logging
5 
6 from ondilo import Ondilo
7 
8 from homeassistant import config_entries, core
9 from homeassistant.helpers import config_entry_oauth2_flow
10 
11 _LOGGER = logging.getLogger(__name__)
12 
13 
14 class OndiloClient(Ondilo):
15  """Provide Ondilo ICO authentication tied to an OAuth2 based config entry."""
16 
17  def __init__(
18  self,
19  hass: core.HomeAssistant,
20  config_entry: config_entries.ConfigEntry,
21  implementation: config_entry_oauth2_flow.AbstractOAuth2Implementation,
22  ) -> None:
23  """Initialize Ondilo ICO Auth."""
24  self.hasshass = hass
25  self.config_entryconfig_entry = config_entry
26  self.sessionsession = config_entry_oauth2_flow.OAuth2Session(
27  hass, config_entry, implementation
28  )
29  super().__init__(self.sessionsession.token)
30 
31  def refresh_tokens(self) -> dict:
32  """Refresh and return new Ondilo ICO tokens using Home Assistant OAuth2 session."""
33  run_coroutine_threadsafe(
34  self.sessionsession.async_ensure_token_valid(), self.hasshass.loop
35  ).result()
36 
37  return self.sessionsession.token
None __init__(self, core.HomeAssistant hass, config_entries.ConfigEntry config_entry, config_entry_oauth2_flow.AbstractOAuth2Implementation implementation)
Definition: api.py:22