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
This commit is contained in:
David Zhu 2020-05-27 21:51:39 +08:00 committed by GitHub
parent c7e97f0cf8
commit 6f4829c390
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 7 deletions

View File

@ -9,6 +9,7 @@ from websockets.exceptions import ConnectionClosed
from homeassistant.components.webostv.const import ( from homeassistant.components.webostv.const import (
ATTR_BUTTON, ATTR_BUTTON,
ATTR_COMMAND, ATTR_COMMAND,
ATTR_PAYLOAD,
CONF_ON_ACTION, CONF_ON_ACTION,
CONF_SOURCES, CONF_SOURCES,
DEFAULT_NAME, 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}) 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}) SOUND_OUTPUT_SCHEMA = CALL_SCHEMA.extend({vol.Required(ATTR_SOUND_OUTPUT): cv.string})

View File

@ -5,6 +5,7 @@ DEFAULT_NAME = "LG webOS Smart TV"
ATTR_BUTTON = "button" ATTR_BUTTON = "button"
ATTR_COMMAND = "command" ATTR_COMMAND = "command"
ATTR_PAYLOAD = "payload"
ATTR_SOUND_OUTPUT = "sound_output" ATTR_SOUND_OUTPUT = "sound_output"
CONF_ON_ACTION = "turn_on_action" CONF_ON_ACTION = "turn_on_action"

View File

@ -24,6 +24,7 @@ from homeassistant.components.media_player.const import (
SUPPORT_VOLUME_STEP, SUPPORT_VOLUME_STEP,
) )
from homeassistant.components.webostv.const import ( from homeassistant.components.webostv.const import (
ATTR_PAYLOAD,
ATTR_SOUND_OUTPUT, ATTR_SOUND_OUTPUT,
CONF_ON_ACTION, CONF_ON_ACTION,
CONF_SOURCES, CONF_SOURCES,
@ -450,6 +451,6 @@ class LgWebOSMediaPlayerEntity(MediaPlayerEntity):
await self._client.button(button) await self._client.button(button)
@cmd @cmd
async def async_command(self, command): async def async_command(self, command, **kwargs):
"""Send a command.""" """Send a command."""
await self._client.request(command) await self._client.request(command, payload=kwargs.get(ATTR_PAYLOAD))

View File

@ -24,7 +24,12 @@ command:
description: >- description: >-
Endpoint of the command. Known valid endpoints are listed in Endpoint of the command. Known valid endpoints are listed in
https://github.com/TheRealLink/pylgtv/blob/master/pylgtv/endpoints.py 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: select_sound_output:
description: "Send the TV the command to change sound output." description: "Send the TV the command to change sound output."

View File

@ -12,6 +12,7 @@ from homeassistant.components.media_player.const import (
from homeassistant.components.webostv.const import ( from homeassistant.components.webostv.const import (
ATTR_BUTTON, ATTR_BUTTON,
ATTR_COMMAND, ATTR_COMMAND,
ATTR_PAYLOAD,
DOMAIN, DOMAIN,
SERVICE_BUTTON, SERVICE_BUTTON,
SERVICE_COMMAND, SERVICE_COMMAND,
@ -102,8 +103,7 @@ async def test_button(hass, client):
async def test_command(hass, client): async def test_command(hass, client):
"""Test generic button functionality.""" """Test generic command functionality."""
await setup_webostv(hass) await setup_webostv(hass)
data = { data = {
@ -113,4 +113,21 @@ async def test_command(hass, client):
await hass.services.async_call(DOMAIN, SERVICE_COMMAND, data) await hass.services.async_call(DOMAIN, SERVICE_COMMAND, data)
await hass.async_block_till_done() 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"}
)