Skip to content

Commit

Permalink
Fix error message on run applications scan at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
ollo69 committed Feb 1, 2023
1 parent c97395b commit 5ae1403
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
54 changes: 33 additions & 21 deletions custom_components/samsungtv_smart/api/samsungws.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from .shortcuts import SamsungTVShortcuts

DEFAULT_POWER_ON_DELAY = 120
MIN_APP_SCAN_INTERVAL = 8
MIN_APP_SCAN_INTERVAL = 9
MAX_APP_VALIDITY_SEC = 60
MAX_WS_PING_INTERVAL = 10
PING_TIMEOUT = 3
Expand Down Expand Up @@ -277,18 +277,19 @@ def __init__(
self._app_type = {}
self._sync_lock = Lock()
self._last_app_scan = datetime.min
self._is_connected = False

self._ping_thread = None
self._ping_thread_run = False

self._ws_remote = None
self._client_remote = None
self._last_ping = datetime.min
self._is_connected = False

self._ws_control = None
self._client_control = None
self._last_control_ping = datetime.min
self._is_control_connected = False

self._ws_art = None
self._client_art = None
Expand Down Expand Up @@ -386,7 +387,13 @@ def _set_token(self, token):
self._new_token_callback()

def _ws_send(
self, command, key_press_delay=None, *, use_control=False, ws_socket=None
self,
command,
key_press_delay=None,
*,
use_control=False,
ws_socket=None,
raise_on_closed=False,
):
"""Send a command using the appropriate websocket."""
using_remote = False
Expand All @@ -406,6 +413,8 @@ def _ws_send(
try:
connection.send(payload)
except websocket.WebSocketConnectionClosedException:
if raise_on_closed:
raise
_LOGGING.warning("_ws_send: connection is closed, send command failed")
if using_remote or use_control:
_LOGGING.info("_ws_send: try to restart communication threads")
Expand Down Expand Up @@ -586,6 +595,7 @@ def _client_control_thread(self):
# we set ping interval (1 hour) only to enable multi-threading mode
# on socket. TV do not answer to ping but send ping to client
self._run_forever(self._ws_control, sslopt=sslopt, ping_interval=3600)
self._is_control_connected = False
self._ws_control.close()
self._ws_control = None
self._running_app_changed = None
Expand Down Expand Up @@ -621,6 +631,7 @@ def _on_message_control(self, _, message):
if not self._check_conn_id(conn_data):
return
_LOGGING.debug("Message control: received connect")
self._is_control_connected = True
self._get_running_app()
elif event == "ed.installedApp.get":
_LOGGING.debug("Message control: received installedApp")
Expand All @@ -638,8 +649,6 @@ def _set_running_app(self, response):
return

call_time = datetime.utcnow()
with self._sync_lock:
self._last_app_scan = call_time
self._last_running_scan = call_time
self._running_apps[app_id] = call_time
if self._running_app:
Expand Down Expand Up @@ -682,24 +691,27 @@ def _get_app_status(self, app_id, app_type):
"""Send a message to control WS channel to get the app status."""
_LOGGING.debug("Get app status: AppID: %s, AppType: %s", app_id, app_type)

# if app_type == 4:
# method = "ms.webapplication.get"
# else:
# method = "ms.application.get"
if not (self._ws_control and self._is_control_connected):
return

if app_type == 4: # app type 4 always return not found error
return

method = "ms.application.get"
self._ws_send(
{
"id": app_id,
"method": method,
"params": {"id": app_id},
},
key_press_delay=0,
use_control=True,
ws_socket=self._ws_control,
)
try:
self._ws_send(
{
"id": app_id,
"method": method,
"params": {"id": app_id},
},
key_press_delay=0,
use_control=True,
ws_socket=self._ws_control,
raise_on_closed=True,
)
except websocket.WebSocketConnectionClosedException:
_LOGGING.debug("Get app status aborted: connection closed")

def _client_art_thread(self):
"""Start the client art WS thread used to manage art mode status."""
Expand Down Expand Up @@ -909,14 +921,14 @@ def _notify_app_change(self):

def _get_running_app(self, *, force_scan=False):
"""Query current running app using control channel."""
if not self._ws_control:
if not (self._ws_control and self._is_control_connected):
return

scan_interval = 1 if force_scan else MIN_APP_SCAN_INTERVAL
with self._sync_lock:
call_time = datetime.utcnow()
difference = (call_time - self._last_app_scan).total_seconds()
if difference <= scan_interval:
if difference < scan_interval:
return
self._last_app_scan = call_time

Expand Down
2 changes: 1 addition & 1 deletion custom_components/samsungtv_smart/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"codeowners": ["@jaruba", "@ollo69", "@screwdgeh"],
"config_flow": true,
"iot_class": "cloud_polling",
"version": "0.11.4"
"version": "0.11.5"
}

0 comments on commit 5ae1403

Please sign in to comment.