Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Google Mail."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.config_entries import ConfigEntry, ConfigEntryState
6 from homeassistant.const import CONF_NAME, Platform
7 from homeassistant.core import HomeAssistant
8 from homeassistant.helpers import config_validation as cv, discovery
10  OAuth2Session,
11  async_get_config_entry_implementation,
12 )
13 from homeassistant.helpers.typing import ConfigType
14 
15 from .api import AsyncConfigEntryAuth
16 from .const import DATA_AUTH, DATA_HASS_CONFIG, DOMAIN
17 from .services import async_setup_services
18 
19 type GoogleMailConfigEntry = ConfigEntry[AsyncConfigEntryAuth]
20 
21 PLATFORMS = [Platform.NOTIFY, Platform.SENSOR]
22 
23 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
24 
25 
26 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
27  """Set up the Google Mail platform."""
28  hass.data.setdefault(DOMAIN, {})[DATA_HASS_CONFIG] = config
29 
30  return True
31 
32 
33 async def async_setup_entry(hass: HomeAssistant, entry: GoogleMailConfigEntry) -> bool:
34  """Set up Google Mail from a config entry."""
35  implementation = await async_get_config_entry_implementation(hass, entry)
36  session = OAuth2Session(hass, entry, implementation)
37  auth = AsyncConfigEntryAuth(hass, session)
38  await auth.check_and_refresh_token()
39  entry.runtime_data = auth
40 
41  hass.async_create_task(
42  discovery.async_load_platform(
43  hass,
44  Platform.NOTIFY,
45  DOMAIN,
46  {DATA_AUTH: auth, CONF_NAME: entry.title},
47  hass.data[DOMAIN][DATA_HASS_CONFIG],
48  )
49  )
50 
51  await hass.config_entries.async_forward_entry_setups(
52  entry, [platform for platform in PLATFORMS if platform != Platform.NOTIFY]
53  )
54 
55  await async_setup_services(hass)
56 
57  return True
58 
59 
60 async def async_unload_entry(hass: HomeAssistant, entry: GoogleMailConfigEntry) -> bool:
61  """Unload a config entry."""
62  loaded_entries = [
63  entry
64  for entry in hass.config_entries.async_entries(DOMAIN)
65  if entry.state == ConfigEntryState.LOADED
66  ]
67  if len(loaded_entries) == 1:
68  for service_name in hass.services.async_services_for_domain(DOMAIN):
69  hass.services.async_remove(DOMAIN, service_name)
70 
71  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_unload_entry(HomeAssistant hass, GoogleMailConfigEntry entry)
Definition: __init__.py:60
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:26
bool async_setup_entry(HomeAssistant hass, GoogleMailConfigEntry entry)
Definition: __init__.py:33
None async_setup_services(HomeAssistant hass)
Definition: __init__.py:72
AbstractOAuth2Implementation async_get_config_entry_implementation(HomeAssistant hass, config_entries.ConfigEntry config_entry)