core/tests/components/bang_olufsen/test_init.py

101 lines
3.5 KiB
Python
Raw Normal View History

"""Test the bang_olufsen __init__."""
from unittest.mock import AsyncMock
from aiohttp.client_exceptions import ServerTimeoutError
from homeassistant.components.bang_olufsen import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceRegistry
Bang & Olufsen add beolink grouping (#113438) * Add Beolink custom services Add support for media player grouping via beolink Give media player entity name * Fix progress not being set to None as Beolink listener Revert naming changes * Update API simplify Beolink attributes * Improve beolink custom services * Fix Beolink expandable source check Add unexpand return value Set entity name on initialization * Handle entity naming as intended * Fix "null" Beolink self friendly name * Add regex service input validation Add all_discovered to beolink_expand service Improve beolink_expand response * Add service icons * Fix merge Remove unnecessary assignment * Remove invalid typing Update response typing for updated API * Revert to old typed response dict method Remove mypy ignore line Fix jid possibly used before assignment * Re add debugging logging * Fix coroutine Fix formatting * Remove unnecessary update control * Make tests pass Fix remote leader media position bug Improve remote leader BangOlufsenSource comparison * Fix naming and add callback decorators * Move regex service check to variable Suppress KeyError Update tests * Re-add hass running check * Improve comments, naming and type hinting * Remove old temporary fix * Convert logged warning to raised exception for invalid media_player Simplify code using walrus operator * Fix test for invalid media_player grouping * Improve method naming * Improve _beolink_sources explanation * Improve _beolink_sources explanation * Fix tests * Remove service responses Fix and add tests * Change service to action where applicable * Show playback progress for listeners * Fix testing * Remove useless initialization * Fix allstandby name * Fix various casts with assertions Fix comment placement Fix group leader group_members rebase error Replace entity_id method call with attribute * Add syrupy snapshots for Beolink tests, checking entity states Use test JIDs 3 and 4 instead of 2 and 3 to avoid invalid attributes in testing * Add sections for fields using Beolink JIDs directly * Fix typo * FIx rebase mistake * Sort actions alphabetically
2024-11-08 11:06:29 +00:00
from .const import TEST_FRIENDLY_NAME, TEST_MODEL_BALANCE, TEST_SERIAL_NUMBER
from tests.common import MockConfigEntry
async def test_setup_entry(
hass: HomeAssistant,
device_registry: DeviceRegistry,
mock_config_entry: MockConfigEntry,
mock_mozart_client: AsyncMock,
) -> None:
"""Test async_setup_entry."""
assert mock_config_entry.state == ConfigEntryState.NOT_LOADED
# Load entry
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
assert mock_config_entry.state == ConfigEntryState.LOADED
# Check that the device has been registered properly
device = device_registry.async_get_device(
identifiers={(DOMAIN, TEST_SERIAL_NUMBER)}
)
assert device is not None
Bang & Olufsen add beolink grouping (#113438) * Add Beolink custom services Add support for media player grouping via beolink Give media player entity name * Fix progress not being set to None as Beolink listener Revert naming changes * Update API simplify Beolink attributes * Improve beolink custom services * Fix Beolink expandable source check Add unexpand return value Set entity name on initialization * Handle entity naming as intended * Fix "null" Beolink self friendly name * Add regex service input validation Add all_discovered to beolink_expand service Improve beolink_expand response * Add service icons * Fix merge Remove unnecessary assignment * Remove invalid typing Update response typing for updated API * Revert to old typed response dict method Remove mypy ignore line Fix jid possibly used before assignment * Re add debugging logging * Fix coroutine Fix formatting * Remove unnecessary update control * Make tests pass Fix remote leader media position bug Improve remote leader BangOlufsenSource comparison * Fix naming and add callback decorators * Move regex service check to variable Suppress KeyError Update tests * Re-add hass running check * Improve comments, naming and type hinting * Remove old temporary fix * Convert logged warning to raised exception for invalid media_player Simplify code using walrus operator * Fix test for invalid media_player grouping * Improve method naming * Improve _beolink_sources explanation * Improve _beolink_sources explanation * Fix tests * Remove service responses Fix and add tests * Change service to action where applicable * Show playback progress for listeners * Fix testing * Remove useless initialization * Fix allstandby name * Fix various casts with assertions Fix comment placement Fix group leader group_members rebase error Replace entity_id method call with attribute * Add syrupy snapshots for Beolink tests, checking entity states Use test JIDs 3 and 4 instead of 2 and 3 to avoid invalid attributes in testing * Add sections for fields using Beolink JIDs directly * Fix typo * FIx rebase mistake * Sort actions alphabetically
2024-11-08 11:06:29 +00:00
# Is usually TEST_NAME, but is updated to the device's friendly name by _update_name_and_beolink
assert device.name == TEST_FRIENDLY_NAME
assert device.model == TEST_MODEL_BALANCE
# Ensure that the connection has been checked WebSocket connection has been initialized
assert mock_mozart_client.check_device_connection.call_count == 1
assert mock_mozart_client.close_api_client.call_count == 0
assert mock_mozart_client.connect_notifications.call_count == 1
async def test_setup_entry_failed(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_mozart_client: AsyncMock,
) -> None:
"""Test failed async_setup_entry."""
# Set the device connection check to fail
mock_mozart_client.check_device_connection.side_effect = ExceptionGroup(
"", (ServerTimeoutError(), TimeoutError())
)
assert mock_config_entry.state == ConfigEntryState.NOT_LOADED
# Load entry
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
assert mock_config_entry.state == ConfigEntryState.SETUP_RETRY
# Ensure that the connection has been checked, API client correctly closed
# and WebSocket connection has not been initialized
assert mock_mozart_client.check_device_connection.call_count == 1
assert mock_mozart_client.close_api_client.call_count == 1
assert mock_mozart_client.connect_notifications.call_count == 0
async def test_unload_entry(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_mozart_client: AsyncMock,
) -> None:
"""Test unload_entry."""
# Load entry
assert mock_config_entry.state == ConfigEntryState.NOT_LOADED
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
assert mock_config_entry.state == ConfigEntryState.LOADED
assert hasattr(mock_config_entry, "runtime_data")
# Unload entry
await hass.config_entries.async_unload(mock_config_entry.entry_id)
# Ensure WebSocket notification listener and REST API client have been closed
assert mock_mozart_client.disconnect_notifications.call_count == 1
assert mock_mozart_client.close_api_client.call_count == 1
# Ensure that the entry is not loaded and has been removed from hass
assert not hasattr(mock_config_entry, "runtime_data")
assert mock_config_entry.state == ConfigEntryState.NOT_LOADED