Repository: home-assistant/core
File: tests/components/mastodon/test_init.py (Python)

Code before:

async def test_coordinator_general_error(
    hass: HomeAssistant,
    mock_mastodon_client: AsyncMock,
    mock_config_entry: MockConfigEntry,
    freezer: FrozenDateTimeFactory,
) -> None:
    """Test general error during coordinator update makes entities unavailable."""
    await setup_integration(hass, mock_config_entry)
    assert mock_config_entry.state is ConfigEntryState.LOADED

    state = hass.states.get("binary_sensor.mastodon_trwnh_mastodon_social_bot")
    assert state is not None
    assert state.state == "on"

    mock_mastodon_client.account_verify_credentials.side_effect = MastodonError

    freezer.tick(timedelta(hours=1))
    async_fire_time_changed(hass)
    await hass.async_block_till_done()

    assert mock_config_entry.state is ConfigEntryState.LOADED

    state = hass.states.get("binary_sensor.mastodon_trwnh_mastodon_social_bot")
    assert state is not None
    assert state.state == "unavailable"

    # No reauth flow should be triggered (unlike auth errors)
    flows = hass.config_entries.flow.async_progress()
    assert len(flows) == 0


async def test_coordinator_auth_failure_triggers_reauth(
    hass: HomeAssistant,
    mock_mastodon_client: AsyncMock,
    mock_config_entry: MockConfigEntry,
    freezer: FrozenDateTimeFactory,
) -> None:
    """Test auth failure during coordinator update triggers reauth flow."""
    await setup_integration(hass, mock_config_entry)
    assert mock_config_entry.state is ConfigEntryState.LOADED

    mock_mastodon_client.account_verify_credentials.side_effect = (
        MastodonUnauthorizedError
    )

    freezer.tick(timedelta(hours=1))
    async_fire_time_changed(hass)
    await hass.async_block_till_done()

    assert mock_config_entry.state is ConfigEntryState.LOADED

Review comment (suggestion):
```suggestion
    assert state.state == STATE_UNAVAILABLE
```

Code after:
        CONF_ACCESS_TOKEN: "access_token",
    }
    assert config_entry.version == MastodonConfigFlow.VERSION
    assert config_entry.minor_version == MastodonConfigFlow.MINOR_VERSION
    assert config_entry.unique_id == "trwnh_mastodon_social"


async def test_coordinator_general_error(
    hass: HomeAssistant,
    mock_mastodon_client: AsyncMock,
    mock_config_entry: MockConfigEntry,
    freezer: FrozenDateTimeFactory,
) -> None:
    """Test general error during coordinator update makes entities unavailable."""
    await setup_integration(hass, mock_config_entry)
    assert mock_config_entry.state is ConfigEntryState.LOADED

    state = hass.states.get("binary_sensor.mastodon_trwnh_mastodon_social_bot")
    assert state is not None
    assert state.state == STATE_ON

    mock_mastodon_client.account_verify_credentials.side_effect = MastodonError

    freezer.tick(timedelta(hours=1))
    async_fire_time_changed(hass)
    await hass.async_block_till_done()

    assert mock_config_entry.state is ConfigEntryState.LOADED

    state = hass.states.get("binary_sensor.mastodon_trwnh_mastodon_social_bot")
    assert state is not None
    assert state.state == STATE_UNAVAILABLE

    # No reauth flow should be triggered (unlike auth errors)
    flows = hass.config_entries.flow.async_progress()
    assert len(flows) == 0


async def test_coordinator_auth_failure_triggers_reauth(
    hass: HomeAssistant,
    mock_mastodon_client: AsyncMock,
    mock_config_entry: MockConfigEntry,
    freezer: FrozenDateTimeFactory,
) -> None:
    """Test auth failure during coordinator update triggers reauth flow."""
    await setup_integration(hass, mock_config_entry)
    assert mock_config_entry.state is ConfigEntryState.LOADED

    mock_mastodon_client.account_verify_credentials.side_effect = (
        MastodonUnauthorizedError
    )