-
-
Notifications
You must be signed in to change notification settings - Fork 37.6k
Home Assistant Cast #26566
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
Merged
Home Assistant Cast #26566
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| """Home Assistant Cast integration for Cast.""" | ||
| from typing import Optional | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| from pychromecast.controllers.homeassistant import HomeAssistantController | ||
|
|
||
| from homeassistant import auth, config_entries, core | ||
| from homeassistant.const import ATTR_ENTITY_ID | ||
| from homeassistant.helpers import config_validation as cv, dispatcher | ||
|
|
||
| from .const import DOMAIN, SIGNAL_HASS_CAST_SHOW_VIEW | ||
|
|
||
| SERVICE_SHOW_VIEW = "show_lovelace_view" | ||
| ATTR_VIEW_PATH = "view_path" | ||
|
|
||
|
|
||
| async def async_setup_ha_cast( | ||
| hass: core.HomeAssistant, entry: config_entries.ConfigEntry | ||
| ): | ||
| """Set up Home Assistant Cast.""" | ||
| user_id: Optional[str] = entry.data.get("user_id") | ||
| user: Optional[auth.models.User] = None | ||
|
|
||
| if user_id is not None: | ||
| user = await hass.auth.async_get_user(user_id) | ||
|
|
||
| if user is None: | ||
| user = await hass.auth.async_create_system_user( | ||
| "Home Assistant Cast", [auth.GROUP_ID_ADMIN] | ||
| ) | ||
| hass.config_entries.async_update_entry( | ||
| entry, data={**entry.data, "user_id": user.id} | ||
| ) | ||
|
|
||
| if user.refresh_tokens: | ||
| refresh_token: auth.models.RefreshToken = list(user.refresh_tokens.values())[0] | ||
| else: | ||
| refresh_token = await hass.auth.async_create_refresh_token(user) | ||
|
|
||
| async def handle_show_view(call: core.ServiceCall): | ||
| """Handle a Show View service call.""" | ||
| controller = HomeAssistantController( | ||
| # If you are developing Home Assistant Cast, uncomment and set to your dev app id. | ||
| # app_id="5FE44367", | ||
| hass_url=hass.config.api.base_url, | ||
| client_id=None, | ||
| refresh_token=refresh_token.token, | ||
| ) | ||
|
|
||
| dispatcher.async_dispatcher_send( | ||
| hass, | ||
| SIGNAL_HASS_CAST_SHOW_VIEW, | ||
| controller, | ||
| call.data[ATTR_ENTITY_ID], | ||
| call.data[ATTR_VIEW_PATH], | ||
| ) | ||
|
|
||
| hass.helpers.service.async_register_admin_service( | ||
| DOMAIN, | ||
| SERVICE_SHOW_VIEW, | ||
| handle_show_view, | ||
| vol.Schema({ATTR_ENTITY_ID: cv.entity_id, ATTR_VIEW_PATH: str}), | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| show_lovelace_view: | ||
| description: Show a Lovelace view on a Chromecast. | ||
| fields: | ||
| entity_id: | ||
| description: Media Player entity to show the Lovelace view on. | ||
| example: "media_player.kitchen" | ||
| view_path: | ||
| description: The path of the Lovelace view to show. | ||
| example: downstairs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| """Test Home Assistant Cast.""" | ||
| from unittest.mock import Mock | ||
| from homeassistant.components.cast import home_assistant_cast | ||
|
|
||
| from tests.common import MockConfigEntry, async_mock_signal | ||
|
|
||
|
|
||
| async def test_service_show_view(hass): | ||
| """Test we don't set app id in prod.""" | ||
| hass.config.api = Mock(base_url="http://example.com") | ||
| await home_assistant_cast.async_setup_ha_cast(hass, MockConfigEntry()) | ||
| calls = async_mock_signal(hass, home_assistant_cast.SIGNAL_HASS_CAST_SHOW_VIEW) | ||
|
|
||
| await hass.services.async_call( | ||
| "cast", | ||
| "show_lovelace_view", | ||
| {"entity_id": "media_player.kitchen", "view_path": "mock_path"}, | ||
| blocking=True, | ||
| ) | ||
|
|
||
| assert len(calls) == 1 | ||
| controller, entity_id, view_path = calls[0] | ||
| assert controller.hass_url == "http://example.com" | ||
| assert controller.client_id is None | ||
| # Verify user did not accidentally submit their dev app id | ||
| assert controller.supporting_app_id == "B12CE3CA" | ||
| assert entity_id == "media_player.kitchen" | ||
| assert view_path == "mock_path" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is this a random app_id?
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.
It's one of the dev app IDs.
https://github.com/home-assistant/home-assistant-polymer/blob/dev/src/cast/dev_const.ts#L2