Skip to content

Commit

Permalink
Online state for samsungtv is jumping when TV is idle (#11998)
Browse files Browse the repository at this point in the history
* Set timeout to offline

* Have to rewrite to use ping instead.
  • Loading branch information
kennedyshead authored and balloob committed Jan 29, 2018
1 parent 89e0b26 commit 7ad870c
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions homeassistant/components/media_player/samsungtv.py
Expand Up @@ -8,6 +8,9 @@
import socket
from datetime import timedelta

import sys

import subprocess
import voluptuous as vol

from homeassistant.components.media_player import (
Expand Down Expand Up @@ -122,12 +125,19 @@ def __init__(self, host, port, name, timeout, mac):

def update(self):
"""Update state of device."""
if sys.platform == 'win32':
_ping_cmd = ['ping', '-n 1', '-w', '1000', self._config['host']]
else:
_ping_cmd = ['ping', '-n', '-q', '-c1', '-W1',
self._config['host']]

ping = subprocess.Popen(
_ping_cmd,
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(self._config[CONF_TIMEOUT])
sock.connect((self._config['host'], self._config['port']))
self._state = STATE_ON
except socket.error:
ping.communicate()
self._state = STATE_ON if ping.returncode == 0 else STATE_OFF
except subprocess.CalledProcessError:
self._state = STATE_OFF

def get_remote(self):
Expand Down

0 comments on commit 7ad870c

Please sign in to comment.