From f6e3a060ffd41a7c53173c7d89760f2a017a27c9 Mon Sep 17 00:00:00 2001 From: Timmo Date: Mon, 13 Apr 2020 14:47:04 +0100 Subject: [PATCH 1/7] Move consts to const, general cleanup --- homeassistant/components/webostv/__init__.py | 29 +++++++++---------- homeassistant/components/webostv/const.py | 19 ++++++++++-- .../components/webostv/media_player.py | 14 +++++---- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/webostv/__init__.py b/homeassistant/components/webostv/__init__.py index 9dec8fe0c71eb..0790ece9333d4 100644 --- a/homeassistant/components/webostv/__init__.py +++ b/homeassistant/components/webostv/__init__.py @@ -1,4 +1,4 @@ -"""Support for WebOS TV.""" +"""Support for LG webOS Smart TV.""" import asyncio import logging @@ -6,6 +6,18 @@ import voluptuous as vol 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 ( ATTR_ENTITY_ID, CONF_CUSTOMIZE, @@ -19,21 +31,6 @@ 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( {vol.Optional(CONF_SOURCES, default=[]): vol.All(cv.ensure_list, [cv.string])} ) diff --git a/homeassistant/components/webostv/const.py b/homeassistant/components/webostv/const.py index a81696f6c0bae..3e1e790fc02ee 100644 --- a/homeassistant/components/webostv/const.py +++ b/homeassistant/components/webostv/const.py @@ -1,4 +1,19 @@ -"""Constants used for WebOS TV.""" -LIVE_TV_APP_ID = "com.webos.app.livetv" +"""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" + +WEBOSTV_CONFIG_FILE = "webostv.conf" diff --git a/homeassistant/components/webostv/media_player.py b/homeassistant/components/webostv/media_player.py index 87f55fd6b2d6f..af38a55d312f5 100644 --- a/homeassistant/components/webostv/media_player.py +++ b/homeassistant/components/webostv/media_player.py @@ -23,6 +23,13 @@ SUPPORT_VOLUME_SET, 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 ( ATTR_ENTITY_ID, CONF_CUSTOMIZE, @@ -36,9 +43,6 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect 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__) @@ -60,7 +64,7 @@ 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: return @@ -108,7 +112,7 @@ async def wrapper(obj, *args, **kwargs): 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): """Initialize the webos device.""" From 1a76224da9ae16392dc7613e28827d0243b63c1c Mon Sep 17 00:00:00 2001 From: Timmo Date: Mon, 13 Apr 2020 14:54:36 +0100 Subject: [PATCH 2/7] Add unique id --- homeassistant/components/webostv/media_player.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/webostv/media_player.py b/homeassistant/components/webostv/media_player.py index af38a55d312f5..d05ef4f83b20c 100644 --- a/homeassistant/components/webostv/media_player.py +++ b/homeassistant/components/webostv/media_player.py @@ -4,7 +4,7 @@ from functools import wraps import logging -from aiopylgtv import PyLGTVCmdException, PyLGTVPairException +from aiopylgtv import PyLGTVCmdException, PyLGTVPairException, WebOsClient from websockets.exceptions import ConnectionClosed from homeassistant import util @@ -114,10 +114,11 @@ async def wrapper(obj, *args, **kwargs): class LgWebOSMediaPlayerEntity(MediaPlayerDevice): """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.""" self._client = client self._name = name + self._unique_id = client.software_info["device_id"] self._customize = customize self._on_script = on_script @@ -223,6 +224,11 @@ async def async_update(self): ): pass + @property + def unique_id(self): + """Return the unique id of the device.""" + return self._name + @property def name(self): """Return the name of the device.""" From a62240f0235a9d423045e47439dd10633ece039a Mon Sep 17 00:00:00 2001 From: Timmo Date: Mon, 13 Apr 2020 14:58:10 +0100 Subject: [PATCH 3/7] Add default icon --- homeassistant/components/webostv/media_player.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/homeassistant/components/webostv/media_player.py b/homeassistant/components/webostv/media_player.py index d05ef4f83b20c..68eb9996cad36 100644 --- a/homeassistant/components/webostv/media_player.py +++ b/homeassistant/components/webostv/media_player.py @@ -234,6 +234,11 @@ def name(self): """Return the name of the device.""" return self._name + @property + def icon(self): + """Return the icon of the device.""" + return "hass:television" + @property def state(self): """Return the state of the device.""" From 6a1123606a050b52d5b2b62c74ddf9b31d95e39d Mon Sep 17 00:00:00 2001 From: Timmo Date: Mon, 13 Apr 2020 15:29:21 +0100 Subject: [PATCH 4/7] Set supported features based on sound output --- .../components/webostv/media_player.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/webostv/media_player.py b/homeassistant/components/webostv/media_player.py index 68eb9996cad36..8863150cf8a59 100644 --- a/homeassistant/components/webostv/media_player.py +++ b/homeassistant/components/webostv/media_player.py @@ -45,20 +45,18 @@ _LOGGER = logging.getLogger(__name__) - SUPPORT_WEBOSTV = ( SUPPORT_TURN_OFF | SUPPORT_NEXT_TRACK | SUPPORT_PAUSE | SUPPORT_PREVIOUS_TRACK - | SUPPORT_VOLUME_MUTE - | SUPPORT_VOLUME_SET - | SUPPORT_VOLUME_STEP | SUPPORT_SELECT_SOURCE | SUPPORT_PLAY_MEDIA | SUPPORT_PLAY ) +SUPPORT_WEBOSTV_VOLUME = SUPPORT_VOLUME_MUTE | SUPPORT_VOLUME_STEP + MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10) MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(seconds=1) @@ -300,9 +298,17 @@ def media_image_url(self): @property def supported_features(self): """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: - return SUPPORT_WEBOSTV | SUPPORT_TURN_ON - return SUPPORT_WEBOSTV + supported = supported | SUPPORT_TURN_ON + + return supported @property def device_state_attributes(self): From 24aa13abdd0e31e8bed34303ef51d76672f31954 Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Mon, 13 Apr 2020 16:16:21 +0100 Subject: [PATCH 5/7] Update homeassistant/components/webostv/media_player.py Co-Authored-By: Martin Hjelmare --- homeassistant/components/webostv/media_player.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/webostv/media_player.py b/homeassistant/components/webostv/media_player.py index 8863150cf8a59..12523a0d6ce81 100644 --- a/homeassistant/components/webostv/media_player.py +++ b/homeassistant/components/webostv/media_player.py @@ -225,7 +225,7 @@ async def async_update(self): @property def unique_id(self): """Return the unique id of the device.""" - return self._name + return self._unique_id @property def name(self): From 45388fdd16e05262c07ae131da85720544a35bb9 Mon Sep 17 00:00:00 2001 From: Timmo Date: Mon, 13 Apr 2020 16:25:27 +0100 Subject: [PATCH 6/7] Set device class --- homeassistant/components/webostv/media_player.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/webostv/media_player.py b/homeassistant/components/webostv/media_player.py index 12523a0d6ce81..a19f42d7d5602 100644 --- a/homeassistant/components/webostv/media_player.py +++ b/homeassistant/components/webostv/media_player.py @@ -8,7 +8,7 @@ from websockets.exceptions import ConnectionClosed 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 ( MEDIA_TYPE_CHANNEL, SUPPORT_NEXT_TRACK, @@ -233,9 +233,9 @@ def name(self): return self._name @property - def icon(self): - """Return the icon of the device.""" - return "hass:television" + def device_class(self): + """Return the device class of the device.""" + return DEVICE_CLASS_TV @property def state(self): From ba5b420782356d5e59dc21dcb1f372916b045d19 Mon Sep 17 00:00:00 2001 From: Timmo Date: Tue, 14 Apr 2020 17:19:53 +0100 Subject: [PATCH 7/7] Add software_info to client mock --- tests/components/webostv/test_media_player.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/components/webostv/test_media_player.py b/tests/components/webostv/test_media_player.py index e415734bec2f9..2685064a94660 100644 --- a/tests/components/webostv/test_media_player.py +++ b/tests/components/webostv/test_media_player.py @@ -9,7 +9,7 @@ ATTR_MEDIA_VOLUME_MUTED, SERVICE_SELECT_SOURCE, ) -from homeassistant.components.webostv import ( +from homeassistant.components.webostv.const import ( ATTR_BUTTON, ATTR_COMMAND, DOMAIN, @@ -40,7 +40,9 @@ def client_fixture(): with patch( "homeassistant.components.webostv.WebOsClient", autospec=True ) 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):