1
0
mirror of https://github.com/home-assistant/core.git synced 2026-07-14 18:14:35 +01:00
Files
core/tests/components/squeezebox/conftest.py
T
Phill (pssc) 38f3fa0210 Add Squeezebox server service binary sensors (#122473)
* squeezebox add binary sensor + coordinator

* squeezebox add connected via for media_player

* squeezebox add Player type for player

* Add more type info

* Fix linter errors

* squeezebox use our own status entity

* squeezebox rework device handling based on freedback

* Fix device creation

* squeezebox rework coordinator error handling

* Fix lint type error

* Correct spelling

* Correct spelling

* remove large comments

* insert small comment

* add translation support

* Simply sensor

* clean update function, minimise comments to the useful bits

* Fix after testing

* Update homeassistant/components/squeezebox/entity.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* move data prep out of Device assign for clarity

* stop being a generic api

* Humans need to read the sensors...

* ruff format

* Humans need to read the sensors...

* Revert "ruff format"

This reverts commit 8fcb8143e7.

* ruff format

* Humans need to read the sensors...

* errors after testing

* infered

* drop context

* cutdown coordinator for the binary sensors

* add tests for binary sensors

* Fix import

* add some basic media_player tests

* Fix spelling and file headers

* Fix spelling

* remove uuid and use service device cat

* use diag device

* assert execpted value

* ruff format

* Update homeassistant/components/squeezebox/__init__.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Simplify T/F

* Fix file header

* remove redudant check

* remove player tests from this commit

* Fix formatting

* remove unused

* Fix function Type

* Fix Any to bool

* Fix browser tests

* Patch our squeebox componemt not the server in the lib

* ruff

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-09-05 16:49:07 +02:00

135 lines
4.1 KiB
Python

"""Setup the squeezebox tests."""
from collections.abc import Generator
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from homeassistant.components.media_player import MediaType
from homeassistant.components.squeezebox import const
from homeassistant.components.squeezebox.browse_media import (
MEDIA_TYPE_TO_SQUEEZEBOX,
SQUEEZEBOX_ID_BY_TYPE,
)
from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import format_mac
from tests.common import MockConfigEntry
TEST_HOST = "1.2.3.4"
TEST_PORT = "9000"
TEST_USE_HTTPS = False
SERVER_UUID = "12345678-1234-1234-1234-123456789012"
TEST_MAC = "aa:bb:cc:dd:ee:ff"
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock]:
"""Override async_setup_entry."""
with patch(
"homeassistant.components.squeezebox.async_setup_entry", return_value=True
) as mock_setup_entry:
yield mock_setup_entry
@pytest.fixture
def config_entry(hass: HomeAssistant) -> MockConfigEntry:
"""Add the squeezebox mock config entry to hass."""
config_entry = MockConfigEntry(
domain=const.DOMAIN,
unique_id=SERVER_UUID,
data={
CONF_HOST: TEST_HOST,
CONF_PORT: TEST_PORT,
const.CONF_HTTPS: TEST_USE_HTTPS,
},
)
config_entry.add_to_hass(hass)
return config_entry
async def mock_async_browse(
media_type: MediaType, limit: int, browse_id: tuple | None = None
) -> dict | None:
"""Mock the async_browse method of pysqueezebox.Player."""
child_types = {
"favorites": "favorites",
"albums": "album",
"album": "track",
"genres": "genre",
"genre": "album",
"artists": "artist",
"artist": "album",
"titles": "title",
"title": "title",
"playlists": "playlist",
"playlist": "title",
}
fake_items = [
{
"title": "Fake Item 1",
"id": "1234",
"hasitems": False,
"item_type": child_types[media_type],
"artwork_track_id": "b35bb9e9",
},
{
"title": "Fake Item 2",
"id": "12345",
"hasitems": media_type == "favorites",
"item_type": child_types[media_type],
"image_url": "http://lms.internal:9000/html/images/favorites.png",
},
{
"title": "Fake Item 3",
"id": "123456",
"hasitems": media_type == "favorites",
"album_id": "123456" if media_type == "favorites" else None,
},
]
if browse_id:
search_type, search_id = browse_id
if search_id:
if search_type in SQUEEZEBOX_ID_BY_TYPE.values():
for item in fake_items:
if item["id"] == search_id:
return {
"title": item["title"],
"items": [item],
}
return None
if search_type in SQUEEZEBOX_ID_BY_TYPE.values():
return {
"title": search_type,
"items": fake_items,
}
return None
if media_type in MEDIA_TYPE_TO_SQUEEZEBOX.values():
return {
"title": media_type,
"items": fake_items,
}
return None
@pytest.fixture
def lms() -> MagicMock:
"""Mock a Lyrion Media Server with one mock player attached."""
lms = MagicMock()
player = MagicMock()
player.player_id = TEST_MAC
player.name = "Test Player"
player.power = False
player.async_browse = AsyncMock(side_effect=mock_async_browse)
player.async_load_playlist = AsyncMock()
player.async_update = AsyncMock()
player.generate_image_url_from_track_id = MagicMock(
return_value="http://lms.internal:9000/html/images/favorites.png"
)
lms.async_get_players = AsyncMock(return_value=[player])
lms.async_query = AsyncMock(return_value={"uuid": format_mac(TEST_MAC)})
lms.async_status = AsyncMock(return_value={"uuid": format_mac(TEST_MAC)})
return lms