Skip to content

Commit

Permalink
Cache tweaks, documentation, and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
uzlonewolf committed Aug 5, 2024
1 parent d78e86e commit 17fd66a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
3 changes: 2 additions & 1 deletion tinytuya/BulbDevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@

import colorsys

from .core import * # pylint: disable=W0401, W0614
from .core import Device, log
from .core import error_json, ERR_JSON, ERR_RANGE, ERR_STATE

class BulbDevice(Device):
"""
Expand Down
19 changes: 14 additions & 5 deletions tinytuya/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
device_info(dev_id) # Searches DEVICEFILE (usually devices.json) for devices with ID = dev_id and returns just that device
assign_dp_mappings(tuyadevices, mappings) # Adds mappings to all the devices in the tuyadevices list
decrypt_udp(msg) # Decrypts a UDP network broadcast packet
merge_dps_results(dest, src) # Merge multiple receive() responses into a single dict
# `src` will be combined with and merged into `dest`
Device Functions
json = status() # returns json payload
Expand Down Expand Up @@ -872,6 +874,7 @@ def __init__(
self.remote_nonce = b''
self.payload_dict = None
self._last_status = {}
self._have_status = False
self.max_simultaneous_dps = max_simultaneous_dps if max_simultaneous_dps else 0
self.version = 0.0
self.version_str = 'v0.0'
Expand Down Expand Up @@ -937,7 +940,7 @@ def _get_socket(self, renew):
self.socket = None
if self.socket is None:
# Set up Socket
self._last_status = {}
self.cache_clear()
retries = 0
err = ERR_OFFLINE
while retries < self.socketRetryLimit:
Expand Down Expand Up @@ -1009,7 +1012,7 @@ def _check_socket_close(self, force=False):
if (force or not self.socketPersistent) and self.socket:
self.socket.close()
self.socket = None
self._last_status = {}
self.cache_clear()

def _recv_all(self, length):
tries = 2
Expand Down Expand Up @@ -1598,7 +1601,7 @@ def cached_status(self, nowait=False):
json from status() if nowait=False, or
None if nowait=True
"""
if (not self.socketPersistent) or (not self.socket) or (not self._last_status):
if (not self._have_status) or (not self.socketPersistent) or (not self.socket) or (not self._last_status):
if not nowait:
log.debug("Last status caching not available, requesting status from device")
return self.status()
Expand All @@ -1610,6 +1613,7 @@ def cached_status(self, nowait=False):

def cache_clear(self):
self._last_status = {}
self._have_status = False

def subdev_query( self, nowait=False ):
"""Query for a list of sub-devices and their status"""
Expand Down Expand Up @@ -1671,7 +1675,7 @@ def set_socketPersistent(self, persist):
if self.socket and not persist:
self.socket.close()
self.socket = None
self._last_status = {}
self.cache_clear()

def set_socketNODELAY(self, nodelay):
self.socketNODELAY = nodelay
Expand Down Expand Up @@ -1800,6 +1804,10 @@ def _merge_payload_dicts(dict1, dict2):

if command_override is None:
command_override = command

if command == DP_QUERY:
self._have_status = True

if json_data is None:
# I have yet to see a device complain about included but unneeded attribs, but they *will*
# complain about missing attribs, so just include them all unless otherwise specified
Expand Down Expand Up @@ -2147,7 +2155,8 @@ def deviceScan(verbose=False, maxretry=None, color=True, poll=True, forcescan=Fa
from . import scanner
return scanner.devices(verbose=verbose, scantime=maxretry, color=color, poll=poll, forcescan=forcescan, byID=byID)

# Merge multiple results into a single dict
# Merge multiple receive() responses into a single dict
# `src` will be combined with and merged into `dest`
def merge_dps_results(dest, src):
if src and isinstance(src, dict) and 'Error' not in src and 'Err' not in src:
for k in src:
Expand Down

0 comments on commit 17fd66a

Please sign in to comment.