revert changes, dont wait for response but use all detected dps

This commit is contained in:
Martín Villagra
2021-11-29 03:39:22 +00:00
parent 23e48b791a
commit 3cbe6751d3

View File

@@ -92,13 +92,13 @@ PAYLOAD_DICT = {
STATUS: {"hexByte": 0x0A, "command": {"gwId": "", "devId": ""}}, STATUS: {"hexByte": 0x0A, "command": {"gwId": "", "devId": ""}},
SET: {"hexByte": 0x07, "command": {"devId": "", "uid": "", "t": ""}}, SET: {"hexByte": 0x07, "command": {"devId": "", "uid": "", "t": ""}},
HEARTBEAT: {"hexByte": 0x09, "command": {}}, HEARTBEAT: {"hexByte": 0x09, "command": {}},
UPDATEDPS: {"hexByte": 0x12, "command": {"dpId": [4, 5, 6, 18, 19, 20]}}, UPDATEDPS: {"hexByte": 0x12, "command": {"dpId": [18, 19, 20]}},
}, },
"type_0d": { "type_0d": {
STATUS: {"hexByte": 0x0D, "command": {"devId": "", "uid": "", "t": ""}}, STATUS: {"hexByte": 0x0D, "command": {"devId": "", "uid": "", "t": ""}},
SET: {"hexByte": 0x07, "command": {"devId": "", "uid": "", "t": ""}}, SET: {"hexByte": 0x07, "command": {"devId": "", "uid": "", "t": ""}},
HEARTBEAT: {"hexByte": 0x09, "command": {}}, HEARTBEAT: {"hexByte": 0x09, "command": {}},
UPDATEDPS: {"hexByte": 0x12, "command": {"dpId": [4, 5, 6, 18, 19, 20]}}, UPDATEDPS: {"hexByte": 0x12, "command": {"dpId": [18, 19, 20]}},
}, },
} }
@@ -211,11 +211,9 @@ class AESCipher:
class MessageDispatcher(ContextualLogger): class MessageDispatcher(ContextualLogger):
"""Buffer and dispatcher for Tuya messages.""" """Buffer and dispatcher for Tuya messages."""
# Heartbeats and updatedps always respond with sequence number 0, # Heartbeats always respond with sequence number 0, so they can't be waited for like
# so they can't be waited for like other messages. # other messages. This is a hack to allow waiting for heartbeats.
# This is a hack to allow waiting for them.
HEARTBEAT_SEQNO = -100 HEARTBEAT_SEQNO = -100
UPDATEDPS_SEQNO = -101
def __init__(self, dev_id, listener): def __init__(self, dev_id, listener):
"""Initialize a new MessageBuffer.""" """Initialize a new MessageBuffer."""
@@ -300,26 +298,9 @@ class MessageDispatcher(ContextualLogger):
sem.release() sem.release()
elif msg.cmd == 0x12: elif msg.cmd == 0x12:
self.debug("Got normal updatedps response") self.debug("Got normal updatedps response")
if self.UPDATEDPS_SEQNO in self.listeners:
sem = self.listeners[self.UPDATEDPS_SEQNO]
self.listeners[self.UPDATEDPS_SEQNO] = msg
if isinstance(sem, asyncio.Semaphore):
sem.release()
elif msg.cmd == 0x08: elif msg.cmd == 0x08:
# If we have an open updatedps call then this is for it. self.debug("Got status update")
# Some devices send 0x12 and 0x08 in response to a updatedps. self.listener(msg)
# Empty DPS responses here are always for updatedps
# but hey we haven't decoded yet to know
if self.UPDATEDPS_SEQNO in self.listeners and isinstance(
self.listeners[self.UPDATEDPS_SEQNO], asyncio.Semaphore
):
self.debug("Got status type updatedps response")
sem = self.listeners[self.UPDATEDPS_SEQNO]
self.listeners[self.UPDATEDPS_SEQNO] = msg
sem.release()
else:
self.debug("Got status update")
self.listener(msg)
else: else:
self.debug( self.debug(
"Got message type %d for unknown listener %d: %s", "Got message type %d for unknown listener %d: %s",
@@ -465,13 +446,12 @@ class TuyaProtocol(asyncio.Protocol, ContextualLogger):
payload = self._generate_payload(command, dps) payload = self._generate_payload(command, dps)
dev_type = self.dev_type dev_type = self.dev_type
# Wait for special sequence number if heartbeat or updatedps # Wait for special sequence number if heartbeat
if command == HEARTBEAT: seqno = (
seqno = MessageDispatcher.HEARTBEAT_SEQNO MessageDispatcher.HEARTBEAT_SEQNO
elif command == UPDATEDPS: if command == HEARTBEAT
seqno = MessageDispatcher.UPDATEDPS_SEQNO else (self.seqno - 1)
else: )
seqno = self.seqno - 1
self.transport.write(payload) self.transport.write(payload)
msg = await self.dispatcher.wait_for(seqno) msg = await self.dispatcher.wait_for(seqno)
@@ -509,10 +489,17 @@ class TuyaProtocol(asyncio.Protocol, ContextualLogger):
Request device to update index. Request device to update index.
Args: Args:
dps([int]): list of dps to update dps([int]): list of dps to update, default=all detected
""" """
if self.version == 3.3: if self.version == 3.3:
return await self.exchange(UPDATEDPS, dps) if dps is None:
if not self.dps_cache:
await self.detect_available_dps()
if self.dps_cache:
dps = [int(dp) for dp in self.dps_cache][:255]
self.debug("updatedps() entry (dps %s)", dps)
payload = self._generate_payload(UPDATEDPS, dps)
self.transport.write(payload)
return True return True
async def set_dp(self, value, dp_index): async def set_dp(self, value, dp_index):