8000 Add webos customize option to add custom sources by roidayan · Pull Request #2561 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add webos customize option to add custom sources #2561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 46 additions & 21 deletions homeassistant/components/media_player/webostv.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
SUPPORT_SELECT_SOURCE, SUPPORT_PLAY_MEDIA, MEDIA_TYPE_CHANNEL,
MediaPlayerDevice)
from homeassistant.const import (
CONF_HOST, STATE_OFF, STATE_PLAYING, STATE_PAUSED, STATE_UNKNOWN)
CONF_HOST, CONF_CUSTOMIZE, STATE_OFF, STATE_PLAYING, STATE_PAUSED,
STATE_UNKNOWN)
from homeassistant.loader import get_component

_CONFIGURING = {}
Expand All @@ -33,6 +34,16 @@
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(seconds=1)

WEBOS_APP_LIVETV = 'com.webos.app.livetv'
WEBOS_APP_YOUTUBE = 'youtube.leanback.v4'
WEBOS_APP_MAKO = 'makotv'

WEBOS_APPS_SHORT = {
'livetv': WEBOS_APP_LIVETV,
'youtube': WEBOS_APP_YOUTUBE,
'makotv': WEBOS_APP_MAKO
}


# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
Expand All @@ -50,10 +61,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
if host in _CONFIGURING:
return

setup_tv(host, hass, add_devices)
customize = config.get(CONF_CUSTOMIZE, {})
setup_tv(host, customize, hass, add_devices)


def setup_tv(host, hass, add_devices):
def setup_tv(host, customize, hass, add_devices):
"""Setup a phue bridge based on host parameter."""
from pylgtv import WebOsClient
from pylgtv import PyLGTVPairException
Expand All @@ -75,7 +87,7 @@ def setup_tv(host, hass, add_devices):
else:
# Not registered, request configuration.
_LOGGER.warning('LG WebOS TV at %s needs to be paired.', host)
request_configuration(host, hass, add_devices)
request_configuration(host, customize, hass, add_devices)
return

# If we came here and configuring this host, mark as done.
Expand All @@ -84,10 +96,10 @@ def setup_tv(host, hass, add_devices):
configurator = get_component('configurator')
configurator.request_done(request_id)

add_devices([LgWebOSDevice(host)])
add_devices([LgWebOSDevice(host, customize)])


def request_configuration(host, hass, add_devices):
def request_configuration(host, customize, hass, add_devices):
"""Request configuration steps from the user."""
configurator = get_component('configurator')

Expand All @@ -100,7 +112,7 @@ def request_configuration(host, hass, add_devices):
# pylint: disable=unused-argument
def lgtv_configuration_callback(data):
"""The actions to do when our configuration callback is called."""
setup_tv(host, hass, add_devices)
setup_tv(host, customize, hass, add_devices)

_CONFIGURING[host] = configurator.request_config(
hass, 'LG WebOS TV', lgtv_configuration_callback,
Expand All @@ -116,10 +128,11 @@ class LgWebOSDevice(MediaPlayerDevice):
"""Representation of a LG WebOS TV."""

# pylint: disable=too-many-public-methods
def __init__(self, host):
def __init__(self, host, customize):
"""Initialize the webos device."""
from pylgtv import WebOsClient
self._client = WebOsClient(host)
self._customize = customize

self._name = 'LG WebOS TV Remote'
# Assume that the TV is not muted
Expand All @@ -130,7 +143,6 @@ def __init__(self, host):
self._current_source = None
self._current_source_id = None
self._source_list = None
self._source_label_list = None
self._state = STATE_UNKNOWN
self._app_list = None

Expand All @@ -144,19 +156,30 @@ def update(self):
self._muted = self._client.get_muted()
self._volume = self._client.get_volume()
self._current_source_id = self._client.get_input()

self._source_list = {}
self._source_label_list = []
self._app_list = {}

custom_sources = []
for source in self._customize.get('sources', []):
app_id = WEBOS_APPS_SHORT.get(source, None)
if app_id:
custom_sources.append(app_id)
else:
custom_sources.append(source)

for app in self._client.get_apps():
self._app_list[app['id']] = app
if app['id'] == self._current_source_id:
self._current_source = app['title']
self._source_list[app['title']] = app
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line necessary? Won't line 176 catch it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line 176 will catch it if it's in the custom sources but someone might not have the current source in the sources list. so instead of having a dropdown for source select with nothing selected I'm adding it to the list.

if app['id'] in custom_sources:
self._source_list[app['title']] = app

for source in self._client.get_inputs():
self._source_list[source['label']] = source
self._app_list[source['appId']] = source
self._source_label_list.append(source['label'])
if source['appId'] == self._current_source_id:
self._current_source = source['label']
if not source['connected']:
continue
app = self._app_list[source['appId']]
self._source_list[app['title']] = app

except OSError:
self._state = STATE_OFF
Expand Down Expand Up @@ -189,7 +212,7 @@ def source(self):
@property
def source_list(self):
"""List of available input sources."""
return self._source_label_list
return sorted(self._source_list.keys())

@property
def media_content_type(self):
Expand All @@ -199,7 +222,9 @@ def media_content_type(self):
@property
def media_image_url(self):
"""Image url of current playing media."""
return self._app_list[self._current_source_id]['icon']
if self._current_source_id in self._app_list:
return self._app_list[self._current_source_id]['largeIcon']
return None

@property
def supported_media_commands(self):
Expand Down Expand Up @@ -238,9 +263,9 @@ def media_play_pause(self):

def select_source(self, source):
"""Select input source."""
self._current_source_id = self._source_list[source]['appId']
self._current_source = self._source_list[source]['label']
self._client.set_input(self._source_list[source]['id'])
self._current_source_id = self._source_list[source]['id']
self._current_source = self._source_list[source]['title']
self._client.launch_app(self._source_list[source]['id'])

def media_play(self):
"""Send play command."""
Expand Down
0