Skip to content

Commit

Permalink
Add webostv sound_output capability (#31121)
Browse files Browse the repository at this point in the history
* Webostv: add sound_output capability

Add the ability to read and set the sound_output

* Webostv: add sound_output capability

* Webostv: add sound_output capability

* fix blank spaces

* fix to long line

* add ,

* Import ATTR_SOUND_OUTPUT

Do not have the ability to test this change right now

* Add white space

* Create const.py

* Use const import

* Use import from const.py

* Add docstring

* Change order

* Change order

* Fix import

* Fix import

* Fix typo

* Change order again

* Change order again

* Change order of attributes
  • Loading branch information
starkillerOG authored and MartinHjelmare committed Jan 27, 2020
1 parent 1f0f62d commit 1278f32
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
10 changes: 10 additions & 0 deletions homeassistant/components/webostv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_send

from .const import ATTR_SOUND_OUTPUT

DOMAIN = "webostv"

CONF_SOURCES = "sources"
Expand All @@ -30,6 +32,8 @@
SERVICE_COMMAND = "command"
ATTR_COMMAND = "command"

SERVICE_SELECT_SOUND_OUTPUT = "select_sound_output"

CUSTOMIZE_SCHEMA = vol.Schema(
{vol.Optional(CONF_SOURCES, default=[]): vol.All(cv.ensure_list, [cv.string])}
)
Expand Down Expand Up @@ -60,9 +64,15 @@

COMMAND_SCHEMA = CALL_SCHEMA.extend({vol.Required(ATTR_COMMAND): cv.string})

SOUND_OUTPUT_SCHEMA = CALL_SCHEMA.extend({vol.Required(ATTR_SOUND_OUTPUT): cv.string})

SERVICE_TO_METHOD = {
SERVICE_BUTTON: {"method": "async_button", "schema": BUTTON_SCHEMA},
SERVICE_COMMAND: {"method": "async_command", "schema": COMMAND_SCHEMA},
SERVICE_SELECT_SOUND_OUTPUT: {
"method": "async_select_sound_output",
"schema": SOUND_OUTPUT_SCHEMA,
},
}

_LOGGER = logging.getLogger(__name__)
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/webostv/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""Constants used for WebOS TV."""
LIVE_TV_APP_ID = "com.webos.app.livetv"

ATTR_SOUND_OUTPUT = "sound_output"
23 changes: 16 additions & 7 deletions homeassistant/components/webostv/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@
from homeassistant.helpers.script import Script

from . import CONF_ON_ACTION, CONF_SOURCES, DOMAIN
from .const import ATTR_SOUND_OUTPUT, LIVE_TV_APP_ID

_LOGGER = logging.getLogger(__name__)


LIVETV_APP_ID = "com.webos.app.livetv"


SUPPORT_WEBOSTV = (
SUPPORT_TURN_OFF
| SUPPORT_NEXT_TRACK
Expand All @@ -59,8 +57,6 @@
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(seconds=1)

LIVE_TV_APP_ID = "com.webos.app.livetv"


async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the LG WebOS TV platform."""
Expand Down Expand Up @@ -289,6 +285,14 @@ def supported_features(self):
return SUPPORT_WEBOSTV | SUPPORT_TURN_ON
return SUPPORT_WEBOSTV

@property
def device_state_attributes(self):
"""Return device specific state attributes."""
attributes = {}
if self._client.sound_output is not None and self.state != STATE_OFF:
attributes[ATTR_SOUND_OUTPUT] = self._client.sound_output
return attributes

@cmd
async def async_turn_off(self):
"""Turn off media player."""
Expand Down Expand Up @@ -320,6 +324,11 @@ async def async_mute_volume(self, mute):
"""Send mute command."""
await self._client.set_mute(mute)

@cmd
async def async_select_sound_output(self, sound_output):
"""Select the sound output."""
await self._client.change_sound_output(sound_output)

@cmd
async def async_media_play_pause(self):
"""Simulate play pause media player."""
Expand Down Expand Up @@ -396,7 +405,7 @@ async def async_media_stop(self):
async def async_media_next_track(self):
"""Send next track command."""
current_input = self._client.get_input()
if current_input == LIVETV_APP_ID:
if current_input == LIVE_TV_APP_ID:
await self._client.channel_up()
else:
await self._client.fast_forward()
Expand All @@ -405,7 +414,7 @@ async def async_media_next_track(self):
async def async_media_previous_track(self):
"""Send the previous track command."""
current_input = self._client.get_input()
if current_input == LIVETV_APP_ID:
if current_input == LIVE_TV_APP_ID:
await self._client.channel_down()
else:
await self._client.rewind()
Expand Down
9 changes: 9 additions & 0 deletions homeassistant/components/webostv/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@ command:
https://github.com/TheRealLink/pylgtv/blob/master/pylgtv/endpoints.py
example: 'media.controls/rewind'

select_sound_output:
description: 'Send the TV the command to change sound output.'
fields:
entity_id:
description: Name(s) of the webostv entities to change sound output on.
example: 'media_player.living_room_tv'
sound_output:
description: Name of the sound output to switch to.
example: 'external_speaker'

0 comments on commit 1278f32

Please sign in to comment.