-
-
Notifications
You must be signed in to change notification settings - Fork 38k
Rework chromecast fix #16804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rework chromecast fix #16804
Changes from 5 commits
00a3438
897934f
73b4e2e
abbe32c
c480850
0fc48ad
b0ef45e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,10 +61,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: | ||
|
|
@@ -206,8 +202,9 @@ async def async_setup_platform(hass: HomeAssistantType, config: ConfigType, | |
| _LOGGER.warning( | ||
| 'Setting configuration for Cast via platform is deprecated. ' | ||
| 'Configure via Cast component instead.') | ||
| await _async_setup_platform( | ||
| hass, config, async_add_entities, discovery_info) | ||
| if not await _async_setup_platform( | ||
| hass, config, async_add_entities, discovery_info): | ||
| raise PlatformNotReady | ||
|
|
||
|
|
||
| async def async_setup_entry(hass, config_entry, async_add_entities): | ||
|
|
@@ -216,13 +213,15 @@ 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]) | ||
| if pending or any([not task.result() for task in done]): | ||
| raise PlatformNotReady | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if the setup for two chromecast succeeded but failed for a third chromecast? Then I think this code would raise a If the devices have no UUID (don't know if this can be the case with config entries), this could end up creating new entities for the two connectable chromecasts every 10 seconds.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will get the message such as |
||
|
|
||
|
|
||
| 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 | ||
|
|
||
|
|
@@ -250,8 +249,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]): | ||
|
|
@@ -265,10 +264,15 @@ 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| remove_handler() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes, removing the handler is a good idea here 👍 |
||
| return False | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style-wise I find returning a boolean a bit bad. async def _async_setup_platform(hass: HomeAssistantType, config: ConfigType,
async_add_entities, discovery_info):
# ...
if info.friendly_name is None:
# HTTP dial failed, so we won't be able to connect.
remove_handler()
raise PlatformNotReady
# ...
async def async_setup_entry(hass, config_entry, async_add_entities):
# ...
done, pending = await asyncio.wait([
_async_setup_platform(hass, cfg, async_add_entities, None)
for cfg in config], timeout=10)
# ... |
||
|
|
||
| hass.async_add_job(_discover_chromecast, hass, info) | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| class CastStatusListener: | ||
| """Helper class to handle pychromecast status callbacks. | ||
|
|
@@ -379,7 +383,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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't
pendingalways empty here? From what I understand from the docs,pendingcontains a set of tasks that are not done when the timeout occurs. But there is no timeout configured here...Also, it would be good to cancel all tasks in
pending.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed pending