2019-02-13 20:21:14 +00:00
|
|
|
"""Support for interface with an LG webOS Smart TV."""
|
2017-04-22 03:24:21 +00:00
|
|
|
import asyncio
|
2021-03-23 13:36:43 +00:00
|
|
|
from contextlib import suppress
|
2016-04-19 15:53:58 +00:00
|
|
|
from datetime import timedelta
|
2019-12-31 23:26:35 +00:00
|
|
|
from functools import wraps
|
2018-01-21 06:35:38 +00:00
|
|
|
import logging
|
2018-01-29 08:24:08 +00:00
|
|
|
|
2020-04-14 18:26:13 +00:00
|
|
|
from aiopylgtv import PyLGTVCmdException, PyLGTVPairException, WebOsClient
|
2019-11-26 17:49:14 +00:00
|
|
|
from websockets.exceptions import ConnectionClosed
|
2016-10-06 14:11:08 +00:00
|
|
|
|
2018-09-09 12:26:06 +00:00
|
|
|
from homeassistant import util
|
2021-12-16 08:20:52 +00:00
|
|
|
from homeassistant.components.media_player import (
|
|
|
|
MediaPlayerDeviceClass,
|
|
|
|
MediaPlayerEntity,
|
|
|
|
)
|
2019-02-08 22:18:18 +00:00
|
|
|
from homeassistant.components.media_player.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
MEDIA_TYPE_CHANNEL,
|
|
|
|
SUPPORT_NEXT_TRACK,
|
|
|
|
SUPPORT_PAUSE,
|
|
|
|
SUPPORT_PLAY,
|
|
|
|
SUPPORT_PLAY_MEDIA,
|
|
|
|
SUPPORT_PREVIOUS_TRACK,
|
|
|
|
SUPPORT_SELECT_SOURCE,
|
|
|
|
SUPPORT_TURN_OFF,
|
|
|
|
SUPPORT_TURN_ON,
|
|
|
|
SUPPORT_VOLUME_MUTE,
|
|
|
|
SUPPORT_VOLUME_SET,
|
|
|
|
SUPPORT_VOLUME_STEP,
|
|
|
|
)
|
2016-04-19 15:53:58 +00:00
|
|
|
from homeassistant.const import (
|
2020-01-02 21:32:57 +00:00
|
|
|
ATTR_ENTITY_ID,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_CUSTOMIZE,
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_NAME,
|
2020-01-02 21:32:57 +00:00
|
|
|
ENTITY_MATCH_ALL,
|
2020-02-04 22:42:07 +00:00
|
|
|
ENTITY_MATCH_NONE,
|
2019-07-31 19:25:30 +00:00
|
|
|
STATE_OFF,
|
2020-01-03 01:46:32 +00:00
|
|
|
STATE_ON,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-01-02 21:32:57 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2017-09-26 07:03:40 +00:00
|
|
|
from homeassistant.helpers.script import Script
|
2016-04-19 15:53:58 +00:00
|
|
|
|
2021-12-13 08:39:13 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_PAYLOAD,
|
|
|
|
ATTR_SOUND_OUTPUT,
|
|
|
|
CONF_ON_ACTION,
|
|
|
|
CONF_SOURCES,
|
|
|
|
DOMAIN,
|
|
|
|
LIVE_TV_APP_ID,
|
|
|
|
)
|
|
|
|
|
2016-10-06 14:11:08 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_WEBOSTV = (
|
|
|
|
SUPPORT_TURN_OFF
|
|
|
|
| SUPPORT_NEXT_TRACK
|
|
|
|
| SUPPORT_PAUSE
|
|
|
|
| SUPPORT_PREVIOUS_TRACK
|
|
|
|
| SUPPORT_SELECT_SOURCE
|
|
|
|
| SUPPORT_PLAY_MEDIA
|
|
|
|
| SUPPORT_PLAY
|
|
|
|
)
|
2016-04-19 15:53:58 +00:00
|
|
|
|
2020-04-14 18:26:13 +00:00
|
|
|
SUPPORT_WEBOSTV_VOLUME = SUPPORT_VOLUME_MUTE | SUPPORT_VOLUME_STEP
|
|
|
|
|
2016-04-19 15:53:58 +00:00
|
|
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
|
|
|
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(seconds=1)
|
2020-06-22 11:08:02 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=10)
|
2016-04-19 15:53:58 +00:00
|
|
|
|
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2020-04-14 18:26:13 +00:00
|
|
|
"""Set up the LG webOS Smart TV platform."""
|
2016-04-19 15:53:58 +00:00
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
if discovery_info is None:
|
2016-04-19 15:53:58 +00:00
|
|
|
return
|
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
host = discovery_info[CONF_HOST]
|
|
|
|
name = discovery_info[CONF_NAME]
|
|
|
|
customize = discovery_info[CONF_CUSTOMIZE]
|
|
|
|
turn_on_action = discovery_info.get(CONF_ON_ACTION)
|
2017-09-26 07:03:40 +00:00
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
client = hass.data[DOMAIN][host]["client"]
|
2020-08-12 16:39:05 +00:00
|
|
|
on_script = Script(hass, turn_on_action, name, DOMAIN) if turn_on_action else None
|
2017-09-26 07:03:40 +00:00
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
entity = LgWebOSMediaPlayerEntity(client, name, customize, on_script)
|
2016-04-19 15:53:58 +00:00
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
async_add_entities([entity], update_before_add=False)
|
2016-04-19 15:53:58 +00:00
|
|
|
|
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
def cmd(func):
|
|
|
|
"""Catch command exceptions."""
|
2016-04-19 15:53:58 +00:00
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
@wraps(func)
|
|
|
|
async def wrapper(obj, *args, **kwargs):
|
|
|
|
"""Wrap all command methods."""
|
|
|
|
try:
|
|
|
|
await func(obj, *args, **kwargs)
|
|
|
|
except (
|
|
|
|
asyncio.TimeoutError,
|
|
|
|
asyncio.CancelledError,
|
|
|
|
PyLGTVCmdException,
|
|
|
|
) as exc:
|
|
|
|
# If TV is off, we expect calls to fail.
|
|
|
|
if obj.state == STATE_OFF:
|
|
|
|
level = logging.INFO
|
|
|
|
else:
|
|
|
|
level = logging.ERROR
|
|
|
|
_LOGGER.log(
|
|
|
|
level,
|
|
|
|
"Error calling %s on entity %s: %r",
|
|
|
|
func.__name__,
|
|
|
|
obj.entity_id,
|
|
|
|
exc,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-04-19 15:53:58 +00:00
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
return wrapper
|
2016-04-19 15:53:58 +00:00
|
|
|
|
|
|
|
|
2020-04-25 16:00:57 +00:00
|
|
|
class LgWebOSMediaPlayerEntity(MediaPlayerEntity):
|
2020-04-14 18:26:13 +00:00
|
|
|
"""Representation of a LG webOS Smart TV."""
|
2016-04-19 15:53:58 +00:00
|
|
|
|
2020-04-14 18:26:13 +00:00
|
|
|
def __init__(self, client: WebOsClient, name: str, customize, on_script=None):
|
2016-04-19 15:53:58 +00:00
|
|
|
"""Initialize the webos device."""
|
2019-12-31 23:26:35 +00:00
|
|
|
self._client = client
|
|
|
|
self._name = name
|
2020-05-11 09:09:16 +00:00
|
|
|
self._unique_id = client.client_key
|
2016-08-18 06:39:37 +00:00
|
|
|
self._customize = customize
|
2019-12-31 23:26:35 +00:00
|
|
|
self._on_script = on_script
|
2016-04-19 15:53:58 +00:00
|
|
|
|
2020-01-20 09:43:20 +00:00
|
|
|
# Assume that the TV is not paused
|
|
|
|
self._paused = False
|
|
|
|
|
2016-04-19 15:53:58 +00:00
|
|
|
self._current_source = None
|
2021-08-04 16:31:24 +00:00
|
|
|
self._source_list: dict = {}
|
2016-04-19 15:53:58 +00:00
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
async def async_added_to_hass(self):
|
2020-01-02 21:32:57 +00:00
|
|
|
"""Connect and subscribe to dispatcher signals and state updates."""
|
|
|
|
async_dispatcher_connect(self.hass, DOMAIN, self.async_signal_handler)
|
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
await self._client.register_state_update_callback(
|
|
|
|
self.async_handle_state_update
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
async def async_will_remove_from_hass(self):
|
|
|
|
"""Call disconnect on removal."""
|
|
|
|
self._client.unregister_state_update_callback(self.async_handle_state_update)
|
|
|
|
|
2020-01-02 21:32:57 +00:00
|
|
|
async def async_signal_handler(self, data):
|
|
|
|
"""Handle domain-specific signal by calling appropriate method."""
|
2021-10-18 16:36:35 +00:00
|
|
|
if (entity_ids := data[ATTR_ENTITY_ID]) == ENTITY_MATCH_NONE:
|
2020-02-04 22:42:07 +00:00
|
|
|
return
|
|
|
|
|
2020-01-02 21:32:57 +00:00
|
|
|
if entity_ids == ENTITY_MATCH_ALL or self.entity_id in entity_ids:
|
|
|
|
params = {
|
|
|
|
key: value
|
|
|
|
for key, value in data.items()
|
|
|
|
if key not in ["entity_id", "method"]
|
|
|
|
}
|
|
|
|
await getattr(self, data["method"])(**params)
|
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
async def async_handle_state_update(self):
|
|
|
|
"""Update state from WebOsClient."""
|
|
|
|
self.update_sources()
|
|
|
|
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|
2019-12-31 23:26:35 +00:00
|
|
|
|
|
|
|
def update_sources(self):
|
|
|
|
"""Update list of sources from current source, apps, inputs and configured list."""
|
2020-08-05 09:01:12 +00:00
|
|
|
source_list = self._source_list
|
2019-12-31 23:26:35 +00:00
|
|
|
self._source_list = {}
|
|
|
|
conf_sources = self._customize[CONF_SOURCES]
|
|
|
|
|
2020-01-22 18:06:08 +00:00
|
|
|
found_live_tv = False
|
|
|
|
for app in self._client.apps.values():
|
|
|
|
if app["id"] == LIVE_TV_APP_ID:
|
|
|
|
found_live_tv = True
|
|
|
|
if app["id"] == self._client.current_appId:
|
2019-12-31 23:26:35 +00:00
|
|
|
self._current_source = app["title"]
|
|
|
|
self._source_list[app["title"]] = app
|
|
|
|
elif (
|
|
|
|
not conf_sources
|
|
|
|
or app["id"] in conf_sources
|
|
|
|
or any(word in app["title"] for word in conf_sources)
|
|
|
|
or any(word in app["id"] for word in conf_sources)
|
|
|
|
):
|
|
|
|
self._source_list[app["title"]] = app
|
|
|
|
|
2020-01-22 18:06:08 +00:00
|
|
|
for source in self._client.inputs.values():
|
|
|
|
if source["appId"] == LIVE_TV_APP_ID:
|
|
|
|
found_live_tv = True
|
|
|
|
if source["appId"] == self._client.current_appId:
|
2019-12-31 23:26:35 +00:00
|
|
|
self._current_source = source["label"]
|
|
|
|
self._source_list[source["label"]] = source
|
|
|
|
elif (
|
|
|
|
not conf_sources
|
|
|
|
or source["label"] in conf_sources
|
|
|
|
or any(source["label"].find(word) != -1 for word in conf_sources)
|
|
|
|
):
|
|
|
|
self._source_list[source["label"]] = source
|
|
|
|
|
2020-01-22 18:06:08 +00:00
|
|
|
# special handling of live tv since this might not appear in the app or input lists in some cases
|
|
|
|
if not found_live_tv:
|
|
|
|
app = {"id": LIVE_TV_APP_ID, "title": "Live TV"}
|
|
|
|
if LIVE_TV_APP_ID == self._client.current_appId:
|
|
|
|
self._current_source = app["title"]
|
|
|
|
self._source_list["Live TV"] = app
|
|
|
|
elif (
|
|
|
|
not conf_sources
|
|
|
|
or app["id"] in conf_sources
|
|
|
|
or any(word in app["title"] for word in conf_sources)
|
|
|
|
or any(word in app["id"] for word in conf_sources)
|
|
|
|
):
|
|
|
|
self._source_list["Live TV"] = app
|
2020-08-05 09:01:12 +00:00
|
|
|
if not self._source_list and source_list:
|
|
|
|
self._source_list = source_list
|
2020-01-22 18:06:08 +00:00
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
@util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
|
|
|
|
async def async_update(self):
|
|
|
|
"""Connect."""
|
|
|
|
if not self._client.is_connected():
|
2021-03-23 13:36:43 +00:00
|
|
|
with suppress(
|
2019-12-31 23:26:35 +00:00
|
|
|
OSError,
|
|
|
|
ConnectionClosed,
|
|
|
|
ConnectionRefusedError,
|
|
|
|
asyncio.TimeoutError,
|
|
|
|
asyncio.CancelledError,
|
|
|
|
PyLGTVPairException,
|
|
|
|
PyLGTVCmdException,
|
|
|
|
):
|
2021-03-23 13:36:43 +00:00
|
|
|
await self._client.connect()
|
2016-04-19 15:53:58 +00:00
|
|
|
|
2020-04-14 18:26:13 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the unique id of the device."""
|
|
|
|
return self._unique_id
|
|
|
|
|
2016-04-19 15:53:58 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device."""
|
|
|
|
return self._name
|
|
|
|
|
2020-04-14 18:26:13 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class of the device."""
|
2021-12-16 08:20:52 +00:00
|
|
|
return MediaPlayerDeviceClass.TV
|
2020-04-14 18:26:13 +00:00
|
|
|
|
2016-04-19 15:53:58 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the device."""
|
2020-01-25 18:24:21 +00:00
|
|
|
if self._client.is_on:
|
|
|
|
return STATE_ON
|
2020-01-22 18:06:08 +00:00
|
|
|
|
2020-01-25 18:24:21 +00:00
|
|
|
return STATE_OFF
|
2016-04-19 15:53:58 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_volume_muted(self):
|
|
|
|
"""Boolean if volume is currently muted."""
|
2020-01-22 18:06:08 +00:00
|
|
|
return self._client.muted
|
2016-04-19 15:53:58 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def volume_level(self):
|
|
|
|
"""Volume level of the media player (0..1)."""
|
2020-01-22 18:06:08 +00:00
|
|
|
if self._client.volume is not None:
|
|
|
|
return self._client.volume / 100.0
|
|
|
|
|
|
|
|
return None
|
2016-04-19 15:53:58 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def source(self):
|
|
|
|
"""Return the current input source."""
|
|
|
|
return self._current_source
|
|
|
|
|
|
|
|
@property
|
|
|
|
def source_list(self):
|
|
|
|
"""List of available input sources."""
|
2021-03-19 12:41:09 +00:00
|
|
|
return sorted(self._source_list)
|
2016-04-19 15:53:58 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def media_content_type(self):
|
|
|
|
"""Content type of current playing media."""
|
2020-01-22 18:06:08 +00:00
|
|
|
if self._client.current_appId == LIVE_TV_APP_ID:
|
|
|
|
return MEDIA_TYPE_CHANNEL
|
|
|
|
|
|
|
|
return None
|
2016-04-19 15:53:58 +00:00
|
|
|
|
2018-02-02 22:54:54 +00:00
|
|
|
@property
|
|
|
|
def media_title(self):
|
|
|
|
"""Title of current playing media."""
|
2020-01-22 18:06:08 +00:00
|
|
|
if (self._client.current_appId == LIVE_TV_APP_ID) and (
|
|
|
|
self._client.current_channel is not None
|
|
|
|
):
|
|
|
|
return self._client.current_channel.get("channelName")
|
2018-02-11 17:20:28 +00:00
|
|
|
return None
|
2018-02-02 22:54:54 +00:00
|
|
|
|
2016-04-19 15:53:58 +00:00
|
|
|
@property
|
|
|
|
def media_image_url(self):
|
|
|
|
"""Image url of current playing media."""
|
2020-01-22 18:06:08 +00:00
|
|
|
if self._client.current_appId in self._client.apps:
|
|
|
|
icon = self._client.apps[self._client.current_appId]["largeIcon"]
|
2019-07-31 19:25:30 +00:00
|
|
|
if not icon.startswith("http"):
|
2020-01-22 18:06:08 +00:00
|
|
|
icon = self._client.apps[self._client.current_appId]["icon"]
|
2017-04-20 07:08:53 +00:00
|
|
|
return icon
|
2016-08-18 06:39:37 +00:00
|
|
|
return None
|
2016-04-19 15:53:58 +00:00
|
|
|
|
|
|
|
@property
|
2017-02-08 04:42:45 +00:00
|
|
|
def supported_features(self):
|
|
|
|
"""Flag media player features that are supported."""
|
2020-04-14 18:26:13 +00:00
|
|
|
supported = SUPPORT_WEBOSTV
|
|
|
|
|
2021-09-18 11:52:59 +00:00
|
|
|
if self._client.sound_output in ("external_arc", "external_speaker"):
|
2020-04-14 18:26:13 +00:00
|
|
|
supported = supported | SUPPORT_WEBOSTV_VOLUME
|
|
|
|
elif self._client.sound_output != "lineout":
|
|
|
|
supported = supported | SUPPORT_WEBOSTV_VOLUME | SUPPORT_VOLUME_SET
|
|
|
|
|
2017-09-26 07:03:40 +00:00
|
|
|
if self._on_script:
|
2020-04-14 18:26:13 +00:00
|
|
|
supported = supported | SUPPORT_TURN_ON
|
|
|
|
|
|
|
|
return supported
|
2016-04-19 15:53:58 +00:00
|
|
|
|
2020-01-27 09:40:48 +00:00
|
|
|
@property
|
2021-03-11 19:16:26 +00:00
|
|
|
def extra_state_attributes(self):
|
2020-01-27 09:40:48 +00:00
|
|
|
"""Return device specific state attributes."""
|
2020-10-06 16:08:53 +00:00
|
|
|
if self._client.sound_output is None and self.state == STATE_OFF:
|
|
|
|
return {}
|
|
|
|
return {ATTR_SOUND_OUTPUT: self._client.sound_output}
|
2020-01-27 09:40:48 +00:00
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
@cmd
|
|
|
|
async def async_turn_off(self):
|
2016-04-19 15:53:58 +00:00
|
|
|
"""Turn off media player."""
|
2020-01-22 18:06:08 +00:00
|
|
|
await self._client.power_off()
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
async def async_turn_on(self):
|
2016-12-17 07:24:35 +00:00
|
|
|
"""Turn on the media player."""
|
2017-09-26 07:03:40 +00:00
|
|
|
if self._on_script:
|
2020-08-21 12:17:47 +00:00
|
|
|
await self._on_script.async_run(context=self._context)
|
2019-12-31 23:26:35 +00:00
|
|
|
|
|
|
|
@cmd
|
|
|
|
async def async_volume_up(self):
|
2016-04-19 15:53:58 +00:00
|
|
|
"""Volume up the media player."""
|
2019-12-31 23:26:35 +00:00
|
|
|
await self._client.volume_up()
|
2016-04-19 15:53:58 +00:00
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
@cmd
|
|
|
|
async def async_volume_down(self):
|
2016-04-19 15:53:58 +00:00
|
|
|
"""Volume down media player."""
|
2019-12-31 23:26:35 +00:00
|
|
|
await self._client.volume_down()
|
2016-04-19 15:53:58 +00:00
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
@cmd
|
|
|
|
async def async_set_volume_level(self, volume):
|
2016-04-19 15:53:58 +00:00
|
|
|
"""Set volume level, range 0..1."""
|
2020-02-02 22:50:30 +00:00
|
|
|
tv_volume = int(round(volume * 100))
|
2019-12-31 23:26:35 +00:00
|
|
|
await self._client.set_volume(tv_volume)
|
2016-04-19 15:53:58 +00:00
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
@cmd
|
|
|
|
async def async_mute_volume(self, mute):
|
2016-04-19 15:53:58 +00:00
|
|
|
"""Send mute command."""
|
2019-12-31 23:26:35 +00:00
|
|
|
await self._client.set_mute(mute)
|
2016-04-19 15:53:58 +00:00
|
|
|
|
2020-01-27 09:40:48 +00:00
|
|
|
@cmd
|
|
|
|
async def async_select_sound_output(self, sound_output):
|
|
|
|
"""Select the sound output."""
|
|
|
|
await self._client.change_sound_output(sound_output)
|
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
@cmd
|
|
|
|
async def async_media_play_pause(self):
|
2020-01-20 09:43:20 +00:00
|
|
|
"""Simulate play pause media player."""
|
|
|
|
if self._paused:
|
|
|
|
await self.async_media_play()
|
|
|
|
else:
|
|
|
|
await self.async_media_pause()
|
2016-04-19 15:53:58 +00:00
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
@cmd
|
|
|
|
async def async_select_source(self, source):
|
2016-04-20 21:09:50 +00:00
|
|
|
"""Select input source."""
|
2021-10-18 16:36:35 +00:00
|
|
|
if (source_dict := self._source_list.get(source)) is None:
|
2017-12-06 13:48:17 +00:00
|
|
|
_LOGGER.warning("Source %s not found for %s", source, self.name)
|
|
|
|
return
|
2019-07-31 19:25:30 +00:00
|
|
|
if source_dict.get("title"):
|
2019-12-31 23:26:35 +00:00
|
|
|
await self._client.launch_app(source_dict["id"])
|
2019-07-31 19:25:30 +00:00
|
|
|
elif source_dict.get("label"):
|
2019-12-31 23:26:35 +00:00
|
|
|
await self._client.set_input(source_dict["id"])
|
2016-04-20 21:09:50 +00:00
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
@cmd
|
|
|
|
async def async_play_media(self, media_type, media_id, **kwargs):
|
2018-04-17 09:50:26 +00:00
|
|
|
"""Play a piece of media."""
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.debug("Call play media type <%s>, Id <%s>", media_type, media_id)
|
2018-04-17 09:50:26 +00:00
|
|
|
|
|
|
|
if media_type == MEDIA_TYPE_CHANNEL:
|
2021-03-19 14:26:36 +00:00
|
|
|
_LOGGER.debug("Searching channel")
|
2018-04-17 09:50:26 +00:00
|
|
|
partial_match_channel_id = None
|
2018-04-25 02:45:57 +00:00
|
|
|
perfect_match_channel_id = None
|
2018-04-17 09:50:26 +00:00
|
|
|
|
2020-01-22 18:06:08 +00:00
|
|
|
for channel in self._client.channels:
|
2019-07-31 19:25:30 +00:00
|
|
|
if media_id == channel["channelNumber"]:
|
|
|
|
perfect_match_channel_id = channel["channelId"]
|
2018-04-25 02:45:57 +00:00
|
|
|
continue
|
2019-09-24 20:53:03 +00:00
|
|
|
|
|
|
|
if media_id.lower() == channel["channelName"].lower():
|
2019-07-31 19:25:30 +00:00
|
|
|
perfect_match_channel_id = channel["channelId"]
|
2018-04-25 02:45:57 +00:00
|
|
|
continue
|
2019-09-24 20:53:03 +00:00
|
|
|
|
|
|
|
if media_id.lower() in channel["channelName"].lower():
|
2019-07-31 19:25:30 +00:00
|
|
|
partial_match_channel_id = channel["channelId"]
|
2018-04-17 09:50:26 +00:00
|
|
|
|
2018-04-25 02:45:57 +00:00
|
|
|
if perfect_match_channel_id is not None:
|
|
|
|
_LOGGER.info(
|
|
|
|
"Switching to channel <%s> with perfect match",
|
2019-07-31 19:25:30 +00:00
|
|
|
perfect_match_channel_id,
|
|
|
|
)
|
2019-12-31 23:26:35 +00:00
|
|
|
await self._client.set_channel(perfect_match_channel_id)
|
2018-04-25 02:45:57 +00:00
|
|
|
elif partial_match_channel_id is not None:
|
|
|
|
_LOGGER.info(
|
|
|
|
"Switching to channel <%s> with partial match",
|
2019-07-31 19:25:30 +00:00
|
|
|
partial_match_channel_id,
|
|
|
|
)
|
2019-12-31 23:26:35 +00:00
|
|
|
await self._client.set_channel(partial_match_channel_id)
|
2018-04-25 02:45:57 +00:00
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
@cmd
|
|
|
|
async def async_media_play(self):
|
2016-04-19 15:53:58 +00:00
|
|
|
"""Send play command."""
|
2020-01-20 09:43:20 +00:00
|
|
|
self._paused = False
|
2019-12-31 23:26:35 +00:00
|
|
|
await self._client.play()
|
2016-04-19 15:53:58 +00:00
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
@cmd
|
|
|
|
async def async_media_pause(self):
|
2016-04-19 15:53:58 +00:00
|
|
|
"""Send media pause command to media player."""
|
2020-01-20 09:43:20 +00:00
|
|
|
self._paused = True
|
2019-12-31 23:26:35 +00:00
|
|
|
await self._client.pause()
|
|
|
|
|
|
|
|
@cmd
|
|
|
|
async def async_media_stop(self):
|
|
|
|
"""Send stop command to media player."""
|
|
|
|
await self._client.stop()
|
2016-04-19 15:53:58 +00:00
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
@cmd
|
|
|
|
async def async_media_next_track(self):
|
2016-04-19 15:53:58 +00:00
|
|
|
"""Send next track command."""
|
2018-04-12 13:44:56 +00:00
|
|
|
current_input = self._client.get_input()
|
2020-01-27 09:40:48 +00:00
|
|
|
if current_input == LIVE_TV_APP_ID:
|
2019-12-31 23:26:35 +00:00
|
|
|
await self._client.channel_up()
|
2018-04-12 13:44:56 +00:00
|
|
|
else:
|
2019-12-31 23:26:35 +00:00
|
|
|
await self._client.fast_forward()
|
2016-04-19 15:53:58 +00:00
|
|
|
|
2019-12-31 23:26:35 +00:00
|
|
|
@cmd
|
|
|
|
async def async_media_previous_track(self):
|
2016-04-19 15:53:58 +00:00
|
|
|
"""Send the previous track command."""
|
2018-04-12 13:44:56 +00:00
|
|
|
current_input = self._client.get_input()
|
2020-01-27 09:40:48 +00:00
|
|
|
if current_input == LIVE_TV_APP_ID:
|
2019-12-31 23:26:35 +00:00
|
|
|
await self._client.channel_down()
|
2018-04-12 13:44:56 +00:00
|
|
|
else:
|
2019-12-31 23:26:35 +00:00
|
|
|
await self._client.rewind()
|
2020-01-02 21:32:57 +00:00
|
|
|
|
|
|
|
@cmd
|
|
|
|
async def async_button(self, button):
|
|
|
|
"""Send a button press."""
|
|
|
|
await self._client.button(button)
|
|
|
|
|
|
|
|
@cmd
|
2020-05-27 13:51:39 +00:00
|
|
|
async def async_command(self, command, **kwargs):
|
2020-01-02 21:32:57 +00:00
|
|
|
"""Send a command."""
|
2020-05-27 13:51:39 +00:00
|
|
|
await self._client.request(command, payload=kwargs.get(ATTR_PAYLOAD))
|