Fix initialisation of Apps and Radios list for Squeezebox (#149834)

This commit is contained in:
peteS-UK 2025-08-02 19:01:02 +01:00 committed by GitHub
parent 018197e41a
commit fa476d4e34
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 46 additions and 19 deletions

View File

@ -4,6 +4,7 @@ from __future__ import annotations
import contextlib import contextlib
from dataclasses import dataclass, field from dataclasses import dataclass, field
import logging
from typing import Any from typing import Any
from pysqueezebox import Player from pysqueezebox import Player
@ -21,6 +22,8 @@ from homeassistant.helpers.network import is_internal_request
from .const import DOMAIN, UNPLAYABLE_TYPES from .const import DOMAIN, UNPLAYABLE_TYPES
_LOGGER = logging.getLogger(__name__)
LIBRARY = [ LIBRARY = [
"favorites", "favorites",
"artists", "artists",
@ -138,18 +141,42 @@ class BrowseData:
self.squeezebox_id_by_type.update(SQUEEZEBOX_ID_BY_TYPE) self.squeezebox_id_by_type.update(SQUEEZEBOX_ID_BY_TYPE)
self.media_type_to_squeezebox.update(MEDIA_TYPE_TO_SQUEEZEBOX) self.media_type_to_squeezebox.update(MEDIA_TYPE_TO_SQUEEZEBOX)
def add_new_command(self, cmd: str | MediaType, type: str) -> None:
"""Add items to maps for new apps or radios."""
self.known_apps_radios.add(cmd)
self.media_type_to_squeezebox[cmd] = cmd
self.squeezebox_id_by_type[cmd] = type
self.content_type_media_class[cmd] = {
"item": MediaClass.DIRECTORY,
"children": MediaClass.TRACK,
}
self.content_type_to_child_type[cmd] = MediaType.TRACK
def _add_new_command_to_browse_data( async def async_init(self, player: Player, browse_limit: int) -> None:
browse_data: BrowseData, cmd: str | MediaType, type: str """Initialize known apps and radios from the player."""
) -> None:
"""Add items to maps for new apps or radios.""" cmd = ["apps", 0, browse_limit]
browse_data.media_type_to_squeezebox[cmd] = cmd result = await player.async_query(*cmd)
browse_data.squeezebox_id_by_type[cmd] = type for app in result["appss_loop"]:
browse_data.content_type_media_class[cmd] = { app_cmd = "app-" + app["cmd"]
"item": MediaClass.DIRECTORY, if app_cmd not in self.known_apps_radios:
"children": MediaClass.TRACK, self.add_new_command(app_cmd, "item_id")
} _LOGGER.debug(
browse_data.content_type_to_child_type[cmd] = MediaType.TRACK "Adding new command %s to browse data for player %s",
app_cmd,
player.player_id,
)
cmd = ["radios", 0, browse_limit]
result = await player.async_query(*cmd)
for app in result["radioss_loop"]:
app_cmd = "app-" + app["cmd"]
if app_cmd not in self.known_apps_radios:
self.add_new_command(app_cmd, "item_id")
_LOGGER.debug(
"Adding new command %s to browse data for player %s",
app_cmd,
player.player_id,
)
def _build_response_apps_radios_category( def _build_response_apps_radios_category(
@ -292,8 +319,7 @@ async def build_item_response(
app_cmd = "app-" + item["cmd"] app_cmd = "app-" + item["cmd"]
if app_cmd not in browse_data.known_apps_radios: if app_cmd not in browse_data.known_apps_radios:
browse_data.known_apps_radios.add(app_cmd) browse_data.add_new_command(app_cmd, "item_id")
_add_new_command_to_browse_data(browse_data, app_cmd, "item_id")
child_media = _build_response_apps_radios_category( child_media = _build_response_apps_radios_category(
browse_data=browse_data, cmd=app_cmd, item=item browse_data=browse_data, cmd=app_cmd, item=item

View File

@ -311,6 +311,11 @@ class SqueezeBoxMediaPlayerEntity(SqueezeboxEntity, MediaPlayerEntity):
) )
return None return None
async def async_added_to_hass(self) -> None:
"""Call when entity is added to hass."""
await super().async_added_to_hass()
await self._browse_data.async_init(self._player, self.browse_limit)
async def async_will_remove_from_hass(self) -> None: async def async_will_remove_from_hass(self) -> None:
"""Remove from list of known players when removed from hass.""" """Remove from list of known players when removed from hass."""
self.coordinator.config_entry.runtime_data.known_player_ids.remove( self.coordinator.config_entry.runtime_data.known_player_ids.remove(

View File

@ -765,9 +765,7 @@ async def test_squeezebox_call_query(
}, },
blocking=True, blocking=True,
) )
configured_player.async_query.assert_called_once_with( configured_player.async_query.assert_called_with("test_command", "param1", "param2")
"test_command", "param1", "param2"
)
async def test_squeezebox_call_method( async def test_squeezebox_call_method(
@ -784,9 +782,7 @@ async def test_squeezebox_call_method(
}, },
blocking=True, blocking=True,
) )
configured_player.async_query.assert_called_once_with( configured_player.async_query.assert_called_with("test_command", "param1", "param2")
"test_command", "param1", "param2"
)
async def test_squeezebox_invalid_state( async def test_squeezebox_invalid_state(