Add timeout and fix oscillations on Samsung TV component (#17102)

* Add timeout and fix oscillations

* Adjust code to py3.5.3

* Clean code
This commit is contained in:
Ana Paula Gomes 2018-10-04 16:02:14 +02:00 committed by Pascal Vizeli
parent b92b24e8a4
commit 02bf07d9df
2 changed files with 24 additions and 3 deletions

View File

@ -125,10 +125,14 @@ class SamsungTVDevice(MediaPlayerDevice):
def update(self):
"""Update state of device."""
if sys.platform == 'win32':
_ping_cmd = ['ping', '-n 1', '-w', '1000', self._config['host']]
timeout_arg = '-w {}000'.format(self._config['timeout'])
_ping_cmd = [
'ping', '-n 3', timeout_arg, self._config['host']]
else:
_ping_cmd = ['ping', '-n', '-q', '-c1', '-W1',
self._config['host']]
timeout_arg = '-W{}'.format(self._config['timeout'])
_ping_cmd = [
'ping', '-n', '-q',
'-c3', timeout_arg, self._config['host']]
ping = subprocess.Popen(
_ping_cmd,

View File

@ -128,6 +128,23 @@ class TestSamsungTv(unittest.TestCase):
self.device.update()
self.assertEqual(STATE_OFF, self.device._state)
@mock.patch(
'homeassistant.components.media_player.samsungtv.subprocess.Popen'
)
def test_timeout(self, mocked_popen):
"""Test timeout use."""
ping = mock.Mock()
mocked_popen.return_value = ping
ping.returncode = 0
self.device.update()
expected_timeout = self.device._config['timeout']
timeout_arg = '-W{}'.format(expected_timeout)
ping_command = [
'ping', '-n', '-q', '-c3', timeout_arg, 'fake']
expected_call = call(ping_command, stderr=-3, stdout=-1)
self.assertEqual(mocked_popen.call_args, expected_call)
self.assertEqual(STATE_ON, self.device._state)
def test_send_key(self):
"""Test for send key."""
self.device.send_key('KEY_POWER')