mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 14:27:07 +00:00
Remove Spotify audio feature sensors (#131754)
This commit is contained in:
parent
0f5e0dd4bf
commit
39c2a529d1
@ -29,7 +29,7 @@ from .util import (
|
||||
spotify_uri_from_media_browser_url,
|
||||
)
|
||||
|
||||
PLATFORMS = [Platform.MEDIA_PLAYER, Platform.SENSOR]
|
||||
PLATFORMS = [Platform.MEDIA_PLAYER]
|
||||
|
||||
__all__ = [
|
||||
"async_browse_media",
|
||||
|
@ -7,14 +7,12 @@ from typing import TYPE_CHECKING
|
||||
|
||||
from spotifyaio import (
|
||||
ContextType,
|
||||
ItemType,
|
||||
PlaybackState,
|
||||
Playlist,
|
||||
SpotifyClient,
|
||||
SpotifyConnectionError,
|
||||
UserProfile,
|
||||
)
|
||||
from spotifyaio.models import AudioFeatures
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
@ -39,7 +37,6 @@ class SpotifyCoordinatorData:
|
||||
current_playback: PlaybackState | None
|
||||
position_updated_at: datetime | None
|
||||
playlist: Playlist | None
|
||||
audio_features: AudioFeatures | None
|
||||
dj_playlist: bool = False
|
||||
|
||||
|
||||
@ -65,7 +62,6 @@ class SpotifyCoordinator(DataUpdateCoordinator[SpotifyCoordinatorData]):
|
||||
)
|
||||
self.client = client
|
||||
self._playlist: Playlist | None = None
|
||||
self._currently_loaded_track: str | None = None
|
||||
|
||||
async def _async_setup(self) -> None:
|
||||
"""Set up the coordinator."""
|
||||
@ -84,28 +80,11 @@ class SpotifyCoordinator(DataUpdateCoordinator[SpotifyCoordinatorData]):
|
||||
current_playback=None,
|
||||
position_updated_at=None,
|
||||
playlist=None,
|
||||
audio_features=None,
|
||||
)
|
||||
# Record the last updated time, because Spotify's timestamp property is unreliable
|
||||
# and doesn't actually return the fetch time as is mentioned in the API description
|
||||
position_updated_at = dt_util.utcnow()
|
||||
|
||||
audio_features: AudioFeatures | None = None
|
||||
if (item := current.item) is not None and item.type == ItemType.TRACK:
|
||||
if item.uri != self._currently_loaded_track:
|
||||
try:
|
||||
audio_features = await self.client.get_audio_features(item.uri)
|
||||
except SpotifyConnectionError:
|
||||
_LOGGER.debug(
|
||||
"Unable to load audio features for track '%s'. "
|
||||
"Continuing without audio features",
|
||||
item.uri,
|
||||
)
|
||||
audio_features = None
|
||||
else:
|
||||
self._currently_loaded_track = item.uri
|
||||
else:
|
||||
audio_features = self.data.audio_features
|
||||
dj_playlist = False
|
||||
if (context := current.context) is not None:
|
||||
if self._playlist is None or self._playlist.uri != context.uri:
|
||||
@ -128,6 +107,5 @@ class SpotifyCoordinator(DataUpdateCoordinator[SpotifyCoordinatorData]):
|
||||
current_playback=current,
|
||||
position_updated_at=position_updated_at,
|
||||
playlist=self._playlist,
|
||||
audio_features=audio_features,
|
||||
dj_playlist=dj_playlist,
|
||||
)
|
||||
|
@ -4,41 +4,6 @@
|
||||
"spotify": {
|
||||
"default": "mdi:spotify"
|
||||
}
|
||||
},
|
||||
"sensor": {
|
||||
"song_tempo": {
|
||||
"default": "mdi:metronome"
|
||||
},
|
||||
"danceability": {
|
||||
"default": "mdi:dance-ballroom"
|
||||
},
|
||||
"energy": {
|
||||
"default": "mdi:lightning-bolt"
|
||||
},
|
||||
"mode": {
|
||||
"default": "mdi:music"
|
||||
},
|
||||
"speechiness": {
|
||||
"default": "mdi:speaker-message"
|
||||
},
|
||||
"acousticness": {
|
||||
"default": "mdi:guitar-acoustic"
|
||||
},
|
||||
"instrumentalness": {
|
||||
"default": "mdi:guitar-electric"
|
||||
},
|
||||
"valence": {
|
||||
"default": "mdi:emoticon-happy"
|
||||
},
|
||||
"liveness": {
|
||||
"default": "mdi:music-note"
|
||||
},
|
||||
"time_signature": {
|
||||
"default": "mdi:music-clef-treble"
|
||||
},
|
||||
"key": {
|
||||
"default": "mdi:music-clef-treble"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,179 +0,0 @@
|
||||
"""Sensor platform for Spotify."""
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
|
||||
from spotifyaio.models import AudioFeatures, Key
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
)
|
||||
from homeassistant.const import PERCENTAGE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .coordinator import SpotifyConfigEntry, SpotifyCoordinator
|
||||
from .entity import SpotifyEntity
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class SpotifyAudioFeaturesSensorEntityDescription(SensorEntityDescription):
|
||||
"""Describes Spotify sensor entity."""
|
||||
|
||||
value_fn: Callable[[AudioFeatures], float | str | None]
|
||||
|
||||
|
||||
KEYS: dict[Key, str] = {
|
||||
Key.C: "C",
|
||||
Key.C_SHARP_D_FLAT: "C♯/D♭",
|
||||
Key.D: "D",
|
||||
Key.D_SHARP_E_FLAT: "D♯/E♭",
|
||||
Key.E: "E",
|
||||
Key.F: "F",
|
||||
Key.F_SHARP_G_FLAT: "F♯/G♭",
|
||||
Key.G: "G",
|
||||
Key.G_SHARP_A_FLAT: "G♯/A♭",
|
||||
Key.A: "A",
|
||||
Key.A_SHARP_B_FLAT: "A♯/B♭",
|
||||
Key.B: "B",
|
||||
}
|
||||
|
||||
KEY_OPTIONS = list(KEYS.values())
|
||||
|
||||
|
||||
def _get_key(audio_features: AudioFeatures) -> str | None:
|
||||
if audio_features.key is None:
|
||||
return None
|
||||
return KEYS[audio_features.key]
|
||||
|
||||
|
||||
AUDIO_FEATURE_SENSORS: tuple[SpotifyAudioFeaturesSensorEntityDescription, ...] = (
|
||||
SpotifyAudioFeaturesSensorEntityDescription(
|
||||
key="bpm",
|
||||
translation_key="song_tempo",
|
||||
native_unit_of_measurement="bpm",
|
||||
suggested_display_precision=0,
|
||||
value_fn=lambda audio_features: audio_features.tempo,
|
||||
),
|
||||
SpotifyAudioFeaturesSensorEntityDescription(
|
||||
key="danceability",
|
||||
translation_key="danceability",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
suggested_display_precision=0,
|
||||
value_fn=lambda audio_features: audio_features.danceability * 100,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
SpotifyAudioFeaturesSensorEntityDescription(
|
||||
key="energy",
|
||||
translation_key="energy",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
suggested_display_precision=0,
|
||||
value_fn=lambda audio_features: audio_features.energy * 100,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
SpotifyAudioFeaturesSensorEntityDescription(
|
||||
key="mode",
|
||||
translation_key="mode",
|
||||
device_class=SensorDeviceClass.ENUM,
|
||||
options=["major", "minor"],
|
||||
value_fn=lambda audio_features: audio_features.mode.name.lower(),
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
SpotifyAudioFeaturesSensorEntityDescription(
|
||||
key="speechiness",
|
||||
translation_key="speechiness",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
suggested_display_precision=0,
|
||||
value_fn=lambda audio_features: audio_features.speechiness * 100,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
SpotifyAudioFeaturesSensorEntityDescription(
|
||||
key="acousticness",
|
||||
translation_key="acousticness",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
suggested_display_precision=0,
|
||||
value_fn=lambda audio_features: audio_features.acousticness * 100,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
SpotifyAudioFeaturesSensorEntityDescription(
|
||||
key="instrumentalness",
|
||||
translation_key="instrumentalness",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
suggested_display_precision=0,
|
||||
value_fn=lambda audio_features: audio_features.instrumentalness * 100,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
SpotifyAudioFeaturesSensorEntityDescription(
|
||||
key="liveness",
|
||||
translation_key="liveness",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
suggested_display_precision=0,
|
||||
value_fn=lambda audio_features: audio_features.liveness * 100,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
SpotifyAudioFeaturesSensorEntityDescription(
|
||||
key="valence",
|
||||
translation_key="valence",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
suggested_display_precision=0,
|
||||
value_fn=lambda audio_features: audio_features.valence * 100,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
SpotifyAudioFeaturesSensorEntityDescription(
|
||||
key="time_signature",
|
||||
translation_key="time_signature",
|
||||
device_class=SensorDeviceClass.ENUM,
|
||||
options=["3/4", "4/4", "5/4", "6/4", "7/4"],
|
||||
value_fn=lambda audio_features: f"{audio_features.time_signature}/4",
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
SpotifyAudioFeaturesSensorEntityDescription(
|
||||
key="key",
|
||||
translation_key="key",
|
||||
device_class=SensorDeviceClass.ENUM,
|
||||
options=KEY_OPTIONS,
|
||||
value_fn=_get_key,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: SpotifyConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up Spotify sensor based on a config entry."""
|
||||
coordinator = entry.runtime_data.coordinator
|
||||
|
||||
async_add_entities(
|
||||
SpotifyAudioFeatureSensor(coordinator, description)
|
||||
for description in AUDIO_FEATURE_SENSORS
|
||||
)
|
||||
|
||||
|
||||
class SpotifyAudioFeatureSensor(SpotifyEntity, SensorEntity):
|
||||
"""Representation of a Spotify sensor."""
|
||||
|
||||
entity_description: SpotifyAudioFeaturesSensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: SpotifyCoordinator,
|
||||
entity_description: SpotifyAudioFeaturesSensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
super().__init__(coordinator)
|
||||
self._attr_unique_id = (
|
||||
f"{coordinator.current_user.user_id}_{entity_description.key}"
|
||||
)
|
||||
self.entity_description = entity_description
|
||||
|
||||
@property
|
||||
def native_value(self) -> float | str | None:
|
||||
"""Return the state of the sensor."""
|
||||
if (audio_features := self.coordinator.data.audio_features) is None:
|
||||
return None
|
||||
return self.entity_description.value_fn(audio_features)
|
@ -30,46 +30,5 @@
|
||||
"info": {
|
||||
"api_endpoint_reachable": "Spotify API endpoint reachable"
|
||||
}
|
||||
},
|
||||
"entity": {
|
||||
"sensor": {
|
||||
"song_tempo": {
|
||||
"name": "Song tempo"
|
||||
},
|
||||
"danceability": {
|
||||
"name": "Song danceability"
|
||||
},
|
||||
"energy": {
|
||||
"name": "Song energy"
|
||||
},
|
||||
"mode": {
|
||||
"name": "Song mode",
|
||||
"state": {
|
||||
"minor": "Minor",
|
||||
"major": "Major"
|
||||
}
|
||||
},
|
||||
"speechiness": {
|
||||
"name": "Song speechiness"
|
||||
},
|
||||
"acousticness": {
|
||||
"name": "Song acousticness"
|
||||
},
|
||||
"instrumentalness": {
|
||||
"name": "Song instrumentalness"
|
||||
},
|
||||
"valence": {
|
||||
"name": "Song valence"
|
||||
},
|
||||
"liveness": {
|
||||
"name": "Song liveness"
|
||||
},
|
||||
"time_signature": {
|
||||
"name": "Song time signature"
|
||||
},
|
||||
"key": {
|
||||
"name": "Song key"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,6 @@ from spotifyaio.models import (
|
||||
Album,
|
||||
Artist,
|
||||
ArtistResponse,
|
||||
AudioFeatures,
|
||||
CategoriesResponse,
|
||||
Category,
|
||||
CategoryPlaylistResponse,
|
||||
@ -140,7 +139,6 @@ def mock_spotify() -> Generator[AsyncMock]:
|
||||
("album.json", "get_album", Album),
|
||||
("artist.json", "get_artist", Artist),
|
||||
("show.json", "get_show", Show),
|
||||
("audio_features.json", "get_audio_features", AudioFeatures),
|
||||
):
|
||||
getattr(client, method).return_value = obj.from_json(
|
||||
load_fixture(fixture, DOMAIN)
|
||||
|
@ -1,20 +0,0 @@
|
||||
{
|
||||
"danceability": 0.696,
|
||||
"energy": 0.905,
|
||||
"key": 3,
|
||||
"loudness": -2.743,
|
||||
"mode": 1,
|
||||
"speechiness": 0.103,
|
||||
"acousticness": 0.011,
|
||||
"instrumentalness": 0.000905,
|
||||
"liveness": 0.302,
|
||||
"valence": 0.625,
|
||||
"tempo": 114.944,
|
||||
"type": "audio_features",
|
||||
"id": "11dFghVXANMlKmJXsNCbNl",
|
||||
"uri": "spotify:track:11dFghVXANMlKmJXsNCbNl",
|
||||
"track_href": "https://api.spotify.com/v1/tracks/11dFghVXANMlKmJXsNCbNl",
|
||||
"analysis_url": "https://api.spotify.com/v1/audio-analysis/11dFghVXANMlKmJXsNCbNl",
|
||||
"duration_ms": 207960,
|
||||
"time_signature": 4
|
||||
}
|
@ -14,20 +14,6 @@
|
||||
}),
|
||||
]),
|
||||
'playback': dict({
|
||||
'audio_features': dict({
|
||||
'acousticness': 0.011,
|
||||
'danceability': 0.696,
|
||||
'energy': 0.905,
|
||||
'instrumentalness': 0.000905,
|
||||
'key': 3,
|
||||
'liveness': 0.302,
|
||||
'loudness': -2.743,
|
||||
'mode': 1,
|
||||
'speechiness': 0.103,
|
||||
'tempo': 114.944,
|
||||
'time_signature': 4,
|
||||
'valence': 0.625,
|
||||
}),
|
||||
'current_playback': dict({
|
||||
'context': dict({
|
||||
'context_type': 'playlist',
|
||||
|
@ -1,595 +0,0 @@
|
||||
# serializer version: 1
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_acousticness-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_acousticness',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
'sensor': dict({
|
||||
'suggested_display_precision': 0,
|
||||
}),
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Song acousticness',
|
||||
'platform': 'spotify',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'acousticness',
|
||||
'unique_id': '1112264111_acousticness',
|
||||
'unit_of_measurement': '%',
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_acousticness-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Spotify spotify_1 Song acousticness',
|
||||
'unit_of_measurement': '%',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_acousticness',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '1.1',
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_danceability-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_danceability',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
'sensor': dict({
|
||||
'suggested_display_precision': 0,
|
||||
}),
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Song danceability',
|
||||
'platform': 'spotify',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'danceability',
|
||||
'unique_id': '1112264111_danceability',
|
||||
'unit_of_measurement': '%',
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_danceability-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Spotify spotify_1 Song danceability',
|
||||
'unit_of_measurement': '%',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_danceability',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '69.6',
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_energy-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_energy',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
'sensor': dict({
|
||||
'suggested_display_precision': 0,
|
||||
}),
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Song energy',
|
||||
'platform': 'spotify',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'energy',
|
||||
'unique_id': '1112264111_energy',
|
||||
'unit_of_measurement': '%',
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_energy-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Spotify spotify_1 Song energy',
|
||||
'unit_of_measurement': '%',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_energy',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '90.5',
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_instrumentalness-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_instrumentalness',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
'sensor': dict({
|
||||
'suggested_display_precision': 0,
|
||||
}),
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Song instrumentalness',
|
||||
'platform': 'spotify',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'instrumentalness',
|
||||
'unique_id': '1112264111_instrumentalness',
|
||||
'unit_of_measurement': '%',
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_instrumentalness-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Spotify spotify_1 Song instrumentalness',
|
||||
'unit_of_measurement': '%',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_instrumentalness',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '0.0905',
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_key-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'options': list([
|
||||
'C',
|
||||
'C♯/D♭',
|
||||
'D',
|
||||
'D♯/E♭',
|
||||
'E',
|
||||
'F',
|
||||
'F♯/G♭',
|
||||
'G',
|
||||
'G♯/A♭',
|
||||
'A',
|
||||
'A♯/B♭',
|
||||
'B',
|
||||
]),
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_key',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.ENUM: 'enum'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Song key',
|
||||
'platform': 'spotify',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'key',
|
||||
'unique_id': '1112264111_key',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_key-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'enum',
|
||||
'friendly_name': 'Spotify spotify_1 Song key',
|
||||
'options': list([
|
||||
'C',
|
||||
'C♯/D♭',
|
||||
'D',
|
||||
'D♯/E♭',
|
||||
'E',
|
||||
'F',
|
||||
'F♯/G♭',
|
||||
'G',
|
||||
'G♯/A♭',
|
||||
'A',
|
||||
'A♯/B♭',
|
||||
'B',
|
||||
]),
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_key',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'D♯/E♭',
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_liveness-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_liveness',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
'sensor': dict({
|
||||
'suggested_display_precision': 0,
|
||||
}),
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Song liveness',
|
||||
'platform': 'spotify',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'liveness',
|
||||
'unique_id': '1112264111_liveness',
|
||||
'unit_of_measurement': '%',
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_liveness-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Spotify spotify_1 Song liveness',
|
||||
'unit_of_measurement': '%',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_liveness',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '30.2',
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_mode-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'options': list([
|
||||
'major',
|
||||
'minor',
|
||||
]),
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_mode',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.ENUM: 'enum'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Song mode',
|
||||
'platform': 'spotify',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'mode',
|
||||
'unique_id': '1112264111_mode',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_mode-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'enum',
|
||||
'friendly_name': 'Spotify spotify_1 Song mode',
|
||||
'options': list([
|
||||
'major',
|
||||
'minor',
|
||||
]),
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_mode',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'major',
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_speechiness-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_speechiness',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
'sensor': dict({
|
||||
'suggested_display_precision': 0,
|
||||
}),
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Song speechiness',
|
||||
'platform': 'spotify',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'speechiness',
|
||||
'unique_id': '1112264111_speechiness',
|
||||
'unit_of_measurement': '%',
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_speechiness-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Spotify spotify_1 Song speechiness',
|
||||
'unit_of_measurement': '%',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_speechiness',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '10.3',
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_tempo-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_tempo',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
'sensor': dict({
|
||||
'suggested_display_precision': 0,
|
||||
}),
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Song tempo',
|
||||
'platform': 'spotify',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'song_tempo',
|
||||
'unique_id': '1112264111_bpm',
|
||||
'unit_of_measurement': 'bpm',
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_tempo-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Spotify spotify_1 Song tempo',
|
||||
'unit_of_measurement': 'bpm',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_tempo',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '114.944',
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_time_signature-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'options': list([
|
||||
'3/4',
|
||||
'4/4',
|
||||
'5/4',
|
||||
'6/4',
|
||||
'7/4',
|
||||
]),
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_time_signature',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.ENUM: 'enum'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Song time signature',
|
||||
'platform': 'spotify',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'time_signature',
|
||||
'unique_id': '1112264111_time_signature',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_time_signature-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'enum',
|
||||
'friendly_name': 'Spotify spotify_1 Song time signature',
|
||||
'options': list([
|
||||
'3/4',
|
||||
'4/4',
|
||||
'5/4',
|
||||
'6/4',
|
||||
'7/4',
|
||||
]),
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_time_signature',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '4/4',
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_valence-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_valence',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
'sensor': dict({
|
||||
'suggested_display_precision': 0,
|
||||
}),
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Song valence',
|
||||
'platform': 'spotify',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'valence',
|
||||
'unique_id': '1112264111_valence',
|
||||
'unit_of_measurement': '%',
|
||||
})
|
||||
# ---
|
||||
# name: test_entities[sensor.spotify_spotify_1_song_valence-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Spotify spotify_1 Song valence',
|
||||
'unit_of_measurement': '%',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.spotify_spotify_1_song_valence',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '62.5',
|
||||
})
|
||||
# ---
|
@ -1,66 +0,0 @@
|
||||
"""Tests for the Spotify sensor platform."""
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from spotifyaio import PlaybackState
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.spotify import DOMAIN
|
||||
from homeassistant.const import STATE_UNKNOWN, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_integration
|
||||
|
||||
from tests.common import MockConfigEntry, load_fixture, snapshot_platform
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("setup_credentials")
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_entities(
|
||||
hass: HomeAssistant,
|
||||
mock_spotify: MagicMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test the Spotify entities."""
|
||||
with patch("homeassistant.components.spotify.PLATFORMS", [Platform.SENSOR]):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("setup_credentials")
|
||||
async def test_audio_features_unavailable(
|
||||
hass: HomeAssistant,
|
||||
mock_spotify: MagicMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test the Spotify entities."""
|
||||
mock_spotify.return_value.get_audio_features.return_value = None
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
assert hass.states.get("sensor.spotify_spotify_1_song_tempo").state == STATE_UNKNOWN
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("setup_credentials")
|
||||
async def test_audio_features_unknown_during_podcast(
|
||||
hass: HomeAssistant,
|
||||
mock_spotify: MagicMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test the Spotify audio features sensor during a podcast."""
|
||||
mock_spotify.return_value.get_playback.return_value = PlaybackState.from_json(
|
||||
load_fixture("playback_episode.json", DOMAIN)
|
||||
)
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
assert hass.states.get("sensor.spotify_spotify_1_song_tempo").state == STATE_UNKNOWN
|
Loading…
x
Reference in New Issue
Block a user