Use entity class attributes for Cast (#53348)

This commit is contained in:
Robert Hillis 2021-07-27 14:03:13 -04:00 committed by GitHub
parent 736577abef
commit 13443310fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -160,6 +160,9 @@ class CastDevice(MediaPlayerEntity):
"elected leader" itself. "elected leader" itself.
""" """
_attr_should_poll = False
_attr_media_image_remotely_accessible = True
def __init__(self, cast_info: ChromecastInfo) -> None: def __init__(self, cast_info: ChromecastInfo) -> None:
"""Initialize the cast device.""" """Initialize the cast device."""
@ -172,12 +175,21 @@ class CastDevice(MediaPlayerEntity):
self.mz_media_status: dict[str, pychromecast.controllers.media.MediaStatus] = {} self.mz_media_status: dict[str, pychromecast.controllers.media.MediaStatus] = {}
self.mz_media_status_received: dict[str, datetime] = {} self.mz_media_status_received: dict[str, datetime] = {}
self.mz_mgr = None self.mz_mgr = None
self._available = False self._attr_available = False
self._status_listener: CastStatusListener | None = None self._status_listener: CastStatusListener | None = None
self._hass_cast_controller: HomeAssistantController | None = None self._hass_cast_controller: HomeAssistantController | None = None
self._add_remove_handler = None self._add_remove_handler = None
self._cast_view_remove_handler = None self._cast_view_remove_handler = None
self._attr_unique_id = cast_info.uuid
self._attr_name = cast_info.friendly_name
if cast_info.model_name != "Google Cast Group":
self._attr_device_info = {
"name": str(cast_info.friendly_name),
"identifiers": {(CAST_DOMAIN, str(cast_info.uuid).replace("-", ""))},
"model": cast_info.model_name,
"manufacturer": str(cast_info.manufacturer),
}
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Create chromecast object when added to hass.""" """Create chromecast object when added to hass."""
@ -239,7 +251,7 @@ class CastDevice(MediaPlayerEntity):
self.mz_mgr = self.hass.data[CAST_MULTIZONE_MANAGER_KEY] self.mz_mgr = self.hass.data[CAST_MULTIZONE_MANAGER_KEY]
self._status_listener = CastStatusListener(self, chromecast, self.mz_mgr) self._status_listener = CastStatusListener(self, chromecast, self.mz_mgr)
self._available = False self._attr_available = False
self.cast_status = chromecast.status self.cast_status = chromecast.status
self.media_status = chromecast.media_controller.status self.media_status = chromecast.media_controller.status
self._chromecast.start() self._chromecast.start()
@ -255,7 +267,7 @@ class CastDevice(MediaPlayerEntity):
self.entity_id, self.entity_id,
self._cast_info.friendly_name, self._cast_info.friendly_name,
) )
self._available = False self._attr_available = False
self.async_write_ha_state() self.async_write_ha_state()
await self.hass.async_add_executor_job(self._chromecast.disconnect) await self.hass.async_add_executor_job(self._chromecast.disconnect)
@ -282,6 +294,10 @@ class CastDevice(MediaPlayerEntity):
def new_cast_status(self, cast_status): def new_cast_status(self, cast_status):
"""Handle updates of the cast status.""" """Handle updates of the cast status."""
self.cast_status = cast_status self.cast_status = cast_status
self._attr_volume_level = cast_status.volume_level if cast_status else None
self._attr_is_volume_muted = (
cast_status.volume_muted if self.cast_status else None
)
self.schedule_update_ha_state() self.schedule_update_ha_state()
def new_media_status(self, media_status): def new_media_status(self, media_status):
@ -334,13 +350,13 @@ class CastDevice(MediaPlayerEntity):
connection_status.status, connection_status.status,
) )
if connection_status.status == CONNECTION_STATUS_DISCONNECTED: if connection_status.status == CONNECTION_STATUS_DISCONNECTED:
self._available = False self._attr_available = False
self._invalidate() self._invalidate()
self.schedule_update_ha_state() self.schedule_update_ha_state()
return return
new_available = connection_status.status == CONNECTION_STATUS_CONNECTED new_available = connection_status.status == CONNECTION_STATUS_CONNECTED
if new_available != self._available: if new_available != self.available:
# Connection status callbacks happen often when disconnected. # Connection status callbacks happen often when disconnected.
# Only update state when availability changed to put less pressure # Only update state when availability changed to put less pressure
# on state machine. # on state machine.
@ -350,7 +366,7 @@ class CastDevice(MediaPlayerEntity):
self._cast_info.friendly_name, self._cast_info.friendly_name,
connection_status.status, connection_status.status,
) )
self._available = new_available self._attr_available = new_available
self.schedule_update_ha_state() self.schedule_update_ha_state()
def multizone_new_media_status(self, group_uuid, media_status): def multizone_new_media_status(self, group_uuid, media_status):
@ -527,32 +543,6 @@ class CastDevice(MediaPlayerEntity):
media_id, media_type, **kwargs.get(ATTR_MEDIA_EXTRA, {}) media_id, media_type, **kwargs.get(ATTR_MEDIA_EXTRA, {})
) )
# ========== Properties ==========
@property
def should_poll(self):
"""No polling needed."""
return False
@property
def name(self):
"""Return the name of the device."""
return self._cast_info.friendly_name
@property
def device_info(self):
"""Return information about the device."""
cast_info = self._cast_info
if cast_info.model_name == "Google Cast Group":
return None
return {
"name": cast_info.friendly_name,
"identifiers": {(CAST_DOMAIN, cast_info.uuid.replace("-", ""))},
"model": cast_info.model_name,
"manufacturer": cast_info.manufacturer,
}
def _media_status(self): def _media_status(self):
""" """
Return media status. Return media status.
@ -589,21 +579,6 @@ class CastDevice(MediaPlayerEntity):
return STATE_OFF return STATE_OFF
return None return None
@property
def available(self):
"""Return True if the cast device is connected."""
return self._available
@property
def volume_level(self):
"""Volume level of the media player (0..1)."""
return self.cast_status.volume_level if self.cast_status else None
@property
def is_volume_muted(self):
"""Boolean if volume is currently muted."""
return self.cast_status.volume_muted if self.cast_status else None
@property @property
def media_content_id(self): def media_content_id(self):
"""Content ID of current playing media.""" """Content ID of current playing media."""
@ -641,11 +616,6 @@ class CastDevice(MediaPlayerEntity):
return images[0].url if images and images[0].url else None return images[0].url if images and images[0].url else None
@property
def media_image_remotely_accessible(self) -> bool:
"""If the image url is remotely accessible."""
return True
@property @property
def media_title(self): def media_title(self):
"""Title of current playing media.""" """Title of current playing media."""
@ -748,11 +718,6 @@ class CastDevice(MediaPlayerEntity):
media_status_recevied = self._media_status()[1] media_status_recevied = self._media_status()[1]
return media_status_recevied return media_status_recevied
@property
def unique_id(self) -> str | None:
"""Return a unique ID."""
return self._cast_info.uuid
async def _async_cast_discovered(self, discover: ChromecastInfo): async def _async_cast_discovered(self, discover: ChromecastInfo):
"""Handle discovery of new Chromecast.""" """Handle discovery of new Chromecast."""
if self._cast_info.uuid != discover.uuid: if self._cast_info.uuid != discover.uuid: