Improve LG webosTV (#34147)

* Move consts to const, general cleanup

* Add unique id

* Add default icon

* Set supported features based on sound output

* Update homeassistant/components/webostv/media_player.py

Co-Authored-By: Martin Hjelmare <marhje52@gmail.com>

* Set device class

* Add software_info to client mock

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Aidan Timson 2020-04-14 19:26:13 +01:00 committed by GitHub
parent ba394fd2aa
commit 18478ebd05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 34 deletions

View File

@ -1,4 +1,4 @@
"""Support for WebOS TV.""" """Support for LG webOS Smart TV."""
import asyncio import asyncio
import logging import logging
@ -6,6 +6,18 @@ from aiopylgtv import PyLGTVCmdException, PyLGTVPairException, WebOsClient
import voluptuous as vol import voluptuous as vol
from websockets.exceptions import ConnectionClosed from websockets.exceptions import ConnectionClosed
from homeassistant.components.webostv.const import (
ATTR_BUTTON,
ATTR_COMMAND,
CONF_ON_ACTION,
CONF_SOURCES,
DEFAULT_NAME,
DOMAIN,
SERVICE_BUTTON,
SERVICE_COMMAND,
SERVICE_SELECT_SOUND_OUTPUT,
WEBOSTV_CONFIG_FILE,
)
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
CONF_CUSTOMIZE, CONF_CUSTOMIZE,
@ -19,21 +31,6 @@ from homeassistant.helpers.dispatcher import async_dispatcher_send
from .const import ATTR_SOUND_OUTPUT from .const import ATTR_SOUND_OUTPUT
DOMAIN = "webostv"
CONF_SOURCES = "sources"
CONF_ON_ACTION = "turn_on_action"
DEFAULT_NAME = "LG webOS Smart TV"
WEBOSTV_CONFIG_FILE = "webostv.conf"
SERVICE_BUTTON = "button"
ATTR_BUTTON = "button"
SERVICE_COMMAND = "command"
ATTR_COMMAND = "command"
SERVICE_SELECT_SOUND_OUTPUT = "select_sound_output"
CUSTOMIZE_SCHEMA = vol.Schema( CUSTOMIZE_SCHEMA = vol.Schema(
{vol.Optional(CONF_SOURCES, default=[]): vol.All(cv.ensure_list, [cv.string])} {vol.Optional(CONF_SOURCES, default=[]): vol.All(cv.ensure_list, [cv.string])}
) )

View File

@ -1,4 +1,19 @@
"""Constants used for WebOS TV.""" """Constants used for LG webOS Smart TV."""
DOMAIN = "webostv"
DEFAULT_NAME = "LG webOS Smart TV"
ATTR_BUTTON = "button"
ATTR_COMMAND = "command"
ATTR_SOUND_OUTPUT = "sound_output"
CONF_ON_ACTION = "turn_on_action"
CONF_SOURCES = "sources"
SERVICE_BUTTON = "button"
SERVICE_COMMAND = "command"
SERVICE_SELECT_SOUND_OUTPUT = "select_sound_output"
LIVE_TV_APP_ID = "com.webos.app.livetv" LIVE_TV_APP_ID = "com.webos.app.livetv"
ATTR_SOUND_OUTPUT = "sound_output" WEBOSTV_CONFIG_FILE = "webostv.conf"

View File

@ -4,11 +4,11 @@ from datetime import timedelta
from functools import wraps from functools import wraps
import logging import logging
from aiopylgtv import PyLGTVCmdException, PyLGTVPairException from aiopylgtv import PyLGTVCmdException, PyLGTVPairException, WebOsClient
from websockets.exceptions import ConnectionClosed from websockets.exceptions import ConnectionClosed
from homeassistant import util from homeassistant import util
from homeassistant.components.media_player import MediaPlayerDevice from homeassistant.components.media_player import DEVICE_CLASS_TV, MediaPlayerDevice
from homeassistant.components.media_player.const import ( from homeassistant.components.media_player.const import (
MEDIA_TYPE_CHANNEL, MEDIA_TYPE_CHANNEL,
SUPPORT_NEXT_TRACK, SUPPORT_NEXT_TRACK,
@ -23,6 +23,13 @@ from homeassistant.components.media_player.const import (
SUPPORT_VOLUME_SET, SUPPORT_VOLUME_SET,
SUPPORT_VOLUME_STEP, SUPPORT_VOLUME_STEP,
) )
from homeassistant.components.webostv.const import (
ATTR_SOUND_OUTPUT,
CONF_ON_ACTION,
CONF_SOURCES,
DOMAIN,
LIVE_TV_APP_ID,
)
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
CONF_CUSTOMIZE, CONF_CUSTOMIZE,
@ -36,31 +43,26 @@ from homeassistant.const import (
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.script import Script 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__) _LOGGER = logging.getLogger(__name__)
SUPPORT_WEBOSTV = ( SUPPORT_WEBOSTV = (
SUPPORT_TURN_OFF SUPPORT_TURN_OFF
| SUPPORT_NEXT_TRACK | SUPPORT_NEXT_TRACK
| SUPPORT_PAUSE | SUPPORT_PAUSE
| SUPPORT_PREVIOUS_TRACK | SUPPORT_PREVIOUS_TRACK
| SUPPORT_VOLUME_MUTE
| SUPPORT_VOLUME_SET
| SUPPORT_VOLUME_STEP
| SUPPORT_SELECT_SOURCE | SUPPORT_SELECT_SOURCE
| SUPPORT_PLAY_MEDIA | SUPPORT_PLAY_MEDIA
| SUPPORT_PLAY | SUPPORT_PLAY
) )
SUPPORT_WEBOSTV_VOLUME = SUPPORT_VOLUME_MUTE | SUPPORT_VOLUME_STEP
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10) MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(seconds=1) MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(seconds=1)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the LG WebOS TV platform.""" """Set up the LG webOS Smart TV platform."""
if discovery_info is None: if discovery_info is None:
return return
@ -108,12 +110,13 @@ def cmd(func):
class LgWebOSMediaPlayerEntity(MediaPlayerDevice): class LgWebOSMediaPlayerEntity(MediaPlayerDevice):
"""Representation of a LG WebOS TV.""" """Representation of a LG webOS Smart TV."""
def __init__(self, client, name, customize, on_script=None): def __init__(self, client: WebOsClient, name: str, customize, on_script=None):
"""Initialize the webos device.""" """Initialize the webos device."""
self._client = client self._client = client
self._name = name self._name = name
self._unique_id = client.software_info["device_id"]
self._customize = customize self._customize = customize
self._on_script = on_script self._on_script = on_script
@ -219,11 +222,21 @@ class LgWebOSMediaPlayerEntity(MediaPlayerDevice):
): ):
pass pass
@property
def unique_id(self):
"""Return the unique id of the device."""
return self._unique_id
@property @property
def name(self): def name(self):
"""Return the name of the device.""" """Return the name of the device."""
return self._name return self._name
@property
def device_class(self):
"""Return the device class of the device."""
return DEVICE_CLASS_TV
@property @property
def state(self): def state(self):
"""Return the state of the device.""" """Return the state of the device."""
@ -285,9 +298,17 @@ class LgWebOSMediaPlayerEntity(MediaPlayerDevice):
@property @property
def supported_features(self): def supported_features(self):
"""Flag media player features that are supported.""" """Flag media player features that are supported."""
supported = SUPPORT_WEBOSTV
if self._client.sound_output == "external_arc":
supported = supported | SUPPORT_WEBOSTV_VOLUME
elif self._client.sound_output != "lineout":
supported = supported | SUPPORT_WEBOSTV_VOLUME | SUPPORT_VOLUME_SET
if self._on_script: if self._on_script:
return SUPPORT_WEBOSTV | SUPPORT_TURN_ON supported = supported | SUPPORT_TURN_ON
return SUPPORT_WEBOSTV
return supported
@property @property
def device_state_attributes(self): def device_state_attributes(self):

View File

@ -9,7 +9,7 @@ from homeassistant.components.media_player.const import (
ATTR_MEDIA_VOLUME_MUTED, ATTR_MEDIA_VOLUME_MUTED,
SERVICE_SELECT_SOURCE, SERVICE_SELECT_SOURCE,
) )
from homeassistant.components.webostv import ( from homeassistant.components.webostv.const import (
ATTR_BUTTON, ATTR_BUTTON,
ATTR_COMMAND, ATTR_COMMAND,
DOMAIN, DOMAIN,
@ -40,7 +40,9 @@ def client_fixture():
with patch( with patch(
"homeassistant.components.webostv.WebOsClient", autospec=True "homeassistant.components.webostv.WebOsClient", autospec=True
) as mock_client_class: ) as mock_client_class:
yield mock_client_class.return_value client = mock_client_class.return_value
client.software_info = {"device_id": "a1:b1:c1:d1:e1:f1"}
yield client
async def setup_webostv(hass): async def setup_webostv(hass):