Skip to content
This repository has been archived by the owner on Oct 7, 2021. It is now read-only.

Commit

Permalink
Better fix for #3, also fixes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulAnnekov committed Aug 30, 2019
1 parent c63a197 commit eb4d6f3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
20 changes: 14 additions & 6 deletions tuyaha/devices/switch.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import time
from tuyaha.devices.base import TuyaDevice

class TuyaSwitch(TuyaDevice):

def state(self):
devices = self.api.discover_devices()
state = None
for device in devices:
if device['id'] == self.obj_id:
state = device['data']['state']
return state
state = self.data.get('state')
if state is None:
return None
return state
Expand All @@ -18,3 +14,15 @@ def turn_on(self):

def turn_off(self):
self.api.device_control(self.obj_id, 'turnOnOff', {'value': '0'})

# workaround for https://github.com/PaulAnnekov/tuyaha/issues/3
def update(self):
"""Avoid get cache value after control."""
time.sleep(0.5)
devices = self.api.discovery()
if not devices:
return
for device in devices:
if device['id'] == self.obj_id:
self.data = device['data']
return True
14 changes: 10 additions & 4 deletions tuyaha/tuyaapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,21 @@ def poll_devices_update(self):
self.check_access_token()
return self.discover_devices()

def discover_devices(self):
def discovery(self):
response = self._request('Discovery', 'discovery')
if response and response['header']['code'] == 'SUCCESS':
SESSION.devices = []
for device in response['payload']['devices']:
SESSION.devices.extend(get_tuya_device(device, self))
return response['payload']['devices']
return None

def discover_devices(self):
devices = self.discovery()
if not devices:
return None
SESSION.devices = []
for device in devices:
SESSION.devices.extend(get_tuya_device(device, self))
return devices

def get_devices_by_type(self, dev_type):
device_list = []
for device in SESSION.devices:
Expand Down

0 comments on commit eb4d6f3

Please sign in to comment.