mirror of
https://github.com/home-assistant/core.git
synced 2025-11-09 10:59:40 +00:00
Create/delete lists at runtime in Bring integration (#130098)
This commit is contained in:
@@ -3,7 +3,12 @@
|
||||
from datetime import timedelta
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from bring_api import BringAuthException, BringParseException, BringRequestException
|
||||
from bring_api import (
|
||||
BringAuthException,
|
||||
BringListResponse,
|
||||
BringParseException,
|
||||
BringRequestException,
|
||||
)
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
|
||||
@@ -16,7 +21,7 @@ from homeassistant.helpers import device_registry as dr
|
||||
|
||||
from .conftest import UUID
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed, load_fixture
|
||||
|
||||
|
||||
async def setup_integration(
|
||||
@@ -115,6 +120,25 @@ async def test_config_entry_not_ready(
|
||||
assert bring_config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
|
||||
@pytest.mark.parametrize("exception", [BringRequestException, BringParseException])
|
||||
async def test_config_entry_not_ready_udpdate_failed(
|
||||
hass: HomeAssistant,
|
||||
bring_config_entry: MockConfigEntry,
|
||||
mock_bring_client: AsyncMock,
|
||||
exception: Exception,
|
||||
) -> None:
|
||||
"""Test config entry not ready from update failed in _async_update_data."""
|
||||
mock_bring_client.load_lists.side_effect = [
|
||||
mock_bring_client.load_lists.return_value,
|
||||
exception,
|
||||
]
|
||||
bring_config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(bring_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert bring_config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("exception", "state"),
|
||||
[
|
||||
@@ -133,7 +157,10 @@ async def test_config_entry_not_ready_auth_error(
|
||||
) -> None:
|
||||
"""Test config entry not ready from authentication error."""
|
||||
|
||||
mock_bring_client.load_lists.side_effect = BringAuthException
|
||||
mock_bring_client.load_lists.side_effect = [
|
||||
mock_bring_client.load_lists.return_value,
|
||||
BringAuthException,
|
||||
]
|
||||
mock_bring_client.retrieve_new_access_token.side_effect = exception
|
||||
|
||||
bring_config_entry.add_to_hass(hass)
|
||||
@@ -170,3 +197,71 @@ async def test_coordinator_skips_deactivated(
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_bring_client.get_list.await_count == 1
|
||||
|
||||
|
||||
async def test_purge_devices(
|
||||
hass: HomeAssistant,
|
||||
bring_config_entry: MockConfigEntry,
|
||||
mock_bring_client: AsyncMock,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test removing device entry of deleted list."""
|
||||
list_uuid = "b4776778-7f6c-496e-951b-92a35d3db0dd"
|
||||
await setup_integration(hass, bring_config_entry)
|
||||
|
||||
assert bring_config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
assert device_registry.async_get_device(
|
||||
{(DOMAIN, f"{bring_config_entry.unique_id}_{list_uuid}")}
|
||||
)
|
||||
|
||||
mock_bring_client.load_lists.return_value = BringListResponse.from_json(
|
||||
load_fixture("lists2.json", DOMAIN)
|
||||
)
|
||||
|
||||
freezer.tick(timedelta(seconds=90))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert (
|
||||
device_registry.async_get_device(
|
||||
{(DOMAIN, f"{bring_config_entry.unique_id}_{list_uuid}")}
|
||||
)
|
||||
is None
|
||||
)
|
||||
|
||||
|
||||
async def test_create_devices(
|
||||
hass: HomeAssistant,
|
||||
bring_config_entry: MockConfigEntry,
|
||||
mock_bring_client: AsyncMock,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test create device entry for new lists."""
|
||||
list_uuid = "b4776778-7f6c-496e-951b-92a35d3db0dd"
|
||||
mock_bring_client.load_lists.return_value = BringListResponse.from_json(
|
||||
load_fixture("lists2.json", DOMAIN)
|
||||
)
|
||||
await setup_integration(hass, bring_config_entry)
|
||||
|
||||
assert bring_config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
assert (
|
||||
device_registry.async_get_device(
|
||||
{(DOMAIN, f"{bring_config_entry.unique_id}_{list_uuid}")}
|
||||
)
|
||||
is None
|
||||
)
|
||||
|
||||
mock_bring_client.load_lists.return_value = BringListResponse.from_json(
|
||||
load_fixture("lists.json", DOMAIN)
|
||||
)
|
||||
freezer.tick(timedelta(seconds=90))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert device_registry.async_get_device(
|
||||
{(DOMAIN, f"{bring_config_entry.unique_id}_{list_uuid}")}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user