Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Google Tasks integration."""
2 
3 from __future__ import annotations
4 
5 from aiohttp import ClientError, ClientResponseError
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import Platform
9 from homeassistant.core import HomeAssistant
10 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
11 from homeassistant.helpers import config_entry_oauth2_flow
12 
13 from . import api
14 from .const import DOMAIN
15 
16 PLATFORMS: list[Platform] = [Platform.TODO]
17 
18 
19 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
20  """Set up Google Tasks from a config entry."""
21  implementation = (
22  await config_entry_oauth2_flow.async_get_config_entry_implementation(
23  hass, entry
24  )
25  )
26  session = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation)
27  auth = api.AsyncConfigEntryAuth(hass, session)
28  try:
29  await auth.async_get_access_token()
30  except ClientResponseError as err:
31  if 400 <= err.status < 500:
33  "OAuth session is not valid, reauth required"
34  ) from err
35  raise ConfigEntryNotReady from err
36  except ClientError as err:
37  raise ConfigEntryNotReady from err
38 
39  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = auth
40 
41  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
42 
43  return True
44 
45 
46 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
47  """Unload a config entry."""
48  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
49  hass.data[DOMAIN].pop(entry.entry_id)
50 
51  return unload_ok
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:46
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:19