Refactor Xbox integration setup and exception handling (#154823)

This commit is contained in:
Manu
2025-10-19 22:18:56 +02:00
committed by GitHub
parent 711526fc6c
commit d4e72ad2cf
5 changed files with 133 additions and 35 deletions
+3 -3
View File
@@ -38,7 +38,7 @@ async def setup_credentials(hass: HomeAssistant) -> None:
def mock_oauth2_implementation() -> Generator[AsyncMock]:
"""Mock config entry oauth2 implementation."""
with patch(
"homeassistant.components.xbox.config_entry_oauth2_flow.async_get_config_entry_implementation",
"homeassistant.components.xbox.coordinator.config_entry_oauth2_flow.async_get_config_entry_implementation",
return_value=AsyncMock(),
) as mock_client:
client = mock_client.return_value
@@ -89,7 +89,7 @@ def mock_signed_session() -> Generator[AsyncMock]:
with (
patch(
"homeassistant.components.xbox.SignedSession", autospec=True
"homeassistant.components.xbox.coordinator.SignedSession", autospec=True
) as mock_client,
patch(
"homeassistant.components.xbox.config_flow.SignedSession", new=mock_client
@@ -106,7 +106,7 @@ def mock_xbox_live_client(signed_session) -> Generator[AsyncMock]:
with (
patch(
"homeassistant.components.xbox.XboxLiveClient", autospec=True
"homeassistant.components.xbox.coordinator.XboxLiveClient", autospec=True
) as mock_client,
patch(
"homeassistant.components.xbox.config_flow.XboxLiveClient", new=mock_client
+66
View File
@@ -0,0 +1,66 @@
"""Tests for the Xbox integration."""
from unittest.mock import AsyncMock, patch
from httpx import ConnectTimeout, HTTPStatusError, ProtocolError
import pytest
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
@pytest.mark.usefixtures("xbox_live_client")
async def test_entry_setup_unload(
hass: HomeAssistant, config_entry: MockConfigEntry
) -> None:
"""Test integration setup and unload."""
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.LOADED
assert await hass.config_entries.async_unload(config_entry.entry_id)
assert config_entry.state is ConfigEntryState.NOT_LOADED
@pytest.mark.parametrize(
"exception",
[ConnectTimeout, HTTPStatusError, ProtocolError],
)
async def test_config_entry_not_ready(
hass: HomeAssistant,
config_entry: MockConfigEntry,
xbox_live_client: AsyncMock,
exception: Exception,
) -> None:
"""Test config entry not ready."""
xbox_live_client.smartglass.get_console_list.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
@pytest.mark.usefixtures("xbox_live_client")
async def test_config_implementation_not_available(
hass: HomeAssistant,
config_entry: MockConfigEntry,
) -> None:
"""Test implementation not available."""
config_entry.add_to_hass(hass)
with patch(
"homeassistant.components.xbox.coordinator.config_entry_oauth2_flow.async_get_config_entry_implementation",
side_effect=ValueError("Implementation not available"),
):
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.SETUP_RETRY