Add media player to Tesla Fleet (#126416)
* Add media player platform * Use MediaPlayerState * Revert change
This commit is contained in:
parent
79872b3e1d
commit
f073e45575
5 changed files with 448 additions and 0 deletions
|
@ -43,6 +43,7 @@ PLATFORMS: Final = [
|
||||||
Platform.BINARY_SENSOR,
|
Platform.BINARY_SENSOR,
|
||||||
Platform.CLIMATE,
|
Platform.CLIMATE,
|
||||||
Platform.DEVICE_TRACKER,
|
Platform.DEVICE_TRACKER,
|
||||||
|
Platform.MEDIA_PLAYER,
|
||||||
Platform.SELECT,
|
Platform.SELECT,
|
||||||
Platform.SENSOR,
|
Platform.SENSOR,
|
||||||
Platform.SWITCH,
|
Platform.SWITCH,
|
||||||
|
|
149
homeassistant/components/tesla_fleet/media_player.py
Normal file
149
homeassistant/components/tesla_fleet/media_player.py
Normal file
|
@ -0,0 +1,149 @@
|
||||||
|
"""Media player platform for Tesla Fleet integration."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from tesla_fleet_api.const import Scope
|
||||||
|
|
||||||
|
from homeassistant.components.media_player import (
|
||||||
|
MediaPlayerDeviceClass,
|
||||||
|
MediaPlayerEntity,
|
||||||
|
MediaPlayerEntityFeature,
|
||||||
|
MediaPlayerState,
|
||||||
|
)
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
from . import TeslaFleetConfigEntry
|
||||||
|
from .entity import TeslaFleetVehicleEntity
|
||||||
|
from .helpers import handle_vehicle_command
|
||||||
|
from .models import TeslaFleetVehicleData
|
||||||
|
|
||||||
|
STATES = {
|
||||||
|
"Playing": MediaPlayerState.PLAYING,
|
||||||
|
"Paused": MediaPlayerState.PAUSED,
|
||||||
|
"Stopped": MediaPlayerState.IDLE,
|
||||||
|
"Off": MediaPlayerState.OFF,
|
||||||
|
}
|
||||||
|
VOLUME_MAX = 11.0
|
||||||
|
VOLUME_STEP = 1.0 / 3
|
||||||
|
|
||||||
|
PARALLEL_UPDATES = 0
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
entry: TeslaFleetConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
|
) -> None:
|
||||||
|
"""Set up the Tesla Fleet Media platform from a config entry."""
|
||||||
|
|
||||||
|
async_add_entities(
|
||||||
|
TeslaFleetMediaEntity(vehicle, Scope.VEHICLE_CMDS in entry.runtime_data.scopes)
|
||||||
|
for vehicle in entry.runtime_data.vehicles
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TeslaFleetMediaEntity(TeslaFleetVehicleEntity, MediaPlayerEntity):
|
||||||
|
"""Vehicle media player class."""
|
||||||
|
|
||||||
|
_attr_device_class = MediaPlayerDeviceClass.SPEAKER
|
||||||
|
_attr_supported_features = (
|
||||||
|
MediaPlayerEntityFeature.NEXT_TRACK
|
||||||
|
| MediaPlayerEntityFeature.PAUSE
|
||||||
|
| MediaPlayerEntityFeature.PLAY
|
||||||
|
| MediaPlayerEntityFeature.PREVIOUS_TRACK
|
||||||
|
| MediaPlayerEntityFeature.VOLUME_SET
|
||||||
|
)
|
||||||
|
_volume_max: float = VOLUME_MAX
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
data: TeslaFleetVehicleData,
|
||||||
|
scoped: bool,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the media player entity."""
|
||||||
|
super().__init__(data, "media")
|
||||||
|
self.scoped = scoped
|
||||||
|
if not scoped and data.signing:
|
||||||
|
self._attr_supported_features = MediaPlayerEntityFeature(0)
|
||||||
|
|
||||||
|
def _async_update_attrs(self) -> None:
|
||||||
|
"""Update entity attributes."""
|
||||||
|
self._volume_max = (
|
||||||
|
self.get("vehicle_state_media_info_audio_volume_max") or VOLUME_MAX
|
||||||
|
)
|
||||||
|
self._attr_state = STATES.get(
|
||||||
|
self.get("vehicle_state_media_info_media_playback_status") or "Off",
|
||||||
|
)
|
||||||
|
self._attr_volume_step = (
|
||||||
|
1.0
|
||||||
|
/ self._volume_max
|
||||||
|
/ (
|
||||||
|
self.get("vehicle_state_media_info_audio_volume_increment")
|
||||||
|
or VOLUME_STEP
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
if volume := self.get("vehicle_state_media_info_audio_volume"):
|
||||||
|
self._attr_volume_level = volume / self._volume_max
|
||||||
|
else:
|
||||||
|
self._attr_volume_level = None
|
||||||
|
|
||||||
|
if duration := self.get("vehicle_state_media_info_now_playing_duration"):
|
||||||
|
self._attr_media_duration = duration / 1000
|
||||||
|
else:
|
||||||
|
self._attr_media_duration = None
|
||||||
|
|
||||||
|
if duration and (
|
||||||
|
position := self.get("vehicle_state_media_info_now_playing_elapsed")
|
||||||
|
):
|
||||||
|
self._attr_media_position = position / 1000
|
||||||
|
else:
|
||||||
|
self._attr_media_position = None
|
||||||
|
|
||||||
|
self._attr_media_title = self.get("vehicle_state_media_info_now_playing_title")
|
||||||
|
self._attr_media_artist = self.get(
|
||||||
|
"vehicle_state_media_info_now_playing_artist"
|
||||||
|
)
|
||||||
|
self._attr_media_album_name = self.get(
|
||||||
|
"vehicle_state_media_info_now_playing_album"
|
||||||
|
)
|
||||||
|
self._attr_media_playlist = self.get(
|
||||||
|
"vehicle_state_media_info_now_playing_station"
|
||||||
|
)
|
||||||
|
self._attr_source = self.get("vehicle_state_media_info_now_playing_source")
|
||||||
|
|
||||||
|
async def async_set_volume_level(self, volume: float) -> None:
|
||||||
|
"""Set volume level, range 0..1."""
|
||||||
|
await self.wake_up_if_asleep()
|
||||||
|
await handle_vehicle_command(
|
||||||
|
self.api.adjust_volume(int(volume * self._volume_max))
|
||||||
|
)
|
||||||
|
self._attr_volume_level = volume
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
async def async_media_play(self) -> None:
|
||||||
|
"""Send play command."""
|
||||||
|
if self.state != MediaPlayerState.PLAYING:
|
||||||
|
await self.wake_up_if_asleep()
|
||||||
|
await handle_vehicle_command(self.api.media_toggle_playback())
|
||||||
|
self._attr_state = MediaPlayerState.PLAYING
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
async def async_media_pause(self) -> None:
|
||||||
|
"""Send pause command."""
|
||||||
|
if self.state == MediaPlayerState.PLAYING:
|
||||||
|
await self.wake_up_if_asleep()
|
||||||
|
await handle_vehicle_command(self.api.media_toggle_playback())
|
||||||
|
self._attr_state = MediaPlayerState.PAUSED
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
async def async_media_next_track(self) -> None:
|
||||||
|
"""Send next track command."""
|
||||||
|
await self.wake_up_if_asleep()
|
||||||
|
await handle_vehicle_command(self.api.media_next_track())
|
||||||
|
|
||||||
|
async def async_media_previous_track(self) -> None:
|
||||||
|
"""Send previous track command."""
|
||||||
|
await self.wake_up_if_asleep()
|
||||||
|
await handle_vehicle_command(self.api.media_prev_track())
|
|
@ -133,6 +133,11 @@
|
||||||
"name": "Route"
|
"name": "Route"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"media_player": {
|
||||||
|
"media": {
|
||||||
|
"name": "[%key:component::media_player::title%]"
|
||||||
|
}
|
||||||
|
},
|
||||||
"number": {
|
"number": {
|
||||||
"backup_reserve_percent": {
|
"backup_reserve_percent": {
|
||||||
"name": "Backup reserve"
|
"name": "Backup reserve"
|
||||||
|
|
136
tests/components/tesla_fleet/snapshots/test_media_player.ambr
Normal file
136
tests/components/tesla_fleet/snapshots/test_media_player.ambr
Normal file
|
@ -0,0 +1,136 @@
|
||||||
|
# serializer version: 1
|
||||||
|
# name: test_media_player[media_player.test_media_player-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': dict({
|
||||||
|
}),
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'media_player',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'media_player.test_media_player',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': <MediaPlayerDeviceClass.SPEAKER: 'speaker'>,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Media player',
|
||||||
|
'platform': 'tesla_fleet',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'supported_features': <MediaPlayerEntityFeature: 16437>,
|
||||||
|
'translation_key': 'media',
|
||||||
|
'unique_id': 'LRWXF7EK4KC700000-media',
|
||||||
|
'unit_of_measurement': None,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_media_player[media_player.test_media_player-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'device_class': 'speaker',
|
||||||
|
'friendly_name': 'Test Media player',
|
||||||
|
'media_album_name': 'Elon Musk',
|
||||||
|
'media_artist': 'Walter Isaacson',
|
||||||
|
'media_duration': 651.0,
|
||||||
|
'media_playlist': 'Elon Musk',
|
||||||
|
'media_position': 1.0,
|
||||||
|
'media_title': 'Chapter 51: Cybertruck: Tesla, 2018–2019',
|
||||||
|
'source': 'Audible',
|
||||||
|
'supported_features': <MediaPlayerEntityFeature: 16437>,
|
||||||
|
'volume_level': 0.16129355359011466,
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'media_player.test_media_player',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'playing',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_media_player_alt[media_player.test_media_player-statealt]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'device_class': 'speaker',
|
||||||
|
'friendly_name': 'Test Media player',
|
||||||
|
'media_album_name': '',
|
||||||
|
'media_artist': '',
|
||||||
|
'media_playlist': '',
|
||||||
|
'media_title': '',
|
||||||
|
'source': 'Spotify',
|
||||||
|
'supported_features': <MediaPlayerEntityFeature: 16437>,
|
||||||
|
'volume_level': 0.25806775026025003,
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'media_player.test_media_player',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'idle',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_media_player_noscope[media_player.test_media_player-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': dict({
|
||||||
|
}),
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'media_player',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'media_player.test_media_player',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': <MediaPlayerDeviceClass.SPEAKER: 'speaker'>,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Media player',
|
||||||
|
'platform': 'tesla_fleet',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'supported_features': <MediaPlayerEntityFeature: 16437>,
|
||||||
|
'translation_key': 'media',
|
||||||
|
'unique_id': 'LRWXF7EK4KC700000-media',
|
||||||
|
'unit_of_measurement': None,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_media_player_noscope[media_player.test_media_player-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'device_class': 'speaker',
|
||||||
|
'friendly_name': 'Test Media player',
|
||||||
|
'media_album_name': 'Elon Musk',
|
||||||
|
'media_artist': 'Walter Isaacson',
|
||||||
|
'media_duration': 651.0,
|
||||||
|
'media_playlist': 'Elon Musk',
|
||||||
|
'media_position': 1.0,
|
||||||
|
'media_title': 'Chapter 51: Cybertruck: Tesla, 2018–2019',
|
||||||
|
'source': 'Audible',
|
||||||
|
'supported_features': <MediaPlayerEntityFeature: 16437>,
|
||||||
|
'volume_level': 0.16129355359011466,
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'media_player.test_media_player',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'playing',
|
||||||
|
})
|
||||||
|
# ---
|
157
tests/components/tesla_fleet/test_media_player.py
Normal file
157
tests/components/tesla_fleet/test_media_player.py
Normal file
|
@ -0,0 +1,157 @@
|
||||||
|
"""Test the Tesla Fleet media player platform."""
|
||||||
|
|
||||||
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
|
from syrupy import SnapshotAssertion
|
||||||
|
from tesla_fleet_api.exceptions import VehicleOffline
|
||||||
|
|
||||||
|
from homeassistant.components.media_player import (
|
||||||
|
ATTR_MEDIA_VOLUME_LEVEL,
|
||||||
|
DOMAIN as MEDIA_PLAYER_DOMAIN,
|
||||||
|
SERVICE_MEDIA_NEXT_TRACK,
|
||||||
|
SERVICE_MEDIA_PAUSE,
|
||||||
|
SERVICE_MEDIA_PLAY,
|
||||||
|
SERVICE_MEDIA_PREVIOUS_TRACK,
|
||||||
|
SERVICE_VOLUME_SET,
|
||||||
|
MediaPlayerState,
|
||||||
|
)
|
||||||
|
from homeassistant.const import ATTR_ENTITY_ID, Platform
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers import entity_registry as er
|
||||||
|
|
||||||
|
from . import assert_entities, assert_entities_alt, setup_platform
|
||||||
|
from .const import COMMAND_OK, VEHICLE_DATA_ALT
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
|
||||||
|
async def test_media_player(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
snapshot: SnapshotAssertion,
|
||||||
|
entity_registry: er.EntityRegistry,
|
||||||
|
normal_config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Tests that the media player entities are correct."""
|
||||||
|
|
||||||
|
await setup_platform(hass, normal_config_entry, [Platform.MEDIA_PLAYER])
|
||||||
|
assert_entities(hass, normal_config_entry.entry_id, entity_registry, snapshot)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_media_player_alt(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
snapshot: SnapshotAssertion,
|
||||||
|
entity_registry: er.EntityRegistry,
|
||||||
|
mock_vehicle_data: AsyncMock,
|
||||||
|
normal_config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Tests that the media player entities are correct."""
|
||||||
|
|
||||||
|
mock_vehicle_data.return_value = VEHICLE_DATA_ALT
|
||||||
|
await setup_platform(hass, normal_config_entry, [Platform.MEDIA_PLAYER])
|
||||||
|
assert_entities_alt(hass, normal_config_entry.entry_id, entity_registry, snapshot)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_media_player_offline(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_vehicle_data: AsyncMock,
|
||||||
|
normal_config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Tests that the media player entities are correct when offline."""
|
||||||
|
|
||||||
|
mock_vehicle_data.side_effect = VehicleOffline
|
||||||
|
await setup_platform(hass, normal_config_entry, [Platform.MEDIA_PLAYER])
|
||||||
|
state = hass.states.get("media_player.test_media_player")
|
||||||
|
assert state.state == MediaPlayerState.OFF
|
||||||
|
|
||||||
|
|
||||||
|
async def test_media_player_noscope(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
snapshot: SnapshotAssertion,
|
||||||
|
entity_registry: er.EntityRegistry,
|
||||||
|
readonly_config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Tests that the media player entities are correct without required scope."""
|
||||||
|
|
||||||
|
await setup_platform(hass, readonly_config_entry, [Platform.MEDIA_PLAYER])
|
||||||
|
assert_entities(hass, readonly_config_entry.entry_id, entity_registry, snapshot)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_media_player_services(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
snapshot: SnapshotAssertion,
|
||||||
|
normal_config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Tests that the media player services work."""
|
||||||
|
|
||||||
|
await setup_platform(hass, normal_config_entry, [Platform.MEDIA_PLAYER])
|
||||||
|
|
||||||
|
entity_id = "media_player.test_media_player"
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.tesla_fleet.VehicleSpecific.adjust_volume",
|
||||||
|
return_value=COMMAND_OK,
|
||||||
|
) as call:
|
||||||
|
await hass.services.async_call(
|
||||||
|
MEDIA_PLAYER_DOMAIN,
|
||||||
|
SERVICE_VOLUME_SET,
|
||||||
|
{ATTR_ENTITY_ID: entity_id, ATTR_MEDIA_VOLUME_LEVEL: 0.5},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
state = hass.states.get(entity_id)
|
||||||
|
assert state.attributes[ATTR_MEDIA_VOLUME_LEVEL] == 0.5
|
||||||
|
call.assert_called_once()
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.tesla_fleet.VehicleSpecific.media_toggle_playback",
|
||||||
|
return_value=COMMAND_OK,
|
||||||
|
) as call:
|
||||||
|
await hass.services.async_call(
|
||||||
|
MEDIA_PLAYER_DOMAIN,
|
||||||
|
SERVICE_MEDIA_PAUSE,
|
||||||
|
{ATTR_ENTITY_ID: entity_id},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
state = hass.states.get(entity_id)
|
||||||
|
assert state.state == MediaPlayerState.PAUSED
|
||||||
|
call.assert_called_once()
|
||||||
|
|
||||||
|
# This test will fail without the previous call to pause playback
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.tesla_fleet.VehicleSpecific.media_toggle_playback",
|
||||||
|
return_value=COMMAND_OK,
|
||||||
|
) as call:
|
||||||
|
await hass.services.async_call(
|
||||||
|
MEDIA_PLAYER_DOMAIN,
|
||||||
|
SERVICE_MEDIA_PLAY,
|
||||||
|
{ATTR_ENTITY_ID: entity_id},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
state = hass.states.get(entity_id)
|
||||||
|
assert state.state == MediaPlayerState.PLAYING
|
||||||
|
call.assert_called_once()
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.tesla_fleet.VehicleSpecific.media_next_track",
|
||||||
|
return_value=COMMAND_OK,
|
||||||
|
) as call:
|
||||||
|
await hass.services.async_call(
|
||||||
|
MEDIA_PLAYER_DOMAIN,
|
||||||
|
SERVICE_MEDIA_NEXT_TRACK,
|
||||||
|
{ATTR_ENTITY_ID: entity_id},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
state = hass.states.get(entity_id)
|
||||||
|
call.assert_called_once()
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.tesla_fleet.VehicleSpecific.media_prev_track",
|
||||||
|
return_value=COMMAND_OK,
|
||||||
|
) as call:
|
||||||
|
await hass.services.async_call(
|
||||||
|
MEDIA_PLAYER_DOMAIN,
|
||||||
|
SERVICE_MEDIA_PREVIOUS_TRACK,
|
||||||
|
{ATTR_ENTITY_ID: entity_id},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
state = hass.states.get(entity_id)
|
||||||
|
call.assert_called_once()
|
Loading…
Add table
Reference in a new issue