diff --git a/homeassistant/components/mastodon/__init__.py b/homeassistant/components/mastodon/__init__.py index b6e0d863471..6c8f53e4cb2 100644 --- a/homeassistant/components/mastodon/__init__.py +++ b/homeassistant/components/mastodon/__init__.py @@ -2,7 +2,14 @@ from __future__ import annotations -from mastodon.Mastodon import Account, Instance, InstanceV2, Mastodon, MastodonError +from mastodon.Mastodon import ( + Account, + Instance, + InstanceV2, + Mastodon, + MastodonError, + MastodonNotFoundError, +) from homeassistant.const import ( CONF_ACCESS_TOKEN, @@ -105,7 +112,11 @@ def setup_mastodon( entry.data[CONF_ACCESS_TOKEN], ) - instance = client.instance() + try: + instance = client.instance_v2() + except MastodonNotFoundError: + instance = client.instance_v1() + account = client.account_verify_credentials() return client, instance, account diff --git a/homeassistant/components/mastodon/config_flow.py b/homeassistant/components/mastodon/config_flow.py index 1ae1e6b229e..dbd617eca5f 100644 --- a/homeassistant/components/mastodon/config_flow.py +++ b/homeassistant/components/mastodon/config_flow.py @@ -7,7 +7,9 @@ from typing import Any from mastodon.Mastodon import ( Account, Instance, + InstanceV2, MastodonNetworkError, + MastodonNotFoundError, MastodonUnauthorizedError, ) import voluptuous as vol @@ -61,7 +63,7 @@ class MastodonConfigFlow(ConfigFlow, domain=DOMAIN): client_secret: str, access_token: str, ) -> tuple[ - Instance | None, + InstanceV2 | Instance | None, Account | None, dict[str, str], ]: @@ -73,7 +75,10 @@ class MastodonConfigFlow(ConfigFlow, domain=DOMAIN): client_secret, access_token, ) - instance = client.instance() + try: + instance = client.instance_v2() + except MastodonNotFoundError: + instance = client.instance_v1() account = client.account_verify_credentials() except MastodonNetworkError: diff --git a/homeassistant/components/mastodon/diagnostics.py b/homeassistant/components/mastodon/diagnostics.py index 31444413dfd..434f6c0acac 100644 --- a/homeassistant/components/mastodon/diagnostics.py +++ b/homeassistant/components/mastodon/diagnostics.py @@ -4,7 +4,7 @@ from __future__ import annotations from typing import Any -from mastodon.Mastodon import Account, Instance +from mastodon.Mastodon import Account, Instance, InstanceV2, MastodonNotFoundError from homeassistant.core import HomeAssistant @@ -27,11 +27,16 @@ async def async_get_config_entry_diagnostics( } -def get_diagnostics(config_entry: MastodonConfigEntry) -> tuple[Instance, Account]: +def get_diagnostics( + config_entry: MastodonConfigEntry, +) -> tuple[InstanceV2 | Instance, Account]: """Get mastodon diagnostics.""" client = config_entry.runtime_data.client - instance = client.instance() + try: + instance = client.instance_v2() + except MastodonNotFoundError: + instance = client.instance_v1() account = client.account_verify_credentials() return instance, account diff --git a/tests/components/mastodon/conftest.py b/tests/components/mastodon/conftest.py index d8979083de9..0a0e203bf28 100644 --- a/tests/components/mastodon/conftest.py +++ b/tests/components/mastodon/conftest.py @@ -32,12 +32,16 @@ def mock_mastodon_client() -> Generator[AsyncMock]: ) as mock_client, ): client = mock_client.return_value - client.instance.return_value = InstanceV2.from_json( + client.instance_v1.return_value = InstanceV2.from_json( + load_fixture("instance.json", DOMAIN) + ) + client.instance_v2.return_value = InstanceV2.from_json( load_fixture("instance.json", DOMAIN) ) client.account_verify_credentials.return_value = Account.from_json( load_fixture("account_verify_credentials.json", DOMAIN) ) + client.mastodon_api_version = 2 client.status_post.return_value = None yield client diff --git a/tests/components/mastodon/snapshots/test_diagnostics.ambr b/tests/components/mastodon/snapshots/test_diagnostics.ambr index ec9da1836bc..81abc77e21f 100644 --- a/tests/components/mastodon/snapshots/test_diagnostics.ambr +++ b/tests/components/mastodon/snapshots/test_diagnostics.ambr @@ -83,3 +83,87 @@ }), }) # --- +# name: test_entry_diagnostics_fallback_to_instance_v1 + dict({ + 'account': dict({ + 'acct': 'trwnh', + 'avatar': 'https://files.mastodon.social/accounts/avatars/000/014/715/original/051c958388818705.png', + 'avatar_static': 'https://files.mastodon.social/accounts/avatars/000/014/715/original/051c958388818705.png', + 'bot': True, + 'created_at': '2016-11-24T00:00:00+00:00', + 'discoverable': True, + 'display_name': 'infinite love ⴳ', + 'emojis': list([ + ]), + 'fields': list([ + dict({ + 'name': 'Website', + 'value': 'https://trwnh.com', + 'verified_at': '2019-08-29T04:14:55.571+00:00', + }), + dict({ + 'name': 'Portfolio', + 'value': 'https://abdullahtarawneh.com', + 'verified_at': '2021-02-11T20:34:13.574+00:00', + }), + dict({ + 'name': 'Fan of:', + 'value': 'Punk-rock and post-hardcore (Circa Survive, letlive., La Dispute, THE FEVER 333)Manga (Yu-Gi-Oh!, One Piece, JoJo's Bizarre Adventure, Death Note, Shaman King)Platformers and RPGs (Banjo-Kazooie, Boktai, Final Fantasy Crystal Chronicles)', + 'verified_at': None, + }), + dict({ + 'name': 'What to expect:', + 'value': 'talking about various things i find interesting, and otherwise being a genuine and decent wholesome poster. i'm just here to hang out and talk to cool people! and to spill my thoughts.', + 'verified_at': None, + }), + ]), + 'followers_count': 3169, + 'following_count': 328, + 'group': False, + 'header': 'https://files.mastodon.social/accounts/headers/000/014/715/original/5c6fc24edb3bb873.jpg', + 'header_static': 'https://files.mastodon.social/accounts/headers/000/014/715/original/5c6fc24edb3bb873.jpg', + 'hide_collections': True, + 'id': '14715', + 'indexable': False, + 'last_status_at': '2025-03-04T00:00:00', + 'limited': None, + 'locked': False, + 'memorial': None, + 'moved': None, + 'moved_to_account': None, + 'mute_expires_at': None, + 'noindex': False, + 'note': '
i have approximate knowledge of many things. perpetual student. (nb/ace/they)
xmpp/email: a@trwnh.com
https://trwnh.com
help me live:
- https://donate.stripe.com/4gwcPCaMpcQ19RC4gg
- https://liberapay.com/trwnh
notes:
- my triggers are moths and glitter
- i have all notifs except mentions turned off, so please interact if you wanna be friends! i literally will not notice otherwise
- dm me if i did something wrong, so i can improve
- purest person on fedi, do not lewd in my presence