Skip to content

Commit

Permalink
Revert changes of home-assistant#16471, and fix the platform setup issue
Browse files Browse the repository at this point in the history
  • Loading branch information
awarecan committed Sep 23, 2018
1 parent fce275d commit a094bad
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions homeassistant/components/media_player/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@
vol.All(cv.ensure_list, [cv.string]),
})

CONNECTION_RETRY = 3
CONNECTION_RETRY_WAIT = 2
CONNECTION_TIMEOUT = 10


@attr.s(slots=True, frozen=True)
class ChromecastInfo:
Expand Down Expand Up @@ -217,13 +213,18 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
if not isinstance(config, list):
config = [config]

await asyncio.wait([
done, pending = await asyncio.wait([
_async_setup_platform(hass, cfg, async_add_entities, None)
for cfg in config])
for d in done:
print(d.result())
if pending or any([not task.result() for task in done]):
raise PlatformNotReady
# raise PlatformNotReady


async def _async_setup_platform(hass: HomeAssistantType, config: ConfigType,
async_add_entities, discovery_info):
async_add_entities, discovery_info) -> bool:
"""Set up the cast platform."""
import pychromecast

Expand Down Expand Up @@ -251,8 +252,8 @@ def async_cast_discovered(discover: ChromecastInfo) -> None:
if cast_device is not None:
async_add_entities([cast_device])

async_dispatcher_connect(hass, SIGNAL_CAST_DISCOVERED,
async_cast_discovered)
remove_handler = async_dispatcher_connect(
hass, SIGNAL_CAST_DISCOVERED, async_cast_discovered)
# Re-play the callback for all past chromecasts, store the objects in
# a list to avoid concurrent modification resulting in exception.
for chromecast in list(hass.data[KNOWN_CHROMECAST_INFO_KEY]):
Expand All @@ -266,10 +267,20 @@ def async_cast_discovered(discover: ChromecastInfo) -> None:
info = await hass.async_add_job(_fill_out_missing_chromecast_info,
info)
if info.friendly_name is None:
# HTTP dial failed, so we won't be able to connect.
raise PlatformNotReady
_LOGGER.debug("Cannot retrieve detail information for chromecast"
" %s, the device may not online", info)
if not hass.is_running:
# We don't want to block system setup
# If hass.is_running is True, means we are in the retry setup,
# create an unavailable device and open a socket to keep listen
# configured host
remove_handler()
return False

hass.async_add_job(_discover_chromecast, hass, info)

return True


class CastStatusListener:
"""Helper class to handle pychromecast status callbacks.
Expand Down Expand Up @@ -380,7 +391,7 @@ async def async_set_cast_info(self, cast_info):
pychromecast._get_chromecast_from_host, (
cast_info.host, cast_info.port, cast_info.uuid,
cast_info.model_name, cast_info.friendly_name
), CONNECTION_RETRY, CONNECTION_RETRY_WAIT, CONNECTION_TIMEOUT)
))
self._chromecast = chromecast
self._status_listener = CastStatusListener(self, chromecast)
# Initialise connection status as connected because we can only
Expand Down

0 comments on commit a094bad

Please sign in to comment.