From a6d5ed01604d5f6dc813de43eb5881ba653171e9 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Fri, 27 Mar 2020 00:23:46 +0100 Subject: [PATCH] Add support for dashboards to lovelace cast service (#32913) --- .../components/cast/home_assistant_cast.py | 10 ++++++- homeassistant/components/cast/media_player.py | 8 ++++-- homeassistant/components/cast/services.yaml | 3 +++ .../cast/test_home_assistant_cast.py | 27 ++++++++++++++++++- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/cast/home_assistant_cast.py b/homeassistant/components/cast/home_assistant_cast.py index 0b8633e1916..c933136d140 100644 --- a/homeassistant/components/cast/home_assistant_cast.py +++ b/homeassistant/components/cast/home_assistant_cast.py @@ -12,6 +12,7 @@ from .const import DOMAIN, SIGNAL_HASS_CAST_SHOW_VIEW SERVICE_SHOW_VIEW = "show_lovelace_view" ATTR_VIEW_PATH = "view_path" +ATTR_URL_PATH = "dashboard_path" async def async_setup_ha_cast( @@ -63,11 +64,18 @@ async def async_setup_ha_cast( controller, call.data[ATTR_ENTITY_ID], call.data[ATTR_VIEW_PATH], + call.data.get(ATTR_URL_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}), + vol.Schema( + { + ATTR_ENTITY_ID: cv.entity_id, + ATTR_VIEW_PATH: str, + vol.Optional(ATTR_URL_PATH): str, + } + ), ) diff --git a/homeassistant/components/cast/media_player.py b/homeassistant/components/cast/media_player.py index 4e259038f14..e0c48062dfb 100644 --- a/homeassistant/components/cast/media_player.py +++ b/homeassistant/components/cast/media_player.py @@ -948,7 +948,11 @@ class CastDevice(MediaPlayerDevice): await self._async_disconnect() def _handle_signal_show_view( - self, controller: HomeAssistantController, entity_id: str, view_path: str + self, + controller: HomeAssistantController, + entity_id: str, + view_path: str, + url_path: Optional[str], ): """Handle a show view signal.""" if entity_id != self.entity_id: @@ -958,4 +962,4 @@ class CastDevice(MediaPlayerDevice): self._hass_cast_controller = controller self._chromecast.register_handler(controller) - self._hass_cast_controller.show_lovelace_view(view_path) + self._hass_cast_controller.show_lovelace_view(view_path, url_path) diff --git a/homeassistant/components/cast/services.yaml b/homeassistant/components/cast/services.yaml index 24bc7b16a90..d1c29281aad 100644 --- a/homeassistant/components/cast/services.yaml +++ b/homeassistant/components/cast/services.yaml @@ -4,6 +4,9 @@ show_lovelace_view: entity_id: description: Media Player entity to show the Lovelace view on. example: "media_player.kitchen" + dashboard_path: + description: The url path of the Lovelace dashboard to show. + example: lovelace-cast view_path: description: The path of the Lovelace view to show. example: downstairs diff --git a/tests/components/cast/test_home_assistant_cast.py b/tests/components/cast/test_home_assistant_cast.py index 10dd253704e..2ec02da7669 100644 --- a/tests/components/cast/test_home_assistant_cast.py +++ b/tests/components/cast/test_home_assistant_cast.py @@ -20,13 +20,38 @@ async def test_service_show_view(hass): ) assert len(calls) == 1 - controller, entity_id, view_path = calls[0] + controller, entity_id, view_path, url_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" + assert url_path is None + + +async def test_service_show_view_dashboard(hass): + """Test casting a specific dashboard.""" + 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", + "dashboard_path": "mock-dashboard", + }, + blocking=True, + ) + + assert len(calls) == 1 + _controller, entity_id, view_path, url_path = calls[0] + assert entity_id == "media_player.kitchen" + assert view_path == "mock_path" + assert url_path == "mock-dashboard" async def test_use_cloud_url(hass):