1 """The flume integration."""
3 from __future__
import annotations
5 from pyflume
import FlumeAuth, FlumeDeviceList
6 from requests
import Session
7 from requests.exceptions
import RequestException
8 import voluptuous
as vol
33 FLUME_NOTIFICATIONS_COORDINATOR,
36 from .coordinator
import FlumeNotificationDataUpdateCoordinator
38 SERVICE_LIST_NOTIFICATIONS =
"list_notifications"
39 CONF_CONFIG_ENTRY =
"config_entry"
40 LIST_NOTIFICATIONS_SERVICE_SCHEMA = vol.All(
48 hass: HomeAssistant, entry: ConfigEntry
49 ) -> tuple[FlumeAuth, FlumeDeviceList, Session]:
50 """Config entry set up in executor."""
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}")
59 http_session = Session()
62 flume_auth = FlumeAuth(
67 flume_token_file=flume_token_full_path,
68 http_session=http_session,
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
76 return flume_auth, flume_devices, http_session
80 """Set up flume from a config entry."""
82 flume_auth, flume_devices, http_session = await hass.async_add_executor_job(
83 _setup_entry, hass, entry
86 hass=hass, auth=flume_auth
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,
96 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
103 """Unload a config entry."""
104 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
106 hass.data[DOMAIN][entry.entry_id][FLUME_HTTP_SESSION].close()
109 hass.data[DOMAIN].pop(entry.entry_id)
115 """Add the services for the flume integration."""
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)
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}")
127 "notifications": flume_domain_data[
128 FLUME_NOTIFICATIONS_COORDINATOR
132 hass.services.async_register(
134 SERVICE_LIST_NOTIFICATIONS,
136 schema=LIST_NOTIFICATIONS_SERVICE_SCHEMA,
137 supports_response=SupportsResponse.ONLY,
web.Response get(self, web.Request request, str config_key)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
None setup_service(HomeAssistant hass)
tuple[FlumeAuth, FlumeDeviceList, Session] _setup_entry(HomeAssistant hass, ConfigEntry entry)