def try_connect(self): """Try to connect to the Websocket TV.""" for self.port in (8002, 8001): config = { CONF_NAME: VALUE_CONF_NAME, CONF_HOST: self.host, CONF_METHOD: self.method, CONF_PORT: self.port, # We need this high timeout because waiting for auth popup is just an open socket CONF_TIMEOUT: 31, CONF_TOKEN: self.token, } try: LOGGER.debug("Try config: %s", _hide_token(config)) with SamsungTVWS( host=self.host, port=self.port, token=self.token, timeout=config[CONF_TIMEOUT], name=config[CONF_NAME], ) as remote: remote.open() self.token = remote.token LOGGER.debug("Working config: %s", _hide_token(config)) return RESULT_SUCCESS except (WebSocketException, ConnectionFailure): LOGGER.debug("Working but unsupported config: %s", _hide_token(config)) return RESULT_NOT_SUPPORTED except OSError as err: LOGGER.debug("Failing config: %s, error: %s", _hide_token(config), err) return RESULT_NOT_SUCCESSFUL
def __init__(self, host, port, name, timeout, mac, uuid, update_method, update_custom_ping_url, source_list, app_list): """Initialize the Samsung device.""" # Save a reference to the imported classes self._host = host self._name = name self._timeout = timeout self._mac = mac self._update_method = update_method self._update_custom_ping_url = update_custom_ping_url self._source_list = json.loads(source_list) self._app_list = json.loads(app_list) if app_list is not None else None self._uuid = uuid self._is_ws_connection = True if port in (8001, 8002) else False # Assume that the TV is not muted and volume is 0 self._muted = False # Assume that the TV is in Play mode self._playing = True self._state = None # Mark the end of a shutdown command (need to wait 15 seconds before # sending the next command to avoid turning the TV back ON). self._end_of_power_off = None self._token_file = None # Generate token file only for WS + SSL + Token connection if port == 8002: self._gen_token_file() self._remote = SamsungTVWS(name=name, host=host, port=port, timeout=self._timeout, key_press_delay=KEY_PRESS_TIMEOUT, token_file=self._token_file)
def try_connect(self, port): """Try to connect to the Websocket TV.""" for self.port in (8001, 8002): if port is not None and port != self.port: continue config = { "name": "HomeAssistant", "description": "HomeAssistant", "host": self.host, "method": self.method, "port": self.port, # We need this high timeout because waiting for auth popup is just an open socket "timeout": 31, "token": self.token, } try: LOGGER.debug("Try config: %s", config) with SamsungTVWS( host=self.host, port=self.port, token=self.token, timeout=config["timeout"], name=config["name"], ) as remote: remote.open() LOGGER.debug("Working config: %s", config) LOGGER.debug("Token: %s", self.token) return RESULT_SUCCESS except WebSocketException: LOGGER.debug("Working but unsupported config: %s", config) return RESULT_NOT_SUPPORTED except (OSError, ConnectionFailure) as err: LOGGER.debug("Failing config: %s, error: %s", config, err) return RESULT_NOT_SUCCESSFUL
def connect(self, host: Optional[str] = None, port: Optional[int] = None) -> SamsungTVWS: host, port = self._get_host_and_port(host, port) if (host, port) not in self._connections: self._connections[(host, port)] = SamsungTVWS(host=host, port=port, token_file=self.token_file, timeout=self.timeout, name=self.name) return self._connections[(host, port)]
def _get_remote(self): """Create or return a remote control instance.""" if self._remote is None: # We need to create a new instance to reconnect. LOGGER.debug("Create SamsungTVWS") self._remote = SamsungTVWS( host=self.config["host"], port=self.config["port"], token=self.config["token"], timeout=self.config["timeout"], name=self.config["name"], ) self._remote.open() return self._remote
def _try_connect_samsungtvws(self, port): if self._port is None or port == self._port: token_file = None if port == 8002: token_file = _get_token_file(self._host) config = { "name": "HomeAssistant", "description": "HomeAssistant", "host": self._host, "method": "websocket", "port": port, # We need this high timeout because waiting for auth popup is just an open socket "timeout": 31, "token_file": token_file, } try: LOGGER.debug("Try config: %s", config) # with SamsungTVWS( # host=self._host, # port=port, # token_file=token_file, # timeout=config["timeout"], # name=config["name"], # ) as remote: remote = SamsungTVWS( host=self._host, port=port, token_file=token_file, timeout=config["timeout"], name=config["name"], ) remote.open() LOGGER.debug("Working config: %s", config) self._method = "websocket" self._port = port self._token_file = token_file return RESULT_SUCCESS except AccessDenied: LOGGER.debug("Working but denied config: %s", config) return RESULT_AUTH_MISSING except UnhandledResponse: LOGGER.debug("Working but unsupported config: %s", config) return RESULT_NOT_SUPPORTED except OSError as err: LOGGER.debug("Failing config: %s, error: %s", config, err) except AttributeError as err: LOGGER.debug("Failing config: %s, error: %s", config, err) return RESULT_NOT_SUCCESSFUL
def _get_remote(self): """Create or return a remote control instance.""" if self._remote is None: # We need to create a new instance to reconnect. try: LOGGER.debug("Create SamsungTVWS") self._remote = SamsungTVWS( host=self.config[CONF_HOST], port=self.config[CONF_PORT], token=self.config[CONF_TOKEN], timeout=self.config[CONF_TIMEOUT], name=self.config[CONF_NAME], ) self._remote.open() # This is only happening when the auth was switched to DENY # A removed auth will lead to socket timeout because waiting for auth popup is just an open socket except ConnectionFailure: self._notify_callback() raise return self._remote
def try_connect(self): """Try to connect to the Websocket TV.""" for self.port in WEBSOCKET_PORTS: config = { CONF_NAME: VALUE_CONF_NAME, CONF_HOST: self.host, CONF_METHOD: self.method, CONF_PORT: self.port, # We need this high timeout because waiting for auth popup is just an open socket CONF_TIMEOUT: TIMEOUT_REQUEST, } result = None try: LOGGER.debug("Try config: %s", config) with SamsungTVWS( host=self.host, port=self.port, token=self.token, timeout=config[CONF_TIMEOUT], name=config[CONF_NAME], ) as remote: remote.open() self.token = remote.token if self.token: config[CONF_TOKEN] = "*****" LOGGER.debug("Working config: %s", config) return RESULT_SUCCESS except WebSocketException as err: LOGGER.debug("Working but unsupported config: %s, error: %s", config, err) result = RESULT_NOT_SUPPORTED except (OSError, ConnectionFailure) as err: LOGGER.debug("Failing config: %s, error: %s", config, err) # pylint: disable=useless-else-on-loop else: if result: return result return RESULT_CANNOT_CONNECT
def _get_remote(self): """Create or return a remote control instance.""" if self._remote is None: # We need to create a new instance to reconnect. try: LOGGER.debug("Create SamsungTVWS") self._remote = SamsungTVWS( host=self.host, port=self.port, token=self.token, timeout=1, name=VALUE_CONF_NAME, ) self._remote.open() # This is only happening when the auth was switched to DENY # A removed auth will lead to socket timeout because waiting for auth popup is just an open socket except ConnectionFailure: self._notify_callback() raise except WebSocketException: self._remote = None return self._remote
def __init__(self, host, port, name, timeout, mac, uuid, update_method, update_custom_ping_url, source_list, app_list): """Initialize the Samsung device.""" # Save a reference to the imported classes self._name = name self._host = host self._mac = mac self._update_method = update_method self._update_custom_ping_url = update_custom_ping_url self._source_list = json.loads(source_list) self._app_list = json.loads(app_list) if app_list is not None else None self._uuid = uuid self._is_ws_connection = True if port in (8001, 8002) else False # Assume that the TV is not muted and volume is 0 self._muted = False # Assume that the TV is in Play mode self._playing = True self._state = None # Mark the end of a shutdown command (need to wait 15 seconds before # sending the next command to avoid turning the TV back ON). self._end_of_power_off = None token_file = None if port == 8002: token_file = os.path.dirname(os.path.realpath(__file__)) + '/token-' + host + '.txt' # For correct set of auth token if os.path.isfile(token_file) is False: timeout = 30 self._remote = SamsungTVWS( name=name, host=host, port=port, timeout=timeout, key_press_delay=KEY_PRESS_TIMEOUT, token_file=token_file )
def _get_remote(self, avoid_open: bool = False): """Create or return a remote control instance.""" if self._remote is None: # We need to create a new instance to reconnect. try: LOGGER.debug("Create SamsungTVWSBridge for %s (%s)", CONF_NAME, self.host) self._remote = SamsungTVWS( host=self.host, port=self.port, token=self.token, timeout=TIMEOUT_WEBSOCKET, name=VALUE_CONF_NAME, ) if not avoid_open: self._remote.open() # This is only happening when the auth was switched to DENY # A removed auth will lead to socket timeout because waiting for auth popup is just an open socket except ConnectionFailure: self._notify_callback() except (WebSocketException, OSError): self._remote = None return self._remote
import logging import os import sys import wakeonlan sys.path.append("../") from samsungtvws import SamsungTVWS # noqa: E402 # Increase debug level logging.basicConfig(level=logging.INFO) # Normal constructor tv = SamsungTVWS("192.168.xxx.xxx") # Autosave token to file token_file = os.path.dirname(os.path.realpath(__file__)) + "/tv-token.txt" tv = SamsungTVWS(host="192.168.xxx.xxx", port=8002, token_file=token_file) # Toggle power tv.shortcuts().power() # Power On wakeonlan.send_magic_packet("CC:6E:A4:xx:xx:xx") # Open web in browser tv.open_browser("https://duckduckgo.com/") # View installed apps apps = tv.app_list()
import sys import os import logging import wakeonlan sys.path.append('../') from samsungtvws import SamsungTVWS # Increase debug level logging.basicConfig(level=logging.INFO) # Normal constructor tv = SamsungTVWS('192.168.xxx.xxx') # Autosave token to file token_file = os.path.dirname(os.path.realpath(__file__)) + '/tv-token.txt' tv = SamsungTVWS(host='192.168.xxx.xxx', port=8002, token_file=token_file) # Toggle power tv.shortcuts().power() # Power On wakeonlan.send_magic_packet('CC:6E:A4:xx:xx:xx') # Open web in browser tv.open_browser('https://duckduckgo.com/') # View installed apps apps = tv.app_list() logging.info(apps)
SAT = "sat" CD = "cd" FM = "fm" AM = "am" NET = "net" PC = "game" USB = "usb" BT = "bluetooth" cube_on = False second_slide = False pause_on = False last_volume = 80 sys.path.append('../') token_file = os.path.dirname(os.path.realpath(__file__)) + '/tv-token' tvr = SamsungTVWS(host=TVHOST, port=TVPORT, token_file=token_file) vsxr = eiscp.eISCP(VSXHOST) dr = 'http://' + DHOST + ':8080/control/rcu' def bd_send(command): bdr = telnetlib.Telnet(BDHOST, BDPORT) bdr.write(command) bdr.close() def bd_channel_up(): bd_send(NEXT) def bd_channel_down():
# import os import wakeonlan from time import sleep from random import randrange TV_IP_ADDR = '192.168.0.38' TV_MAC_ADDR = '00:7C:2D:06:89:40' NETFLIX_APP_ID = '11101200001' from samsungtvws import SamsungTVWS, SamsungTVShortcuts token_file = os.path.dirname(os.path.realpath(__file__)) + '/token.txt' tv = SamsungTVWS(host=TV_IP_ADDR, port=8002, token_file=token_file) remote_ctrl: SamsungTVShortcuts = tv.shortcuts() # Wake up TV Set wakeonlan.send_magic_packet(TV_MAC_ADDR) # check is Netfix app running and close it tv.rest_app_close(NETFLIX_APP_ID) while True: app = tv.rest_app_status(NETFLIX_APP_ID) if not app.get("running"): break sleep(1) # run netflix tv.run_app(NETFLIX_APP_ID) sleep(10)