diff --git a/.coveragerc b/.coveragerc index 4d0f78a81f5..303f9696fe3 100644 --- a/.coveragerc +++ b/.coveragerc @@ -130,7 +130,6 @@ omit = homeassistant/components/baf/sensor.py homeassistant/components/baf/switch.py homeassistant/components/baidu/tts.py - homeassistant/components/bang_olufsen/__init__.py homeassistant/components/bang_olufsen/entity.py homeassistant/components/bang_olufsen/media_player.py homeassistant/components/bang_olufsen/util.py diff --git a/tests/components/bang_olufsen/conftest.py b/tests/components/bang_olufsen/conftest.py index e77dc4d16a9..1fbcbe0fe69 100644 --- a/tests/components/bang_olufsen/conftest.py +++ b/tests/components/bang_olufsen/conftest.py @@ -1,10 +1,10 @@ """Test fixtures for bang_olufsen.""" -from unittest.mock import AsyncMock, patch +from collections.abc import Generator +from unittest.mock import AsyncMock, Mock, patch from mozart_api.models import BeolinkPeer import pytest -from typing_extensions import Generator from homeassistant.components.bang_olufsen.const import DOMAIN @@ -44,10 +44,19 @@ def mock_mozart_client() -> Generator[AsyncMock]: ), ): client = mock_client.return_value + + # REST API client methods client.get_beolink_self = AsyncMock() client.get_beolink_self.return_value = BeolinkPeer( friendly_name=TEST_FRIENDLY_NAME, jid=TEST_JID_1 ) + + # Non-REST API client methods + client.check_device_connection = AsyncMock() + client.close_api_client = AsyncMock() + client.connect_notifications = AsyncMock() + client.disconnect_notifications = Mock() + yield client diff --git a/tests/components/bang_olufsen/test_init.py b/tests/components/bang_olufsen/test_init.py new file mode 100644 index 00000000000..11742b846ae --- /dev/null +++ b/tests/components/bang_olufsen/test_init.py @@ -0,0 +1,90 @@ +"""Test the bang_olufsen __init__.""" + +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 + +from .const import TEST_MODEL_BALANCE, TEST_NAME, TEST_SERIAL_NUMBER + + +async def test_setup_entry( + hass: HomeAssistant, + mock_config_entry, + mock_mozart_client, + device_registry: DeviceRegistry, +) -> 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 + assert device.name == TEST_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, mock_mozart_client +) -> 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, mock_mozart_client +) -> 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 + + # 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 mock_config_entry.entry_id not in hass.data[DOMAIN] + assert mock_config_entry.state == ConfigEntryState.NOT_LOADED