mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 01:37:08 +00:00
Improve exception handling of PlayStation Network (#147792)
This commit is contained in:
parent
5e3fc858d8
commit
2c30a5a14c
@ -7,13 +7,13 @@ import logging
|
||||
|
||||
from psnawp_api.core.psnawp_exceptions import (
|
||||
PSNAWPAuthenticationError,
|
||||
PSNAWPClientError,
|
||||
PSNAWPServerError,
|
||||
)
|
||||
from psnawp_api.models.user import User
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
from .const import DOMAIN
|
||||
@ -28,7 +28,6 @@ class PlaystationNetworkCoordinator(DataUpdateCoordinator[PlaystationNetworkData
|
||||
"""Data update coordinator for PSN."""
|
||||
|
||||
config_entry: PlaystationNetworkConfigEntry
|
||||
user: User
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@ -51,12 +50,17 @@ class PlaystationNetworkCoordinator(DataUpdateCoordinator[PlaystationNetworkData
|
||||
"""Set up the coordinator."""
|
||||
|
||||
try:
|
||||
self.user = await self.psn.get_user()
|
||||
await self.psn.get_user()
|
||||
except PSNAWPAuthenticationError as error:
|
||||
raise ConfigEntryAuthFailed(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="not_ready",
|
||||
) from error
|
||||
except (PSNAWPServerError, PSNAWPClientError) as error:
|
||||
raise ConfigEntryNotReady(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="update_failed",
|
||||
) from error
|
||||
|
||||
async def _async_update_data(self) -> PlaystationNetworkData:
|
||||
"""Get the latest data from the PSN."""
|
||||
@ -67,7 +71,7 @@ class PlaystationNetworkCoordinator(DataUpdateCoordinator[PlaystationNetworkData
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="not_ready",
|
||||
) from error
|
||||
except PSNAWPServerError as error:
|
||||
except (PSNAWPServerError, PSNAWPClientError) as error:
|
||||
raise UpdateFailed(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="update_failed",
|
||||
|
109
tests/components/playstation_network/test_init.py
Normal file
109
tests/components/playstation_network/test_init.py
Normal file
@ -0,0 +1,109 @@
|
||||
"""Tests for PlayStation Network."""
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from psnawp_api.core import (
|
||||
PSNAWPAuthenticationError,
|
||||
PSNAWPClientError,
|
||||
PSNAWPNotFoundError,
|
||||
PSNAWPServerError,
|
||||
)
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.playstation_network.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"exception", [PSNAWPNotFoundError, PSNAWPServerError, PSNAWPClientError]
|
||||
)
|
||||
async def test_config_entry_not_ready(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_psnawpapi: MagicMock,
|
||||
exception: Exception,
|
||||
) -> None:
|
||||
"""Test config entry not ready."""
|
||||
|
||||
mock_psnawpapi.user.side_effect = exception
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
|
||||
async def test_config_entry_auth_failed(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_psnawpapi: MagicMock,
|
||||
) -> None:
|
||||
"""Test config entry auth failed setup error."""
|
||||
|
||||
mock_psnawpapi.user.side_effect = PSNAWPAuthenticationError
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.SETUP_ERROR
|
||||
|
||||
flows = hass.config_entries.flow.async_progress()
|
||||
assert len(flows) == 1
|
||||
|
||||
flow = flows[0]
|
||||
assert flow.get("step_id") == "reauth_confirm"
|
||||
assert flow.get("handler") == DOMAIN
|
||||
|
||||
assert "context" in flow
|
||||
assert flow["context"].get("source") == SOURCE_REAUTH
|
||||
assert flow["context"].get("entry_id") == config_entry.entry_id
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"exception", [PSNAWPNotFoundError, PSNAWPServerError, PSNAWPClientError]
|
||||
)
|
||||
async def test_coordinator_update_data_failed(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_psnawpapi: MagicMock,
|
||||
exception: Exception,
|
||||
) -> None:
|
||||
"""Test coordinator data update failed."""
|
||||
|
||||
mock_psnawpapi.user.return_value.get_presence.side_effect = exception
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
|
||||
async def test_coordinator_update_auth_failed(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_psnawpapi: MagicMock,
|
||||
) -> None:
|
||||
"""Test coordinator update auth failed setup error."""
|
||||
|
||||
mock_psnawpapi.user.return_value.get_presence.side_effect = (
|
||||
PSNAWPAuthenticationError
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.SETUP_ERROR
|
||||
|
||||
flows = hass.config_entries.flow.async_progress()
|
||||
assert len(flows) == 1
|
||||
|
||||
flow = flows[0]
|
||||
assert flow.get("step_id") == "reauth_confirm"
|
||||
assert flow.get("handler") == DOMAIN
|
||||
|
||||
assert "context" in flow
|
||||
assert flow["context"].get("source") == SOURCE_REAUTH
|
||||
assert flow["context"].get("entry_id") == config_entry.entry_id
|
Loading…
x
Reference in New Issue
Block a user