Don't do http requests to determine Cast device details (#34082)

* Don't do http requests to determine cast device details

* Fix tests

* Update homeassistant/components/cast/media_player.py

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
Erik MontnemeryandPaulus Schoutsen authored and GitHub committed 2020-04-12 15:02:28 -07:00
1 parent 200e140e80
commit ec2c7ea932
4 files changed
+26 -139

No files matched your search

@@ -25,7 +25,6 @@ def discover_chromecast(hass: HomeAssistant, info: ChromecastInfo):
_LOGGER.debug("Discovered previous chromecast %s", info)
# Either discovered completely new chromecast or a "moved" one.
info = info.fill_out_missing_chromecast_info()
_LOGGER.debug("Discovered chromecast %s", info)
if info.uuid is not None:
-43
View File
@@ -2,7 +2,6 @@
from typing import Optional, Tuple
import attr
from pychromecast import dial
from pychromecast.const import CAST_MANUFACTURERS
from .const import DEFAULT_PORT
@@ -29,11 +28,6 @@ class ChromecastInfo:
"""Return if this is an audio group."""
return self.port != DEFAULT_PORT
@property
def is_information_complete(self) -> bool:
"""Return if all information is filled out."""
return all(attr.astuple(self))
@property
def host_port(self) -> Tuple[str, int]:
"""Return the host+port tuple."""
@@ -46,43 +40,6 @@ class ChromecastInfo:
return None
return CAST_MANUFACTURERS.get(self.model_name.lower(), "Google Inc.")
def fill_out_missing_chromecast_info(self) -> "ChromecastInfo":
"""Return a new ChromecastInfo object with missing attributes filled in.
Uses blocking HTTP.
"""
if self.is_information_complete:
# We have all information, no need to check HTTP API. Or this is an
# audio group, so checking via HTTP won't give us any new information.
return self
if self.is_audio_group:
return ChromecastInfo(
service=self.service,
host=self.host,
port=self.port,
uuid=self.uuid,
friendly_name=self.friendly_name,
model_name=self.model_name,
)
# Fill out some missing information (friendly_name, uuid) via HTTP dial.
http_device_status = dial.get_device_status(
self.host, services=[self.service], zconf=ChromeCastZeroconf.get_zeroconf()
)
if http_device_status is None:
# HTTP dial didn't give us any new information.
return self
return ChromecastInfo(
service=self.service,
host=self.host,
port=self.port,
uuid=(self.uuid or http_device_status.uuid),
friendly_name=(self.friendly_name or http_device_status.friendly_name),
model_name=self.model_name,
)
class ChromeCastZeroconf:
"""Class to hold a zeroconf instance."""
+20 -45
View File
@@ -91,9 +91,8 @@ def _async_create_cast_device(hass: HomeAssistantType, info: ChromecastInfo):
"""
_LOGGER.debug("_async_create_cast_device: %s", info)
if info.uuid is None:
# Found a cast without UUID, we don't store it because we won't be able
# to update it anyway.
return CastDevice(info)
_LOGGER.error("_async_create_cast_device uuid none: %s", info)
return None
# Found a cast with UUID
added_casts = hass.data[ADDED_CAST_DEVICES_KEY]
@@ -156,7 +155,7 @@ async def _async_setup_platform(
def async_cast_discovered(discover: ChromecastInfo) -> None:
"""Handle discovery of a new chromecast."""
if info is not None and info.host_port != discover.host_port:
# Not our requested cast device.
# Waiting for a specific cast device, this is not it.
return
cast_device = _async_create_cast_device(hass, discover)
@@ -262,44 +261,24 @@ class CastDevice(MediaPlayerDevice):
return
# pylint: disable=protected-access
if self.services is None:
_LOGGER.debug(
"[%s %s (%s:%s)] Connecting to cast device by host %s",
self.entity_id,
self._cast_info.friendly_name,
self._cast_info.host,
self._cast_info.port,
cast_info,
)
chromecast = await self.hass.async_add_job(
pychromecast._get_chromecast_from_host,
(
cast_info.host,
cast_info.port,
cast_info.uuid,
cast_info.model_name,
cast_info.friendly_name,
),
)
else:
_LOGGER.debug(
"[%s %s (%s:%s)] Connecting to cast device by service %s",
self.entity_id,
self._cast_info.friendly_name,
self._cast_info.host,
self._cast_info.port,
_LOGGER.debug(
"[%s %s (%s:%s)] Connecting to cast device by service %s",
self.entity_id,
self._cast_info.friendly_name,
self._cast_info.host,
self._cast_info.port,
self.services,
)
chromecast = await self.hass.async_add_executor_job(
pychromecast._get_chromecast_from_service,
(
self.services,
)
chromecast = await self.hass.async_add_job(
pychromecast._get_chromecast_from_service,
(
self.services,
ChromeCastZeroconf.get_zeroconf(),
cast_info.uuid,
cast_info.model_name,
cast_info.friendly_name,
),
)
ChromeCastZeroconf.get_zeroconf(),
cast_info.uuid,
cast_info.model_name,
cast_info.friendly_name,
),
)
self._chromecast = chromecast
if CAST_MULTIZONE_MANAGER_KEY not in self.hass.data:
@@ -403,10 +382,6 @@ class CastDevice(MediaPlayerDevice):
self._cast_info.port,
connection_status.status,
)
info = self._cast_info
if info.friendly_name is None and not info.is_audio_group:
# We couldn't find friendly_name when the cast was added, retry
self._cast_info = info.fill_out_missing_chromecast_info()
self._available = new_available
self.schedule_update_ha_state()