Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

New method to query TV status (ON/OFF) #71

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions samsungctl/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ def close(self):

def control(self, key):
return self.remote.control(key)

def is_tv_on(self):
return self.remote.is_tv_on()
12 changes: 12 additions & 0 deletions samsungctl/remote_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,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],
Expand Down
18 changes: 18 additions & 0 deletions samsungctl/remote_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import socket
import time
import requests

from . import exceptions

Expand All @@ -12,6 +13,7 @@

class RemoteWebsocket():
"""Object for remote control connection."""
_config = None

def __init__(self, config):
import websocket
Expand All @@ -22,6 +24,7 @@ def __init__(self, config):
if config["timeout"] == 0:
config["timeout"] = None

self._config = config
url = URL_FORMAT.format(config["host"], config["port"],
self._serialize_string(config["name"]))

Expand Down Expand Up @@ -63,6 +66,21 @@ def control(self, key):

_key_interval = 1.0

def is_tv_on(self):
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,
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)
Expand Down