Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add base entity to Spotify #128847

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions homeassistant/components/spotify/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
UserProfile,
)

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
import homeassistant.util.dt as dt_util
Expand Down Expand Up @@ -42,6 +43,7 @@ class SpotifyCoordinator(DataUpdateCoordinator[SpotifyCoordinatorData]):
"""Class to manage fetching Spotify data."""

current_user: UserProfile
config_entry: ConfigEntry
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this initialized somewhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the coordinator constructor

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this not be a typed config entry?

Suggested change
config_entry: ConfigEntry
config_entry: SpotifyConfigEntry

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really use the runtime data here though

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really use the runtime data here though

I thought the policy was to update it everywhere...
For example, even if a migration doesn't use runtime_data, we still recommend the signature to match.


def __init__(self, hass: HomeAssistant, client: SpotifyClient) -> None:
"""Initialize."""
Expand Down
25 changes: 25 additions & 0 deletions homeassistant/components/spotify/entity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Base entity for Spotify."""

from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN
from .coordinator import SpotifyCoordinator


class SpotifyEntity(CoordinatorEntity[SpotifyCoordinator]):
"""Defines a base Spotify entity."""

_attr_has_entity_name = True

def __init__(self, coordinator: SpotifyCoordinator) -> None:
"""Initialize the Spotify entity."""
super().__init__(coordinator)
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, coordinator.current_user.user_id)},
manufacturer="Spotify AB",
model=f"Spotify {coordinator.current_user.product}",
name=f"Spotify {coordinator.config_entry.title}",
entry_type=DeviceEntryType.SERVICE,
configuration_url="https://open.spotify.com",
)
22 changes: 4 additions & 18 deletions homeassistant/components/spotify/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,14 @@
RepeatMode,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

from . import SpotifyConfigEntry
from .browse_media import async_browse_media_internal
from .const import DOMAIN, MEDIA_PLAYER_PREFIX, PLAYABLE_MEDIA_TYPES
from .const import MEDIA_PLAYER_PREFIX, PLAYABLE_MEDIA_TYPES
from .coordinator import SpotifyCoordinator
from .entity import SpotifyEntity

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -99,10 +96,9 @@ def wrapper(self: SpotifyMediaPlayer) -> _R | None:
return wrapper
class SpotifyMediaPlayer(CoordinatorEntity[SpotifyCoordinator], MediaPlayerEntity):
class SpotifyMediaPlayer(SpotifyEntity, MediaPlayerEntity):
"""Representation of a Spotify controller."""
_attr_has_entity_name = True
_attr_media_image_remotely_accessible = False
_attr_name = None
_attr_translation_key = "spotify"
Expand All @@ -117,18 +113,8 @@ def __init__(
"""Initialize."""
super().__init__(coordinator)
self.devices = device_coordinator
self._attr_unique_id = user_id
Copy link
Contributor

@CFenner CFenner Oct 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user_id is just handed in for the ID, what about moving this to the base entity as well? You could have a suffix for further entities in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case I don't have a suffix. In other examples I expect to be using an entity description so I'd rather set it in the platforms itself, since the unique id format changes per platform

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case I don't have a suffix

Which I guess would be fine as this is the main entity (name=None). So in this case hand in None as suffix and don't append anything.

self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, user_id)},
manufacturer="Spotify AB",
model=f"Spotify {coordinator.current_user.product}",
name=f"Spotify {name}",
entry_type=DeviceEntryType.SERVICE,
configuration_url="https://open.spotify.com",
)
@property
def currently_playing(self) -> PlaybackState | None:
"""Return the current playback."""
Expand Down