Home Assistant Unofficial Reference 2024.12.1
utils.py
Go to the documentation of this file.
1 """Mastodon util functions."""
2 
3 from __future__ import annotations
4 
5 from mastodon import Mastodon
6 
7 from .const import ACCOUNT_USERNAME, DEFAULT_NAME, INSTANCE_DOMAIN, INSTANCE_URI
8 
9 
11  base_url: str, client_id: str, client_secret: str, access_token: str
12 ) -> Mastodon:
13  """Create a Mastodon client with the api base url."""
14  return Mastodon(
15  api_base_url=base_url,
16  client_id=client_id,
17  client_secret=client_secret,
18  access_token=access_token,
19  )
20 
21 
23  instance: dict[str, str] | None, account: dict[str, str] | None
24 ) -> str:
25  """Construct a mastodon username from the account and instance."""
26  if instance and account:
27  return (
28  f"@{account[ACCOUNT_USERNAME]}@"
29  f"{instance.get(INSTANCE_URI, instance.get(INSTANCE_DOMAIN))}"
30  )
31 
32  return DEFAULT_NAME
Mastodon create_mastodon_client(str base_url, str client_id, str client_secret, str access_token)
Definition: utils.py:12
str construct_mastodon_username(dict[str, str]|None instance, dict[str, str]|None account)
Definition: utils.py:24