2022-12-21 01:32:29 +00:00
|
|
|
"""The Mastodon integration."""
|
2024-07-27 11:07:02 +00:00
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from mastodon.Mastodon import Mastodon, MastodonError
|
|
|
|
|
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_ACCESS_TOKEN,
|
|
|
|
CONF_CLIENT_ID,
|
|
|
|
CONF_CLIENT_SECRET,
|
|
|
|
CONF_NAME,
|
|
|
|
Platform,
|
|
|
|
)
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2025-01-31 11:29:23 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv, discovery
|
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2024-08-14 10:55:59 +00:00
|
|
|
from homeassistant.util import slugify
|
2024-07-27 11:07:02 +00:00
|
|
|
|
2024-08-14 10:55:59 +00:00
|
|
|
from .const import CONF_BASE_URL, DOMAIN, LOGGER
|
2025-01-31 11:29:23 +00:00
|
|
|
from .coordinator import MastodonConfigEntry, MastodonCoordinator, MastodonData
|
|
|
|
from .services import setup_services
|
2024-08-14 10:55:59 +00:00
|
|
|
from .utils import construct_mastodon_username, create_mastodon_client
|
2024-07-27 11:07:02 +00:00
|
|
|
|
2024-08-09 14:02:27 +00:00
|
|
|
PLATFORMS: list[Platform] = [Platform.NOTIFY, Platform.SENSOR]
|
|
|
|
|
2025-01-31 11:29:23 +00:00
|
|
|
CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
|
2024-08-09 14:02:27 +00:00
|
|
|
|
|
|
|
|
2025-01-31 11:29:23 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|
|
|
"""Set up the Mastodon component."""
|
|
|
|
setup_services(hass)
|
|
|
|
return True
|
2024-08-09 14:02:27 +00:00
|
|
|
|
2024-07-27 11:07:02 +00:00
|
|
|
|
2024-08-09 14:02:27 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: MastodonConfigEntry) -> bool:
|
2024-07-27 11:07:02 +00:00
|
|
|
"""Set up Mastodon from a config entry."""
|
|
|
|
|
|
|
|
try:
|
2024-08-09 14:02:27 +00:00
|
|
|
client, instance, account = await hass.async_add_executor_job(
|
2024-07-27 11:07:02 +00:00
|
|
|
setup_mastodon,
|
|
|
|
entry,
|
|
|
|
)
|
|
|
|
|
|
|
|
except MastodonError as ex:
|
|
|
|
raise ConfigEntryNotReady("Failed to connect") from ex
|
|
|
|
|
|
|
|
assert entry.unique_id
|
|
|
|
|
2024-08-09 14:02:27 +00:00
|
|
|
coordinator = MastodonCoordinator(hass, client)
|
|
|
|
|
|
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
|
|
|
|
entry.runtime_data = MastodonData(client, instance, account, coordinator)
|
|
|
|
|
2024-07-27 11:07:02 +00:00
|
|
|
await discovery.async_load_platform(
|
|
|
|
hass,
|
|
|
|
Platform.NOTIFY,
|
|
|
|
DOMAIN,
|
|
|
|
{CONF_NAME: entry.title, "client": client},
|
|
|
|
{},
|
|
|
|
)
|
|
|
|
|
2024-08-09 14:02:27 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(
|
|
|
|
entry, [platform for platform in PLATFORMS if platform != Platform.NOTIFY]
|
|
|
|
)
|
|
|
|
|
2024-07-27 11:07:02 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2024-08-09 14:02:27 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: MastodonConfigEntry) -> bool:
|
|
|
|
"""Unload a config entry."""
|
|
|
|
return await hass.config_entries.async_unload_platforms(
|
|
|
|
entry, [platform for platform in PLATFORMS if platform != Platform.NOTIFY]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-12-04 08:54:29 +00:00
|
|
|
async def async_migrate_entry(hass: HomeAssistant, entry: MastodonConfigEntry) -> bool:
|
2024-08-14 10:55:59 +00:00
|
|
|
"""Migrate old config."""
|
|
|
|
|
|
|
|
if entry.version == 1 and entry.minor_version == 1:
|
|
|
|
# Version 1.1 had the unique_id as client_id, this isn't necessarily unique
|
|
|
|
LOGGER.debug("Migrating config entry from version %s", entry.version)
|
|
|
|
|
|
|
|
try:
|
|
|
|
_, instance, account = await hass.async_add_executor_job(
|
|
|
|
setup_mastodon,
|
|
|
|
entry,
|
|
|
|
)
|
|
|
|
except MastodonError as ex:
|
|
|
|
LOGGER.error("Migration failed with error %s", ex)
|
|
|
|
return False
|
|
|
|
|
|
|
|
hass.config_entries.async_update_entry(
|
|
|
|
entry,
|
2024-08-29 10:23:04 +00:00
|
|
|
minor_version=2,
|
2024-08-14 10:55:59 +00:00
|
|
|
unique_id=slugify(construct_mastodon_username(instance, account)),
|
|
|
|
)
|
|
|
|
|
2024-08-25 07:53:30 +00:00
|
|
|
LOGGER.debug(
|
2024-08-14 10:55:59 +00:00
|
|
|
"Entry %s successfully migrated to version %s.%s",
|
|
|
|
entry.entry_id,
|
|
|
|
entry.version,
|
|
|
|
entry.minor_version,
|
|
|
|
)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2024-12-04 08:54:29 +00:00
|
|
|
def setup_mastodon(entry: MastodonConfigEntry) -> tuple[Mastodon, dict, dict]:
|
2024-07-27 11:07:02 +00:00
|
|
|
"""Get mastodon details."""
|
|
|
|
client = create_mastodon_client(
|
|
|
|
entry.data[CONF_BASE_URL],
|
|
|
|
entry.data[CONF_CLIENT_ID],
|
|
|
|
entry.data[CONF_CLIENT_SECRET],
|
|
|
|
entry.data[CONF_ACCESS_TOKEN],
|
|
|
|
)
|
|
|
|
|
|
|
|
instance = client.instance()
|
|
|
|
account = client.account_verify_credentials()
|
|
|
|
|
|
|
|
return client, instance, account
|