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 all 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 @@ -15,6 +15,7 @@
)
from spotifyaio.models import AudioFeatures

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 @@ -45,6 +46,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",
)
28 changes: 5 additions & 23 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 @@ -80,8 +77,6 @@ async def async_setup_entry(
spotify = SpotifyMediaPlayer(
data.coordinator,
data.devices,
entry.unique_id,
entry.title,
)
async_add_entities([spotify])

Expand All @@ -99,10 +94,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 @@ -111,23 +105,11 @@ def __init__(
self,
coordinator: SpotifyCoordinator,
device_coordinator: DataUpdateCoordinator[list[Device]],
user_id: str,
name: str,
) -> None:
"""Initialize."""
super().__init__(coordinator)
self.devices = device_coordinator
self._attr_unique_id = user_id
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",
)
self._attr_unique_id = coordinator.current_user.user_id
@property
def currently_playing(self) -> PlaybackState | None:
Expand Down
28 changes: 7 additions & 21 deletions homeassistant/components/spotify/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@

from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from . import DOMAIN, SpotifyConfigEntry
from . import SpotifyConfigEntry
from .coordinator import SpotifyCoordinator
from .entity import SpotifyEntity


@dataclass(frozen=True, kw_only=True)
Expand Down Expand Up @@ -41,41 +40,28 @@ async def async_setup_entry(
"""Set up Spotify sensor based on a config entry."""
coordinator = entry.runtime_data.coordinator

user_id = entry.unique_id

assert user_id is not None

async_add_entities(
SpotifyAudioFeatureSensor(coordinator, description, user_id, entry.title)
SpotifyAudioFeatureSensor(coordinator, description)
for description in AUDIO_FEATURE_SENSORS
)


class SpotifyAudioFeatureSensor(CoordinatorEntity[SpotifyCoordinator], SensorEntity):
class SpotifyAudioFeatureSensor(SpotifyEntity, SensorEntity):
"""Representation of a Spotify sensor."""

_attr_has_entity_name = True
entity_description: SpotifyAudioFeaturesSensorEntityDescription

def __init__(
self,
coordinator: SpotifyCoordinator,
entity_description: SpotifyAudioFeaturesSensorEntityDescription,
user_id: str,
name: str,
) -> None:
"""Initialize."""
super().__init__(coordinator)
self._attr_unique_id = f"{user_id}_{entity_description.key}"
self.entity_description = entity_description
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",
self._attr_unique_id = (
f"{coordinator.current_user.user_id}_{entity_description.key}"
)
self.entity_description = entity_description

@property
def native_value(self) -> float | None:
Expand Down