Updated Chromecast plugin to work with pychromecast >= 10.0

pychromecast 10.0 introduced some [breaking changes](https://github.com/home-assistant-libs/pychromecast/pull/556/files)
in the declaration of the Chromecast object -
namely, the `device` attribute has been renamed to
`cast_info`. The code of ChromecastPlugin has been
updated to guarantee compatibility in both cases.
This commit is contained in:
Fabio Manganiello 2021-12-11 22:14:47 +01:00
parent 2560bfa03f
commit 20fc3d91fc
Signed by: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -132,6 +132,12 @@ class MediaChromecastPlugin(MediaPlugin):
return chromecasts[0] return chromecasts[0]
return chromecasts return chromecasts
@staticmethod
def _get_device_property(cc, prop: str):
if hasattr(cc, 'device'): # Previous pychromecast API
return getattr(cc.device, prop)
return getattr(cc.cast_info, prop)
@action @action
def get_chromecasts(self, tries=2, retry_wait=10, timeout=60, def get_chromecasts(self, tries=2, retry_wait=10, timeout=60,
blocking=True, callback=None): blocking=True, callback=None):
@ -156,9 +162,8 @@ class MediaChromecastPlugin(MediaPlugin):
will be invoked when a new device is discovered will be invoked when a new device is discovered
:type callback: func :type callback: func
""" """
self.chromecasts.update({ self.chromecasts.update({
cast.device.friendly_name: cast self._get_device_property(cast, 'friendly_name'): cast
for cast in self._get_chromecasts(tries=tries, retry_wait=retry_wait, for cast in self._get_chromecasts(tries=tries, retry_wait=retry_wait,
timeout=timeout, blocking=blocking, timeout=timeout, blocking=blocking,
callback=callback) callback=callback)
@ -170,9 +175,9 @@ class MediaChromecastPlugin(MediaPlugin):
return [{ return [{
'type': cc.cast_type, 'type': cc.cast_type,
'name': cc.name, 'name': cc.name,
'manufacturer': cc.device.manufacturer, 'manufacturer': self._get_device_property(cc, 'manufacturer'),
'model_name': cc.model_name, 'model_name': cc.model_name,
'uuid': str(cc.device.uuid), 'uuid': str(cc.uuid),
'address': cc.host if hasattr(cc, 'host') else cc.uri.split(':')[0], 'address': cc.host if hasattr(cc, 'host') else cc.uri.split(':')[0],
'port': cc.port if hasattr(cc, 'port') else int(cc.uri.split(':')[1]), 'port': cc.port if hasattr(cc, 'port') else int(cc.uri.split(':')[1]),
@ -213,7 +218,7 @@ class MediaChromecastPlugin(MediaPlugin):
while n_tries > 0: while n_tries > 0:
n_tries -= 1 n_tries -= 1
casts.update({ casts.update({
cast.device.friendly_name: cast self._get_device_property(cast, 'friendly_name'): cast
for cast in self._get_chromecasts() for cast in self._get_chromecasts()
}) })