Add extended device info and some attributes to Apple TV (#44277)

This commit is contained in:
Pierre Ståhl 2020-12-17 16:47:20 +01:00 committed by GitHub
parent 524db33f13
commit fe7109acf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 10 deletions

View File

@ -15,6 +15,7 @@ from homeassistant.const import (
EVENT_HOMEASSISTANT_STOP,
)
from homeassistant.core import callback
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect,
@ -135,15 +136,6 @@ class AppleTVEntity(Entity):
def async_device_disconnected(self):
"""Handle when connection was lost to device."""
@property
def device_info(self):
"""Return the device info."""
return {
"identifiers": {(DOMAIN, self._identifier)},
"manufacturer": "Apple",
"name": self.name,
}
@property
def name(self):
"""Return the name of the device."""
@ -337,6 +329,8 @@ class AppleTVManager:
self._dispatch_send(SIGNAL_CONNECTED, self.atv)
self._address_updated(str(conf.address))
await self._async_setup_device_registry()
self._connection_attempts = 0
if self._connection_was_lost:
_LOGGER.info(
@ -344,6 +338,27 @@ class AppleTVManager:
)
self._connection_was_lost = False
async def _async_setup_device_registry(self):
attrs = {
"identifiers": {(DOMAIN, self.config_entry.unique_id)},
"manufacturer": "Apple",
"name": self.config_entry.data[CONF_NAME],
}
if self.atv:
dev_info = self.atv.device_info
attrs["model"] = "Apple TV " + dev_info.model.name.replace("Gen", "")
attrs["sw_version"] = dev_info.version
if dev_info.mac:
attrs["connections"] = {(dr.CONNECTION_NETWORK_MAC, dev_info.mac)}
device_registry = await dr.async_get_registry(self.hass)
device_registry.async_get_or_create(
config_entry_id=self.config_entry.entry_id, **attrs
)
@property
def is_connecting(self):
"""Return true if connection is in progress."""

View File

@ -1,7 +1,7 @@
"""Support for Apple TV media player."""
import logging
from pyatv.const import DeviceState, MediaType
from pyatv.const import DeviceState, FeatureName, FeatureState, MediaType
from homeassistant.components.media_player import MediaPlayerEntity
from homeassistant.components.media_player.const import (
@ -107,6 +107,22 @@ class AppleTvMediaPlayer(AppleTVEntity, MediaPlayerEntity):
self._playing = None
self.async_write_ha_state()
@property
def app_id(self):
"""ID of the current running app."""
if self.atv:
if self.atv.features.in_state(FeatureState.Available, FeatureName.App):
return self.atv.metadata.app.identifier
return None
@property
def app_name(self):
"""Name of the current running app."""
if self.atv:
if self.atv.features.in_state(FeatureState.Available, FeatureName.App):
return self.atv.metadata.app.name
return None
@property
def media_content_type(self):
"""Content type of current playing media."""
@ -168,11 +184,31 @@ class AppleTvMediaPlayer(AppleTVEntity, MediaPlayerEntity):
return self._playing.title
return None
@property
def media_artist(self):
"""Artist of current playing media, music track only."""
if self._is_feature_available(FeatureName.Artist):
return self._playing.artist
return None
@property
def media_album_name(self):
"""Album name of current playing media, music track only."""
if self._is_feature_available(FeatureName.Album):
return self._playing.album
return None
@property
def supported_features(self):
"""Flag media player features that are supported."""
return SUPPORT_APPLE_TV
def _is_feature_available(self, feature):
"""Return if a feature is available."""
if self.atv and self._playing:
return self.atv.features.in_state(FeatureState.Available, feature)
return False
async def async_turn_on(self):
"""Turn the media player on."""
await self.manager.connect()