mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Fix Bang and olufsen naming (#111344)
* Fix Bang and olufsen naming * Fix * Fix tests * Fix tests
This commit is contained in:
parent
f3594c543d
commit
c1d45f63f5
@ -36,7 +36,6 @@ class BangOlufsenBase:
|
|||||||
|
|
||||||
# Set the configuration variables.
|
# Set the configuration variables.
|
||||||
self._host: str = self.entry.data[CONF_HOST]
|
self._host: str = self.entry.data[CONF_HOST]
|
||||||
self._name: str = self.entry.title
|
|
||||||
self._unique_id: str = cast(str, self.entry.unique_id)
|
self._unique_id: str = cast(str, self.entry.unique_id)
|
||||||
|
|
||||||
# Objects that get directly updated by notifications.
|
# Objects that get directly updated by notifications.
|
||||||
@ -54,15 +53,13 @@ class BangOlufsenEntity(Entity, BangOlufsenBase):
|
|||||||
"""Base Entity for BangOlufsen entities."""
|
"""Base Entity for BangOlufsen entities."""
|
||||||
|
|
||||||
_attr_has_entity_name = True
|
_attr_has_entity_name = True
|
||||||
|
_attr_should_poll = False
|
||||||
|
|
||||||
def __init__(self, entry: ConfigEntry, client: MozartClient) -> None:
|
def __init__(self, entry: ConfigEntry, client: MozartClient) -> None:
|
||||||
"""Initialize the object."""
|
"""Initialize the object."""
|
||||||
super().__init__(entry, client)
|
super().__init__(entry, client)
|
||||||
|
|
||||||
self._attr_device_info = DeviceInfo(identifiers={(DOMAIN, self._unique_id)})
|
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:
|
async def _update_connection_state(self, connection_state: bool) -> None:
|
||||||
"""Update entity connection state."""
|
"""Update entity connection state."""
|
||||||
|
@ -94,11 +94,12 @@ async def async_setup_entry(
|
|||||||
async_add_entities(new_entities=[BangOlufsenMediaPlayer(config_entry, data.client)])
|
async_add_entities(new_entities=[BangOlufsenMediaPlayer(config_entry, data.client)])
|
||||||
|
|
||||||
|
|
||||||
class BangOlufsenMediaPlayer(MediaPlayerEntity, BangOlufsenEntity):
|
class BangOlufsenMediaPlayer(BangOlufsenEntity, MediaPlayerEntity):
|
||||||
"""Representation of a media player."""
|
"""Representation of a media player."""
|
||||||
|
|
||||||
_attr_has_entity_name = False
|
|
||||||
_attr_icon = "mdi:speaker-wireless"
|
_attr_icon = "mdi:speaker-wireless"
|
||||||
|
_attr_name = None
|
||||||
|
_attr_device_class = MediaPlayerDeviceClass.SPEAKER
|
||||||
_attr_supported_features = BANG_OLUFSEN_FEATURES
|
_attr_supported_features = BANG_OLUFSEN_FEATURES
|
||||||
|
|
||||||
def __init__(self, entry: ConfigEntry, client: MozartClient) -> None:
|
def __init__(self, entry: ConfigEntry, client: MozartClient) -> None:
|
||||||
@ -113,12 +114,9 @@ class BangOlufsenMediaPlayer(MediaPlayerEntity, BangOlufsenEntity):
|
|||||||
identifiers={(DOMAIN, self._unique_id)},
|
identifiers={(DOMAIN, self._unique_id)},
|
||||||
manufacturer="Bang & Olufsen",
|
manufacturer="Bang & Olufsen",
|
||||||
model=self._model,
|
model=self._model,
|
||||||
name=cast(str, self.name),
|
|
||||||
serial_number=self._unique_id,
|
serial_number=self._unique_id,
|
||||||
)
|
)
|
||||||
self._attr_name = self._name
|
|
||||||
self._attr_unique_id = self._unique_id
|
self._attr_unique_id = self._unique_id
|
||||||
self._attr_device_class = MediaPlayerDeviceClass.SPEAKER
|
|
||||||
|
|
||||||
# Misc. variables.
|
# Misc. variables.
|
||||||
self._audio_sources: dict[str, str] = {}
|
self._audio_sources: dict[str, str] = {}
|
||||||
|
@ -80,12 +80,12 @@ class BangOlufsenWebsocket(BangOlufsenBase):
|
|||||||
|
|
||||||
def on_connection(self) -> None:
|
def on_connection(self) -> None:
|
||||||
"""Handle WebSocket connection made."""
|
"""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()
|
self._update_connection_status()
|
||||||
|
|
||||||
def on_connection_lost(self) -> None:
|
def on_connection_lost(self) -> None:
|
||||||
"""Handle WebSocket connection lost."""
|
"""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()
|
self._update_connection_status()
|
||||||
|
|
||||||
def on_notification_notification(
|
def on_notification_notification(
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
"""Test fixtures for bang_olufsen."""
|
"""Test fixtures for bang_olufsen."""
|
||||||
|
from collections.abc import Generator
|
||||||
from unittest.mock import AsyncMock, patch
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
from mozart_api.models import BeolinkPeer
|
from mozart_api.models import BeolinkPeer
|
||||||
@ -18,25 +18,6 @@ from .const import (
|
|||||||
from tests.common import MockConfigEntry
|
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
|
@pytest.fixture
|
||||||
def mock_config_entry():
|
def mock_config_entry():
|
||||||
"""Mock config entry."""
|
"""Mock config entry."""
|
||||||
@ -49,17 +30,22 @@ def mock_config_entry():
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_client():
|
def mock_mozart_client() -> Generator[AsyncMock, None, None]:
|
||||||
"""Mock MozartClient."""
|
"""Mock MozartClient."""
|
||||||
|
|
||||||
client = MockMozartClient()
|
with patch(
|
||||||
|
"homeassistant.components.bang_olufsen.MozartClient", autospec=True
|
||||||
with patch("mozart_api.mozart_client.MozartClient", return_value=client):
|
) 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
|
yield client
|
||||||
|
|
||||||
# Reset mocked API call counts and side effects
|
|
||||||
client.get_beolink_self.reset_mock(side_effect=True)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_setup_entry():
|
def mock_setup_entry():
|
||||||
|
@ -13,7 +13,6 @@ from homeassistant.const import CONF_SOURCE
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import FlowResultType
|
from homeassistant.data_entry_flow import FlowResultType
|
||||||
|
|
||||||
from .conftest import MockMozartClient
|
|
||||||
from .const import (
|
from .const import (
|
||||||
TEST_DATA_CREATE_ENTRY,
|
TEST_DATA_CREATE_ENTRY,
|
||||||
TEST_DATA_USER,
|
TEST_DATA_USER,
|
||||||
@ -27,10 +26,10 @@ pytestmark = pytest.mark.usefixtures("mock_setup_entry")
|
|||||||
|
|
||||||
|
|
||||||
async def test_config_flow_timeout_error(
|
async def test_config_flow_timeout_error(
|
||||||
hass: HomeAssistant, mock_client: MockMozartClient
|
hass: HomeAssistant, mock_mozart_client
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test we handle timeout_error."""
|
"""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(
|
result_user = await hass.config_entries.flow.async_init(
|
||||||
handler=DOMAIN,
|
handler=DOMAIN,
|
||||||
@ -40,14 +39,16 @@ async def test_config_flow_timeout_error(
|
|||||||
assert result_user["type"] == FlowResultType.FORM
|
assert result_user["type"] == FlowResultType.FORM
|
||||||
assert result_user["errors"] == {"base": "timeout_error"}
|
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(
|
async def test_config_flow_client_connector_error(
|
||||||
hass: HomeAssistant, mock_client: MockMozartClient
|
hass: HomeAssistant, mock_mozart_client
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test we handle client_connector_error."""
|
"""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(
|
result_user = await hass.config_entries.flow.async_init(
|
||||||
handler=DOMAIN,
|
handler=DOMAIN,
|
||||||
@ -57,7 +58,7 @@ async def test_config_flow_client_connector_error(
|
|||||||
assert result_user["type"] == FlowResultType.FORM
|
assert result_user["type"] == FlowResultType.FORM
|
||||||
assert result_user["errors"] == {"base": "client_connector_error"}
|
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:
|
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(
|
async def test_config_flow_api_exception(
|
||||||
hass: HomeAssistant, mock_client: MockMozartClient
|
hass: HomeAssistant, mock_mozart_client
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test we handle api_exception."""
|
"""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(
|
result_user = await hass.config_entries.flow.async_init(
|
||||||
handler=DOMAIN,
|
handler=DOMAIN,
|
||||||
@ -86,10 +87,10 @@ async def test_config_flow_api_exception(
|
|||||||
assert result_user["type"] == FlowResultType.FORM
|
assert result_user["type"] == FlowResultType.FORM
|
||||||
assert result_user["errors"] == {"base": "api_exception"}
|
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."""
|
"""Test config flow."""
|
||||||
|
|
||||||
result_init = await hass.config_entries.flow.async_init(
|
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["type"] == FlowResultType.CREATE_ENTRY
|
||||||
assert result_user["data"] == TEST_DATA_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(
|
async def test_config_flow_zeroconf(hass: HomeAssistant, mock_mozart_client) -> None:
|
||||||
hass: HomeAssistant, mock_client: MockMozartClient
|
|
||||||
) -> None:
|
|
||||||
"""Test zeroconf discovery."""
|
"""Test zeroconf discovery."""
|
||||||
|
|
||||||
result_zeroconf = await hass.config_entries.flow.async_init(
|
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["type"] == FlowResultType.CREATE_ENTRY
|
||||||
assert result_confirm["data"] == TEST_DATA_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:
|
async def test_config_flow_zeroconf_not_mozart_device(hass: HomeAssistant) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user