Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The flume integration."""
2 
3 from __future__ import annotations
4 
5 from pyflume import FlumeAuth, FlumeDeviceList
6 from requests import Session
7 from requests.exceptions import RequestException
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import (
12  CONF_CLIENT_ID,
13  CONF_CLIENT_SECRET,
14  CONF_PASSWORD,
15  CONF_USERNAME,
16 )
17 from homeassistant.core import (
18  HomeAssistant,
19  ServiceCall,
20  ServiceResponse,
21  SupportsResponse,
22  callback,
23 )
24 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
25 from homeassistant.helpers.selector import ConfigEntrySelector
26 
27 from .const import (
28  BASE_TOKEN_FILENAME,
29  DOMAIN,
30  FLUME_AUTH,
31  FLUME_DEVICES,
32  FLUME_HTTP_SESSION,
33  FLUME_NOTIFICATIONS_COORDINATOR,
34  PLATFORMS,
35 )
36 from .coordinator import FlumeNotificationDataUpdateCoordinator
37 
38 SERVICE_LIST_NOTIFICATIONS = "list_notifications"
39 CONF_CONFIG_ENTRY = "config_entry"
40 LIST_NOTIFICATIONS_SERVICE_SCHEMA = vol.All(
41  {
42  vol.Required(CONF_CONFIG_ENTRY): ConfigEntrySelector(),
43  },
44 )
45 
46 
48  hass: HomeAssistant, entry: ConfigEntry
49 ) -> tuple[FlumeAuth, FlumeDeviceList, Session]:
50  """Config entry set up in executor."""
51  config = entry.data
52 
53  username = config[CONF_USERNAME]
54  password = config[CONF_PASSWORD]
55  client_id = config[CONF_CLIENT_ID]
56  client_secret = config[CONF_CLIENT_SECRET]
57  flume_token_full_path = hass.config.path(f"{BASE_TOKEN_FILENAME}-{username}")
58 
59  http_session = Session()
60 
61  try:
62  flume_auth = FlumeAuth(
63  username,
64  password,
65  client_id,
66  client_secret,
67  flume_token_file=flume_token_full_path,
68  http_session=http_session,
69  )
70  flume_devices = FlumeDeviceList(flume_auth, http_session=http_session)
71  except RequestException as ex:
72  raise ConfigEntryNotReady from ex
73  except Exception as ex:
74  raise ConfigEntryAuthFailed from ex
75 
76  return flume_auth, flume_devices, http_session
77 
78 
79 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
80  """Set up flume from a config entry."""
81 
82  flume_auth, flume_devices, http_session = await hass.async_add_executor_job(
83  _setup_entry, hass, entry
84  )
85  notification_coordinator = FlumeNotificationDataUpdateCoordinator(
86  hass=hass, auth=flume_auth
87  )
88 
89  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
90  FLUME_DEVICES: flume_devices,
91  FLUME_AUTH: flume_auth,
92  FLUME_HTTP_SESSION: http_session,
93  FLUME_NOTIFICATIONS_COORDINATOR: notification_coordinator,
94  }
95 
96  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
97  setup_service(hass)
98 
99  return True
100 
101 
102 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
103  """Unload a config entry."""
104  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
105 
106  hass.data[DOMAIN][entry.entry_id][FLUME_HTTP_SESSION].close()
107 
108  if unload_ok:
109  hass.data[DOMAIN].pop(entry.entry_id)
110 
111  return unload_ok
112 
113 
114 def setup_service(hass: HomeAssistant) -> None:
115  """Add the services for the flume integration."""
116 
117  @callback
118  def list_notifications(call: ServiceCall) -> ServiceResponse:
119  """Return the user notifications."""
120  entry_id: str = call.data[CONF_CONFIG_ENTRY]
121  entry: ConfigEntry | None = hass.config_entries.async_get_entry(entry_id)
122  if not entry:
123  raise ValueError(f"Invalid config entry: {entry_id}")
124  if not (flume_domain_data := hass.data[DOMAIN].get(entry_id)):
125  raise ValueError(f"Config entry not loaded: {entry_id}")
126  return {
127  "notifications": flume_domain_data[
128  FLUME_NOTIFICATIONS_COORDINATOR
129  ].notifications
130  }
131 
132  hass.services.async_register(
133  DOMAIN,
134  SERVICE_LIST_NOTIFICATIONS,
135  list_notifications,
136  schema=LIST_NOTIFICATIONS_SERVICE_SCHEMA,
137  supports_response=SupportsResponse.ONLY,
138  )
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:79
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:102
None setup_service(HomeAssistant hass)
Definition: __init__.py:114
tuple[FlumeAuth, FlumeDeviceList, Session] _setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:49