diff --git a/homeassistant/components/media_player/webostv.py b/homeassistant/components/media_player/webostv.py index 68de934d28b..67f2afc8520 100644 --- a/homeassistant/components/media_player/webostv.py +++ b/homeassistant/components/media_player/webostv.py @@ -173,34 +173,55 @@ class LgWebOSDevice(MediaPlayerDevice): """Retrieve the latest data.""" from websockets.exceptions import ConnectionClosed try: - self._state = STATE_PLAYING - self._muted = self._client.get_muted() - self._volume = self._client.get_volume() - self._current_source_id = self._client.get_input() - self._source_list = {} - self._app_list = {} + current_input = self._client.get_input() + if current_input is not None: + self._current_source_id = current_input + if self._state in (STATE_UNKNOWN, STATE_OFF): + self._state = STATE_PLAYING + else: + self._state = STATE_OFF + self._current_source = None + self._current_source_id = None - custom_sources = self._customize.get(CONF_SOURCES, []) + if self._state is not STATE_OFF: + self._muted = self._client.get_muted() + self._volume = self._client.get_volume() - 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 - elif (app['id'] in custom_sources or - any(word in app['title'] for word in custom_sources) or - any(word in app['id'] for word in custom_sources)): - self._source_list[app['title']] = app + self._source_list = {} + self._app_list = {} + conf_sources = self._customize.get(CONF_SOURCES, []) - for source in self._client.get_inputs(): - if not source['connected']: - continue - app = self._app_list[source['appId']] - self._source_list[app['title']] = app + for app in self._client.get_apps(): + self._app_list[app['id']] = app + if conf_sources: + if app['id'] == self._current_source_id: + self._current_source = app['title'] + self._source_list[app['title']] = app + elif (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 + else: + self._current_source = app['title'] + self._source_list[app['title']] = app + for source in self._client.get_inputs(): + if conf_sources: + if source['id'] == self._current_source_id: + self._source_list[source['label']] = source + elif (source['label'] in conf_sources or + any(source['label'].find(word) != -1 + for word in conf_sources)): + self._source_list[source['label']] = source + else: + self._source_list[source['label']] = source except (OSError, ConnectionClosed, TypeError, asyncio.TimeoutError): self._state = STATE_OFF + self._current_source = None + self._current_source_id = None @property def name(self): @@ -296,9 +317,14 @@ class LgWebOSDevice(MediaPlayerDevice): def select_source(self, source): """Select input source.""" - 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']) + if self._source_list.get(source).get('title'): + 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']) + elif self._source_list.get(source).get('label'): + self._current_source_id = self._source_list[source]['id'] + self._current_source = self._source_list[source]['label'] + self._client.set_input(self._source_list[source]['id']) def media_play(self): """Send play command."""