Bump pychromecast to 9.0.0 (#47086)

* Adapt to Pychromecast 9.0.0

* Bump pychromecast to 9.0.0

* Fix lint issues
This commit is contained in:
Erik Montnemery 2021-02-26 13:43:53 +01:00 committed by GitHub
parent d5ee49cd4e
commit dfbb653107
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 134 additions and 146 deletions

View file

@ -9,6 +9,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import dispatcher_send
from .const import (
DEFAULT_PORT,
INTERNAL_DISCOVERY_RUNNING_KEY,
KNOWN_CHROMECAST_INFO_KEY,
SIGNAL_CAST_DISCOVERED,
@ -19,8 +20,17 @@ from .helpers import ChromecastInfo, ChromeCastZeroconf
_LOGGER = logging.getLogger(__name__)
def discover_chromecast(hass: HomeAssistant, info: ChromecastInfo):
def discover_chromecast(hass: HomeAssistant, device_info):
"""Discover a Chromecast."""
info = ChromecastInfo(
services=device_info.services,
uuid=device_info.uuid,
model_name=device_info.model_name,
friendly_name=device_info.friendly_name,
is_audio_group=device_info.port != DEFAULT_PORT,
)
if info.uuid is None:
_LOGGER.error("Discovered chromecast without uuid %s", info)
return
@ -51,72 +61,39 @@ def setup_internal_discovery(hass: HomeAssistant) -> None:
# Internal discovery is already running
return
def internal_add_update_callback(uuid, service_name):
"""Handle zeroconf discovery of a new or updated chromecast."""
service = listener.services[uuid]
class CastListener(pychromecast.discovery.AbstractCastListener):
"""Listener for discovering chromecasts."""
# For support of deprecated IP based white listing
zconf = ChromeCastZeroconf.get_zeroconf()
service_info = None
tries = 0
while service_info is None and tries < 4:
try:
service_info = zconf.get_service_info(
"_googlecast._tcp.local.", service_name
)
except OSError:
# If the zeroconf fails to receive the necessary data we abort
# adding the service
break
tries += 1
def add_cast(self, uuid, _):
"""Handle zeroconf discovery of a new chromecast."""
discover_chromecast(hass, browser.devices[uuid])
if not service_info:
_LOGGER.warning(
"setup_internal_discovery failed to get info for %s, %s",
uuid,
service_name,
def update_cast(self, uuid, _):
"""Handle zeroconf discovery of an updated chromecast."""
discover_chromecast(hass, browser.devices[uuid])
def remove_cast(self, uuid, service, cast_info):
"""Handle zeroconf discovery of a removed chromecast."""
_remove_chromecast(
hass,
ChromecastInfo(
services=cast_info.services,
uuid=cast_info.uuid,
model_name=cast_info.model_name,
friendly_name=cast_info.friendly_name,
),
)
return
addresses = service_info.parsed_addresses()
host = addresses[0] if addresses else service_info.server
discover_chromecast(
hass,
ChromecastInfo(
services=service[0],
uuid=service[1],
model_name=service[2],
friendly_name=service[3],
host=host,
port=service_info.port,
),
)
def internal_remove_callback(uuid, service_name, service):
"""Handle zeroconf discovery of a removed chromecast."""
_remove_chromecast(
hass,
ChromecastInfo(
services=service[0],
uuid=service[1],
model_name=service[2],
friendly_name=service[3],
),
)
_LOGGER.debug("Starting internal pychromecast discovery")
listener = pychromecast.CastListener(
internal_add_update_callback,
internal_remove_callback,
internal_add_update_callback,
browser = pychromecast.discovery.CastBrowser(
CastListener(), ChromeCastZeroconf.get_zeroconf()
)
browser = pychromecast.start_discovery(listener, ChromeCastZeroconf.get_zeroconf())
browser.start_discovery()
def stop_discovery(event):
"""Stop discovery of new chromecasts."""
_LOGGER.debug("Stopping internal pychromecast discovery")
pychromecast.discovery.stop_discovery(browser)
browser.stop_discovery()
hass.data[INTERNAL_DISCOVERY_RUNNING_KEY].release()
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_discovery)