From 7906b0347d16fb8c60e7ba2cec26f4494472a7e2 Mon Sep 17 00:00:00 2001 From: Romain Rinie Date: Mon, 15 Jan 2018 10:43:49 +0100 Subject: [PATCH 1/2] Report if TV is on This new method "is_tv_on()" report the status of the TV. This aim at avoiding sending a dummy key for TV using websocket, doing so can wake TV up if send during an update, or during an update status check. https://github.com/home-assistant/home-assistant/pull/11179 --- samsungctl/remote.py | 3 +++ samsungctl/remote_legacy.py | 12 ++++++++++++ samsungctl/remote_websocket.py | 19 ++++++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/samsungctl/remote.py b/samsungctl/remote.py index daad60a..7cbf00d 100644 --- a/samsungctl/remote.py +++ b/samsungctl/remote.py @@ -22,3 +22,6 @@ def close(self): def control(self, key): return self.remote.control(key) + + def is_tv_on(self): + return self.remote.is_tv_on() diff --git a/samsungctl/remote_legacy.py b/samsungctl/remote_legacy.py index 8bd44f3..6b297d4 100644 --- a/samsungctl/remote_legacy.py +++ b/samsungctl/remote_legacy.py @@ -58,6 +58,18 @@ def control(self, key): _key_interval = 0.2 + def is_tv_on(self): + try: + # Send an empty key to see if we are still connected + self.control('KEY') + except (exceptions.UnhandledResponse, + exceptions.AccessDenied, BrokenPipeError): + # We got a response so it's on. + # BrokenPipe can occur when the commands is sent to fast + return True + except (exceptions.ConnectionClosed, OSError): + return False + def _read_response(self, first_time=False): header = self.connection.recv(3) tv_name_len = int.from_bytes(header[1:3], diff --git a/samsungctl/remote_websocket.py b/samsungctl/remote_websocket.py index d218f8e..6297f2c 100644 --- a/samsungctl/remote_websocket.py +++ b/samsungctl/remote_websocket.py @@ -3,11 +3,13 @@ import logging import socket import time +import requests from . import exceptions class RemoteWebsocket(): """Object for remote control connection.""" + _config = None def __init__(self, config): import websocket @@ -17,7 +19,7 @@ def __init__(self, config): if config["timeout"] == 0: config["timeout"] = None - + self._config = config URL_FORMAT = "ws://{}:{}/api/v2/channels/samsung.remote.control?name={}" """Make a new connection.""" @@ -60,6 +62,21 @@ def control(self, key): _key_interval = 1.0 + def is_tv_on(self): + url = "http://{}:{}/api/v2/" + url = url.format(self._config['host'], self._config['port']) + try: + res = requests.get(url, timeout=5) + except (requests.exceptions.Timeout, + requests.exceptions.ConnectionError, + requests.exceptions.HTTPError, + requests.exceptions.ReadTimeout): + return False + if res is not None and res.status_code == 200: + return True + else: + return False + def _read_response(self): response = self.connection.recv() response = json.loads(response) From c30105ef3121107f1ea03207b3eabe335cdebcde Mon Sep 17 00:00:00 2001 From: Touliloup Date: Sun, 21 Jan 2018 18:08:00 +0100 Subject: [PATCH 2/2] Resolve merge conflict --- samsungctl/remote_websocket.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/samsungctl/remote_websocket.py b/samsungctl/remote_websocket.py index 4069c92..98d11d9 100644 --- a/samsungctl/remote_websocket.py +++ b/samsungctl/remote_websocket.py @@ -25,8 +25,6 @@ def __init__(self, config): config["timeout"] = None self._config = config - - URL_FORMAT = "ws://{}:{}/api/v2/channels/samsung.remote.control?name={}" url = URL_FORMAT.format(config["host"], config["port"], self._serialize_string(config["name"])) @@ -69,8 +67,8 @@ def control(self, key): _key_interval = 1.0 def is_tv_on(self): - url = "http://{}:{}/api/v2/" - url = url.format(self._config['host'], self._config['port']) + base_url = "http://{}:{}/api/v2/" + url = base_url.format(self._config['host'], self._config['port']) try: res = requests.get(url, timeout=5) except (requests.exceptions.Timeout,