1 """Support for Notion."""
3 from __future__
import annotations
5 from datetime
import timedelta
9 from aionotion.errors
import InvalidCredentialsError, NotionError
10 from aionotion.listener.models
import ListenerKind
34 from .coordinator
import NotionDataUpdateCoordinator
35 from .util
import async_get_client_with_credentials, async_get_client_with_refresh_token
37 PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
39 ATTR_SYSTEM_MODE =
"system_mode"
40 ATTR_SYSTEM_NAME =
"system_name"
46 TASK_TYPE_TO_LISTENER_MAP: dict[str, ListenerKind] = {
47 SENSOR_BATTERY: ListenerKind.BATTERY,
48 SENSOR_DOOR: ListenerKind.DOOR,
49 SENSOR_GARAGE_DOOR: ListenerKind.GARAGE_DOOR,
50 SENSOR_LEAK: ListenerKind.LEAK_STATUS,
51 SENSOR_MISSING: ListenerKind.CONNECTED,
52 SENSOR_SAFE: ListenerKind.SAFE,
53 SENSOR_SLIDING: ListenerKind.SLIDING_DOOR_OR_WINDOW,
54 SENSOR_SMOKE_CO: ListenerKind.SMOKE,
55 SENSOR_TEMPERATURE: ListenerKind.TEMPERATURE,
56 SENSOR_WINDOW_HINGED: ListenerKind.HINGED_WINDOW,
62 """Return whether a string is a valid UUID."""
71 """Set up Notion as a config entry."""
72 entry_updates: dict[str, Any] = {
"data": {**entry.data}}
74 if not entry.unique_id:
75 entry_updates[
"unique_id"] = entry.data[CONF_USERNAME]
78 if password := entry_updates[
"data"].pop(CONF_PASSWORD,
None):
82 hass, entry.data[CONF_USERNAME], password
89 entry.data[CONF_USER_UUID],
90 entry.data[CONF_REFRESH_TOKEN],
92 except InvalidCredentialsError
as err:
94 except NotionError
as err:
99 (CONF_REFRESH_TOKEN, client.refresh_token),
100 (CONF_USER_UUID, client.user_uuid),
102 if entry.data.get(key) == value:
104 entry_updates[
"data"][key] = value
106 hass.config_entries.async_update_entry(entry, **entry_updates)
109 def async_save_refresh_token(refresh_token: str) ->
None:
110 """Save a refresh token to the config entry data."""
111 LOGGER.debug(
"Saving new refresh token to HASS storage")
112 hass.config_entries.async_update_entry(
113 entry, data={**entry.data, CONF_REFRESH_TOKEN: refresh_token}
117 entry.async_on_unload(client.add_refresh_token_callback(async_save_refresh_token))
121 await coordinator.async_config_entry_first_refresh()
122 hass.data.setdefault(DOMAIN, {})
123 hass.data[DOMAIN][entry.entry_id] = coordinator
127 """Migrate Notion entity entries.
129 This migration focuses on unique IDs, which have changed because of a Notion API
132 Old Format: <sensor_id>_<task_type>
133 New Format: <listener_uuid>
139 sensor_id_str, task_type = entry.unique_id.split(
"_", 1)
142 for sensor
in coordinator.data.sensors.values()
143 if sensor.id ==
int(sensor_id_str)
147 for listener
in coordinator.data.listeners.values()
148 if listener.sensor_id == sensor.uuid
149 and listener.definition_id == TASK_TYPE_TO_LISTENER_MAP[task_type].value
152 return {
"new_unique_id": listener.id}
154 await er.async_migrate_entries(hass, entry.entry_id, async_migrate_entity_entry)
155 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
161 """Unload a Notion config entry."""
162 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
164 hass.data[DOMAIN].pop(entry.entry_id)
Client async_get_client_with_credentials(HomeAssistant hass, str email, str password)
Client async_get_client_with_refresh_token(HomeAssistant hass, str user_uuid, str refresh_token)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
dict[str, Any]|None async_migrate_entity_entry(er.RegistryEntry entity_entry)