diff --git a/homeassistant/components/bang_olufsen/entity.py b/homeassistant/components/bang_olufsen/entity.py index 76d93ca0635..bf6966f83bf 100644 --- a/homeassistant/components/bang_olufsen/entity.py +++ b/homeassistant/components/bang_olufsen/entity.py @@ -36,7 +36,6 @@ class BangOlufsenBase: # Set the configuration variables. self._host: str = self.entry.data[CONF_HOST] - self._name: str = self.entry.title self._unique_id: str = cast(str, self.entry.unique_id) # Objects that get directly updated by notifications. @@ -54,15 +53,13 @@ class BangOlufsenEntity(Entity, BangOlufsenBase): """Base Entity for BangOlufsen entities.""" _attr_has_entity_name = True + _attr_should_poll = False def __init__(self, entry: ConfigEntry, client: MozartClient) -> None: """Initialize the object.""" super().__init__(entry, client) self._attr_device_info = DeviceInfo(identifiers={(DOMAIN, self._unique_id)}) - self._attr_device_class = None - self._attr_entity_category = None - self._attr_should_poll = False async def _update_connection_state(self, connection_state: bool) -> None: """Update entity connection state.""" diff --git a/homeassistant/components/bang_olufsen/media_player.py b/homeassistant/components/bang_olufsen/media_player.py index eaafddcabd6..3209c676af7 100644 --- a/homeassistant/components/bang_olufsen/media_player.py +++ b/homeassistant/components/bang_olufsen/media_player.py @@ -94,11 +94,12 @@ async def async_setup_entry( async_add_entities(new_entities=[BangOlufsenMediaPlayer(config_entry, data.client)]) -class BangOlufsenMediaPlayer(MediaPlayerEntity, BangOlufsenEntity): +class BangOlufsenMediaPlayer(BangOlufsenEntity, MediaPlayerEntity): """Representation of a media player.""" - _attr_has_entity_name = False _attr_icon = "mdi:speaker-wireless" + _attr_name = None + _attr_device_class = MediaPlayerDeviceClass.SPEAKER _attr_supported_features = BANG_OLUFSEN_FEATURES def __init__(self, entry: ConfigEntry, client: MozartClient) -> None: @@ -113,12 +114,9 @@ class BangOlufsenMediaPlayer(MediaPlayerEntity, BangOlufsenEntity): identifiers={(DOMAIN, self._unique_id)}, manufacturer="Bang & Olufsen", model=self._model, - name=cast(str, self.name), serial_number=self._unique_id, ) - self._attr_name = self._name self._attr_unique_id = self._unique_id - self._attr_device_class = MediaPlayerDeviceClass.SPEAKER # Misc. variables. self._audio_sources: dict[str, str] = {} diff --git a/homeassistant/components/bang_olufsen/websocket.py b/homeassistant/components/bang_olufsen/websocket.py index fd378a40bd3..81de5596e31 100644 --- a/homeassistant/components/bang_olufsen/websocket.py +++ b/homeassistant/components/bang_olufsen/websocket.py @@ -80,12 +80,12 @@ class BangOlufsenWebsocket(BangOlufsenBase): def on_connection(self) -> None: """Handle WebSocket connection made.""" - _LOGGER.debug("Connected to the %s notification channel", self._name) + _LOGGER.debug("Connected to the %s notification channel", self.entry.title) self._update_connection_status() def on_connection_lost(self) -> None: """Handle WebSocket connection lost.""" - _LOGGER.error("Lost connection to the %s", self._name) + _LOGGER.error("Lost connection to the %s", self.entry.title) self._update_connection_status() def on_notification_notification( diff --git a/tests/components/bang_olufsen/conftest.py b/tests/components/bang_olufsen/conftest.py index 8c212ef16be..e714f55de4a 100644 --- a/tests/components/bang_olufsen/conftest.py +++ b/tests/components/bang_olufsen/conftest.py @@ -1,5 +1,5 @@ """Test fixtures for bang_olufsen.""" - +from collections.abc import Generator from unittest.mock import AsyncMock, patch from mozart_api.models import BeolinkPeer @@ -18,25 +18,6 @@ from .const import ( from tests.common import MockConfigEntry -class MockMozartClient: - """Class for mocking MozartClient objects and methods.""" - - async def __aenter__(self): - """Mock async context entry.""" - - async def __aexit__(self, exc_type, exc, tb): - """Mock async context exit.""" - - # API call results - get_beolink_self_result = BeolinkPeer( - friendly_name=TEST_FRIENDLY_NAME, jid=TEST_JID_1 - ) - - # API endpoints - get_beolink_self = AsyncMock() - get_beolink_self.return_value = get_beolink_self_result - - @pytest.fixture def mock_config_entry(): """Mock config entry.""" @@ -49,17 +30,22 @@ def mock_config_entry(): @pytest.fixture -def mock_client(): +def mock_mozart_client() -> Generator[AsyncMock, None, None]: """Mock MozartClient.""" - client = MockMozartClient() - - with patch("mozart_api.mozart_client.MozartClient", return_value=client): + with patch( + "homeassistant.components.bang_olufsen.MozartClient", autospec=True + ) as mock_client, patch( + "homeassistant.components.bang_olufsen.config_flow.MozartClient", + new=mock_client, + ): + client = mock_client.return_value + client.get_beolink_self = AsyncMock() + client.get_beolink_self.return_value = BeolinkPeer( + friendly_name=TEST_FRIENDLY_NAME, jid=TEST_JID_1 + ) yield client - # Reset mocked API call counts and side effects - client.get_beolink_self.reset_mock(side_effect=True) - @pytest.fixture def mock_setup_entry(): diff --git a/tests/components/bang_olufsen/test_config_flow.py b/tests/components/bang_olufsen/test_config_flow.py index dd42c4c5c8c..793ef0c2c9b 100644 --- a/tests/components/bang_olufsen/test_config_flow.py +++ b/tests/components/bang_olufsen/test_config_flow.py @@ -13,7 +13,6 @@ from homeassistant.const import CONF_SOURCE from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType -from .conftest import MockMozartClient from .const import ( TEST_DATA_CREATE_ENTRY, TEST_DATA_USER, @@ -27,10 +26,10 @@ pytestmark = pytest.mark.usefixtures("mock_setup_entry") async def test_config_flow_timeout_error( - hass: HomeAssistant, mock_client: MockMozartClient + hass: HomeAssistant, mock_mozart_client ) -> None: """Test we handle timeout_error.""" - mock_client.get_beolink_self.side_effect = TimeoutError() + mock_mozart_client.get_beolink_self.side_effect = TimeoutError() result_user = await hass.config_entries.flow.async_init( handler=DOMAIN, @@ -40,14 +39,16 @@ async def test_config_flow_timeout_error( assert result_user["type"] == FlowResultType.FORM assert result_user["errors"] == {"base": "timeout_error"} - assert mock_client.get_beolink_self.call_count == 1 + assert mock_mozart_client.get_beolink_self.call_count == 1 async def test_config_flow_client_connector_error( - hass: HomeAssistant, mock_client: MockMozartClient + hass: HomeAssistant, mock_mozart_client ) -> None: """Test we handle client_connector_error.""" - mock_client.get_beolink_self.side_effect = ClientConnectorError(Mock(), Mock()) + mock_mozart_client.get_beolink_self.side_effect = ClientConnectorError( + Mock(), Mock() + ) result_user = await hass.config_entries.flow.async_init( handler=DOMAIN, @@ -57,7 +58,7 @@ async def test_config_flow_client_connector_error( assert result_user["type"] == FlowResultType.FORM assert result_user["errors"] == {"base": "client_connector_error"} - assert mock_client.get_beolink_self.call_count == 1 + assert mock_mozart_client.get_beolink_self.call_count == 1 async def test_config_flow_invalid_ip(hass: HomeAssistant) -> None: @@ -73,10 +74,10 @@ async def test_config_flow_invalid_ip(hass: HomeAssistant) -> None: async def test_config_flow_api_exception( - hass: HomeAssistant, mock_client: MockMozartClient + hass: HomeAssistant, mock_mozart_client ) -> None: """Test we handle api_exception.""" - mock_client.get_beolink_self.side_effect = ApiException() + mock_mozart_client.get_beolink_self.side_effect = ApiException() result_user = await hass.config_entries.flow.async_init( handler=DOMAIN, @@ -86,10 +87,10 @@ async def test_config_flow_api_exception( assert result_user["type"] == FlowResultType.FORM assert result_user["errors"] == {"base": "api_exception"} - assert mock_client.get_beolink_self.call_count == 1 + assert mock_mozart_client.get_beolink_self.call_count == 1 -async def test_config_flow(hass: HomeAssistant, mock_client: MockMozartClient) -> None: +async def test_config_flow(hass: HomeAssistant, mock_mozart_client) -> None: """Test config flow.""" result_init = await hass.config_entries.flow.async_init( @@ -109,12 +110,10 @@ async def test_config_flow(hass: HomeAssistant, mock_client: MockMozartClient) - assert result_user["type"] == FlowResultType.CREATE_ENTRY assert result_user["data"] == TEST_DATA_CREATE_ENTRY - assert mock_client.get_beolink_self.call_count == 1 + assert mock_mozart_client.get_beolink_self.call_count == 1 -async def test_config_flow_zeroconf( - hass: HomeAssistant, mock_client: MockMozartClient -) -> None: +async def test_config_flow_zeroconf(hass: HomeAssistant, mock_mozart_client) -> None: """Test zeroconf discovery.""" result_zeroconf = await hass.config_entries.flow.async_init( @@ -134,7 +133,7 @@ async def test_config_flow_zeroconf( assert result_confirm["type"] == FlowResultType.CREATE_ENTRY assert result_confirm["data"] == TEST_DATA_CREATE_ENTRY - assert mock_client.get_beolink_self.call_count == 0 + assert mock_mozart_client.get_beolink_self.call_count == 0 async def test_config_flow_zeroconf_not_mozart_device(hass: HomeAssistant) -> None: