-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
"""Media Player platform for Teslemetry integration.""" | ||
from __future__ import annotations | ||
|
||
from homeassistant.components.media_player import ( | ||
MediaPlayerDeviceClass, | ||
MediaPlayerEntity, | ||
MediaPlayerState, | ||
) | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
||
from .const import DOMAIN | ||
from .entity import ( | ||
TeslemetryEnergyEntity, | ||
TeslemetryVehicleEntity, | ||
TeslemetryWallConnectorEntity, | ||
) | ||
from .coordinator import ( | ||
TeslemetryEnergyDataCoordinator, | ||
TeslemetryVehicleDataCoordinator, | ||
) | ||
from .models import TeslemetryEnergyData, TeslemetryVehicleData | ||
|
||
STATES = { | ||
"Playing": MediaPlayerState.PLAYING, | ||
"Paused": MediaPlayerState.PAUSED, | ||
"Stopped": MediaPlayerState.IDLE, | ||
} | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback | ||
) -> None: | ||
"""Set up the Teslemetry Media platform from a config entry.""" | ||
data = hass.data[DOMAIN][entry.entry_id] | ||
|
||
async_add_entities( | ||
TeslemetryMediaEntity(vehicle) for vehicle in data.vehicles | ||
) | ||
|
||
|
||
class TeslemetryMediaEntity(TeslemetryVehicleEntity, MediaPlayerEntity): | ||
"""Vehicle Location Media Class.""" | ||
|
||
_attr_device_class = MediaPlayerDeviceClass.SPEAKER | ||
|
||
def __init__( | ||
self, | ||
vehicle: TeslemetryVehicleData, | ||
) -> None: | ||
"""Initialize the media player entity.""" | ||
super().__init__(vehicle, "media") | ||
|
||
@property | ||
def state(self) -> MediaPlayerState: | ||
"""State of the player.""" | ||
return STATES.get( | ||
self.get("vehicle_state_media_info_media_playback_status"), | ||
MediaPlayerState.OFF, | ||
) | ||
|
||
@property | ||
def volume_level(self) -> float: | ||
"""Volume level of the media player (0..1).""" | ||
return self.get("vehicle_state_media_info_audio_volume", 0) / self.get( | ||
"vehicle_state_media_info_audio_volume_max", 10.333333 | ||
) | ||
|
||
@property | ||
def media_duration(self) -> int | None: | ||
"""Duration of current playing media in seconds.""" | ||
if duration := self.get("vehicle_state_media_info_now_playing_duration"): | ||
return duration / 1000 | ||
return None | ||
|
||
@property | ||
def media_position(self) -> int | None: | ||
"""Position of current playing media in seconds.""" | ||
# Return media position only when a media duration is > 0 | ||
if self.get("vehicle_state_media_info_now_playing_duration"): | ||
return self.get("vehicle_state_media_info_now_playing_elapsed") / 1000 | ||
return None | ||
|
||
@property | ||
def media_title(self) -> str | None: | ||
"""Title of current playing media.""" | ||
return self.get("vehicle_state_media_info_now_playing_title") | ||
|
||
@property | ||
def media_artist(self) -> str | None: | ||
"""Artist of current playing media, music track only.""" | ||
return self.get("vehicle_state_media_info_now_playing_artist") | ||
|
||
@property | ||
def media_album_name(self) -> str | None: | ||
"""Album name of current playing media, music track only.""" | ||
return self.get("vehicle_state_media_info_now_playing_album") | ||
|
||
@property | ||
def media_playlist(self) -> str | None: | ||
"""Title of Playlist currently playing.""" | ||
return self.get("vehicle_state_media_info_now_playing_station") | ||
|
||
@property | ||
def source(self) -> str | None: | ||
"""Name of the current input source.""" | ||
return self.get("vehicle_state_media_info_now_playing_source") |