1 """The Mastodon integration."""
3 from __future__
import annotations
5 from dataclasses
import dataclass
7 from mastodon.Mastodon
import Mastodon, MastodonError
22 from .const
import CONF_BASE_URL, DOMAIN, LOGGER
23 from .coordinator
import MastodonCoordinator
24 from .utils
import construct_mastodon_username, create_mastodon_client
26 PLATFORMS: list[Platform] = [Platform.NOTIFY, Platform.SENSOR]
31 """Mastodon data type."""
36 coordinator: MastodonCoordinator
39 type MastodonConfigEntry = ConfigEntry[MastodonData]
43 """Set up Mastodon from a config entry."""
46 client, instance, account = await hass.async_add_executor_job(
51 except MastodonError
as ex:
54 assert entry.unique_id
58 await coordinator.async_config_entry_first_refresh()
60 entry.runtime_data =
MastodonData(client, instance, account, coordinator)
62 await discovery.async_load_platform(
66 {CONF_NAME: entry.title,
"client": client},
70 await hass.config_entries.async_forward_entry_setups(
71 entry, [platform
for platform
in PLATFORMS
if platform != Platform.NOTIFY]
78 """Unload a config entry."""
79 return await hass.config_entries.async_unload_platforms(
80 entry, [platform
for platform
in PLATFORMS
if platform != Platform.NOTIFY]
85 """Migrate old config."""
87 if entry.version == 1
and entry.minor_version == 1:
89 LOGGER.debug(
"Migrating config entry from version %s", entry.version)
92 _, instance, account = await hass.async_add_executor_job(
96 except MastodonError
as ex:
97 LOGGER.error(
"Migration failed with error %s", ex)
100 hass.config_entries.async_update_entry(
107 "Entry %s successfully migrated to version %s.%s",
117 """Get mastodon details."""
119 entry.data[CONF_BASE_URL],
120 entry.data[CONF_CLIENT_ID],
121 entry.data[CONF_CLIENT_SECRET],
122 entry.data[CONF_ACCESS_TOKEN],
125 instance = client.instance()
126 account = client.account_verify_credentials()
128 return client, instance, account
Mastodon create_mastodon_client(str base_url, str client_id, str client_secret, str access_token)
str construct_mastodon_username(dict[str, str]|None instance, dict[str, str]|None account)
bool async_setup_entry(HomeAssistant hass, MastodonConfigEntry entry)
tuple[Mastodon, dict, dict] setup_mastodon(ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, MastodonConfigEntry entry)
bool async_migrate_entry(HomeAssistant hass, ConfigEntry entry)
str slugify(str|None text, *str separator="_")