Fix webostv select source ()

* Fix reuse of variable name

This should fix .

* Add tests for LgWebOSDevice.select_source
pull/11274/head
Daniel Watkins 2017-12-22 12:38:00 -05:00 committed by Martin Hjelmare
parent 46df91ff45
commit 353bb62687
2 changed files with 69 additions and 9 deletions
homeassistant/components/media_player
tests/components/media_player

View File

@ -322,17 +322,17 @@ class LgWebOSDevice(MediaPlayerDevice):
def select_source(self, source):
"""Select input source."""
source = self._source_list.get(source)
if source is None:
source_dict = self._source_list.get(source)
if source_dict is None:
_LOGGER.warning("Source %s not found for %s", source, self.name)
return
self._current_source_id = self._source_list[source]['id']
if source.get('title'):
self._current_source = self._source_list[source]['title']
self._client.launch_app(self._source_list[source]['id'])
elif source.get('label'):
self._current_source = self._source_list[source]['label']
self._client.set_input(self._source_list[source]['id'])
self._current_source_id = source_dict['id']
if source_dict.get('title'):
self._current_source = source_dict['title']
self._client.launch_app(source_dict['id'])
elif source_dict.get('label'):
self._current_source = source_dict['label']
self._client.set_input(source_dict['id'])
def media_play(self):
"""Send play command."""

View File

@ -0,0 +1,60 @@
"""The tests for the LG webOS media player platform."""
import unittest
from unittest import mock
from homeassistant.components.media_player import webostv
class FakeLgWebOSDevice(webostv.LgWebOSDevice):
"""A fake device without the client setup required for the real one."""
def __init__(self, *args, **kwargs):
"""Initialise parameters needed for tests with fake values."""
self._source_list = {}
self._client = mock.MagicMock()
self._name = 'fake_device'
self._current_source = None
class TestLgWebOSDevice(unittest.TestCase):
"""Test the LgWebOSDevice class."""
def setUp(self):
"""Configure a fake device for each test."""
self.device = FakeLgWebOSDevice()
def test_select_source_with_empty_source_list(self):
"""Ensure we don't call client methods when we don't have sources."""
self.device.select_source('nonexistent')
assert 0 == self.device._client.launch_app.call_count
assert 0 == self.device._client.set_input.call_count
def test_select_source_with_titled_entry(self):
"""Test that a titled source is treated as an app."""
self.device._source_list = {
'existent': {
'id': 'existent_id',
'title': 'existent_title',
},
}
self.device.select_source('existent')
assert 'existent_title' == self.device._current_source
assert [mock.call('existent_id')] == (
self.device._client.launch_app.call_args_list)
def test_select_source_with_labelled_entry(self):
"""Test that a labelled source is treated as an input source."""
self.device._source_list = {
'existent': {
'id': 'existent_id',
'label': 'existent_label',
},
}
self.device.select_source('existent')
assert 'existent_label' == self.device._current_source
assert [mock.call('existent_id')] == (
self.device._client.set_input.call_args_list)