From 6f4829c390cf38ed6c727df1c2267ea4cc353af4 Mon Sep 17 00:00:00 2001 From: David Zhu Date: Wed, 27 May 2020 21:51:39 +0800 Subject: [PATCH] Add webostv payload option to command service (#36164) * added optional argument to command service * Fixed crash when optional argument is not provided * Updated argument description * fixed lint error * Fix isort error * switched to use dict for optional field instead of json string * switched to use ATTR_PAYLOAD * fixed test * actually fixed test --- homeassistant/components/webostv/__init__.py | 5 +++- homeassistant/components/webostv/const.py | 1 + .../components/webostv/media_player.py | 5 ++-- .../components/webostv/services.yaml | 7 +++++- tests/components/webostv/test_media_player.py | 23 ++++++++++++++++--- 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/webostv/__init__.py b/homeassistant/components/webostv/__init__.py index 0790ece9333..ebac158c452 100644 --- a/homeassistant/components/webostv/__init__.py +++ b/homeassistant/components/webostv/__init__.py @@ -9,6 +9,7 @@ from websockets.exceptions import ConnectionClosed from homeassistant.components.webostv.const import ( ATTR_BUTTON, ATTR_COMMAND, + ATTR_PAYLOAD, CONF_ON_ACTION, CONF_SOURCES, DEFAULT_NAME, @@ -59,7 +60,9 @@ CALL_SCHEMA = vol.Schema({vol.Required(ATTR_ENTITY_ID): cv.comp_entity_ids}) BUTTON_SCHEMA = CALL_SCHEMA.extend({vol.Required(ATTR_BUTTON): cv.string}) -COMMAND_SCHEMA = CALL_SCHEMA.extend({vol.Required(ATTR_COMMAND): cv.string}) +COMMAND_SCHEMA = CALL_SCHEMA.extend( + {vol.Required(ATTR_COMMAND): cv.string, vol.Optional(ATTR_PAYLOAD): dict} +) SOUND_OUTPUT_SCHEMA = CALL_SCHEMA.extend({vol.Required(ATTR_SOUND_OUTPUT): cv.string}) diff --git a/homeassistant/components/webostv/const.py b/homeassistant/components/webostv/const.py index 3e1e790fc02..bea485a7d68 100644 --- a/homeassistant/components/webostv/const.py +++ b/homeassistant/components/webostv/const.py @@ -5,6 +5,7 @@ DEFAULT_NAME = "LG webOS Smart TV" ATTR_BUTTON = "button" ATTR_COMMAND = "command" +ATTR_PAYLOAD = "payload" ATTR_SOUND_OUTPUT = "sound_output" CONF_ON_ACTION = "turn_on_action" diff --git a/homeassistant/components/webostv/media_player.py b/homeassistant/components/webostv/media_player.py index 3e443929012..556ff7a287b 100644 --- a/homeassistant/components/webostv/media_player.py +++ b/homeassistant/components/webostv/media_player.py @@ -24,6 +24,7 @@ from homeassistant.components.media_player.const import ( SUPPORT_VOLUME_STEP, ) from homeassistant.components.webostv.const import ( + ATTR_PAYLOAD, ATTR_SOUND_OUTPUT, CONF_ON_ACTION, CONF_SOURCES, @@ -450,6 +451,6 @@ class LgWebOSMediaPlayerEntity(MediaPlayerEntity): await self._client.button(button) @cmd - async def async_command(self, command): + async def async_command(self, command, **kwargs): """Send a command.""" - await self._client.request(command) + await self._client.request(command, payload=kwargs.get(ATTR_PAYLOAD)) diff --git a/homeassistant/components/webostv/services.yaml b/homeassistant/components/webostv/services.yaml index 70aa94b6ea6..b88b3d839d7 100644 --- a/homeassistant/components/webostv/services.yaml +++ b/homeassistant/components/webostv/services.yaml @@ -24,7 +24,12 @@ command: description: >- Endpoint of the command. Known valid endpoints are listed in https://github.com/TheRealLink/pylgtv/blob/master/pylgtv/endpoints.py - example: "media.controls/rewind" + example: "system.launcher/open" + payload: + description: >- + An optional payload to provide to the endpoint in the format of key value pair(s). + example: >- + target: https://www.google.com select_sound_output: description: "Send the TV the command to change sound output." diff --git a/tests/components/webostv/test_media_player.py b/tests/components/webostv/test_media_player.py index 099927c6c1f..3b213303281 100644 --- a/tests/components/webostv/test_media_player.py +++ b/tests/components/webostv/test_media_player.py @@ -12,6 +12,7 @@ from homeassistant.components.media_player.const import ( from homeassistant.components.webostv.const import ( ATTR_BUTTON, ATTR_COMMAND, + ATTR_PAYLOAD, DOMAIN, SERVICE_BUTTON, SERVICE_COMMAND, @@ -102,8 +103,7 @@ async def test_button(hass, client): async def test_command(hass, client): - """Test generic button functionality.""" - + """Test generic command functionality.""" await setup_webostv(hass) data = { @@ -113,4 +113,21 @@ async def test_command(hass, client): await hass.services.async_call(DOMAIN, SERVICE_COMMAND, data) await hass.async_block_till_done() - client.request.assert_called_with("test") + client.request.assert_called_with("test", payload=None) + + +async def test_command_with_optional_arg(hass, client): + """Test generic command functionality.""" + await setup_webostv(hass) + + data = { + ATTR_ENTITY_ID: ENTITY_ID, + ATTR_COMMAND: "test", + ATTR_PAYLOAD: {"target": "https://www.google.com"}, + } + await hass.services.async_call(DOMAIN, SERVICE_COMMAND, data) + await hass.async_block_till_done() + + client.request.assert_called_with( + "test", payload={"target": "https://www.google.com"} + )