Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace time.sleep() with asyncio.sleep() #192

Open
wants to merge 2 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
17 changes: 9 additions & 8 deletions custom_components/samsungtv_tizen/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ def device_class(self):
"""Set the device class to TV."""
return DEVICE_CLASS_TV

def turn_on(self):
async def turn_on(self):
"""Turn the media player on."""
if self._power_off_in_progress():
self._end_of_power_off = None
Expand All @@ -752,11 +752,11 @@ def turn_on(self):
if self._broadcast:
for i in range(20):
wakeonlan.send_magic_packet(self._mac, ip_address=self._broadcast)
time.sleep(0.25)
await asyncio.sleep(0.25)
else:
for i in range(20):
wakeonlan.send_magic_packet(self._mac)
time.sleep(0.25)
await asyncio.sleep(0.25)
#Force Update as send command not called
self.update(no_throttle=True)
self.schedule_update_ha_state(True)
Expand Down Expand Up @@ -872,7 +872,7 @@ async def async_play_media(self, media_type, media_id, **kwargs):
else:
#Change to TV source before changing channel
self.hass.async_add_job(self._smartthings_keys, "ST_TV")
time.sleep(5)
await asyncio.sleep(5)
smartthings.device_update(self)
if self._cloud_channel != media_id:
await self.hass.async_add_job(self._smartthings_keys, f"ST_CH{media_id}")
Expand All @@ -889,7 +889,7 @@ async def async_play_media(self, media_type, media_id, **kwargs):
if source.lower() in ["tv", "live tv", "livetv"]:
found_source = True
await self.hass.async_add_job(self.async_select_source, source)
time.sleep(2)
await asyncio.sleep(2)
break
if found_source == False:
keychain = "KEY_EXIT+KEY_EXIT+{}".format(keychain)
Expand All @@ -911,13 +911,14 @@ async def async_play_media(self, media_type, media_id, **kwargs):
for this_key in all_source_keys:
if this_key.isdigit():
last_was_delay = True
time.sleep(int(this_key)/1000)
await asyncio.sleep(int(this_key)/1000)

else:
if this_key.startswith("ST_"):
await self.hass.async_add_job(self._smartthings_keys, this_key)
else:
if last_was_delay == False:
time.sleep(DEFAULT_KEY_CHAIN_DELAY)
await asyncio.sleep(DEFAULT_KEY_CHAIN_DELAY)
last_was_delay = False
self.hass.async_add_job(self.send_command, this_key)
elif source_key.startswith("ST_"):
Expand Down Expand Up @@ -955,7 +956,7 @@ async def async_select_source(self, source):
all_source_keys = source_key.split("+")
for this_key in all_source_keys:
if this_key.isdigit():
time.sleep(int(this_key)/1000)
await asyncio.sleep(int(this_key)/1000)
else:
if this_key.startswith("ST_"):
await self.hass.async_add_job(self._smartthings_keys, this_key)
Expand Down
11 changes: 6 additions & 5 deletions custom_components/samsungtv_tizen/websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Boston, MA 02110-1335 USA

"""
import asyncio
import base64
import json
import logging
Expand Down Expand Up @@ -104,17 +105,17 @@ def _set_token(self, token):
else:
self.token = token

def _ws_send(self, command, key_press_delay=None):
async def _ws_send(self, command, key_press_delay=None):
if self.connection is None:
self.open()

payload = json.dumps(command)
self.connection.send(payload)

if key_press_delay is None:
time.sleep(self.key_press_delay)
await asyncio.sleep(self.key_press_delay)
else:
time.sleep(key_press_delay)
await asyncio.sleep(key_press_delay)

def _rest_request(self, target, method='GET'):
url = self._format_rest_url(target)
Expand Down Expand Up @@ -192,9 +193,9 @@ def send_key(self, key, key_press_delay=None, cmd='Click'):
key_press_delay
)

def hold_key(self, key, seconds):
async def hold_key(self, key, seconds):
self.send_key(key, cmd='Press')
time.sleep(seconds)
await asyncio.sleep(seconds)
self.send_key(key, cmd='Release')

def move_cursor(self, x, y, duration=0):
Expand Down