diff --git a/aiohomekit/controller/coap/connection.py b/aiohomekit/controller/coap/connection.py index 786c3dc7..9654ddee 100644 --- a/aiohomekit/controller/coap/connection.py +++ b/aiohomekit/controller/coap/connection.py @@ -170,7 +170,7 @@ async def post_bytes(self, payload: bytes, timeout: int = 16.0): async def post( self, opcode: OpCode, iid: int, data: bytes - ) -> tuple[int, bytes | PDUStatus | ValueError]: + ) -> tuple[int, bytes | PDUStatus]: tid = random.randint(1, 254) req_pdu = encode_pdu(opcode, tid, iid, data) res_pdu = await self.post_bytes(req_pdu) @@ -178,7 +178,7 @@ async def post( async def post_all( self, opcode: OpCode, iids: list[int], data: list[bytes] - ) -> list[bytes | PDUStatus | ValueError]: + ) -> list[bytes | PDUStatus]: req_pdu = encode_all_pdus(opcode, iids, data) res_pdu = await self.post_bytes(req_pdu) @@ -478,8 +478,16 @@ async def read_characteristics(self, ids: list[tuple[int, int]]): results = dict() for (idx, result) in enumerate(pdu_results): - if isinstance(result, bytes): - aid_iid = ids[idx] + aid_iid = ids[idx] + if isinstance(result, PDUStatus): + logger.warning( + "Failed to read aid %d iid %d" % (int(aid_iid[0]), int(aid_iid[1])) + ) + results[aid_iid] = { + "description": result.description, + "status": -result.value, # XXX + } + else: # decode TLV to get byte value value = decode_pdu_03(result) if len(result) > 0 else b"" # find characteristic so we can get the data type @@ -502,20 +510,6 @@ async def read_characteristics(self, ids: list[tuple[int, int]]): value, ) ) - else: - logger.warning( - "Failed to read aid %d iid %d" % (int(aid_iid[0]), int(aid_iid[1])) - ) - if isinstance(result, PDUStatus): - results[aid_iid] = { - "description": result.description, - "status": -result.value, # XXX - } - else: - results[aid_iid] = { - "description": str(result), - "status": -777, # XXX something went badly wrong - } logger.debug(f"Read characteristics: {results!r}") return results @@ -555,11 +549,6 @@ async def write_characteristics(self, ids_values: list[tuple[int, int, Any]]): "descripton": result.description, "status": -result.value, # XXX } - elif isinstance(result, ValueError): - results[key] = { - "desciption": str(result), - "status": -777, # XXX something went badly wrong - } else: logger.debug( "Wrote value for aid %d iid %d" @@ -585,11 +574,6 @@ async def subscribe_to(self, ids: list[tuple[int, int]]): "descripton": result.description, "status": -result.value, # XXX } - elif isinstance(result, ValueError): - results[key] = { - "desciption": str(result), - "status": -777, # XXX something went badly wrong - } else: logger.debug( "Subscribed to aid %d iid %d" @@ -615,11 +599,6 @@ async def unsubscribe_from(self, ids: list[tuple[int, int]]): "descripton": result.description, "status": -result.value, # XXX } - elif isinstance(result, ValueError): - results[key] = { - "desciption": str(result), - "status": -777, # XXX something went badly wrong - } else: logger.debug( "Unsubscribed from aid %d iid %d" @@ -719,7 +698,5 @@ async def remove_pairing(self, pairing_id): raise AuthenticationError("Remove pairing failed") else: raise UnknownError("Remove pairing failed") - elif isinstance(result, ValueError): - raise UnknownError("Remove pairing failed") from result return True diff --git a/aiohomekit/controller/coap/pdu.py b/aiohomekit/controller/coap/pdu.py index de682538..377394f8 100644 --- a/aiohomekit/controller/coap/pdu.py +++ b/aiohomekit/controller/coap/pdu.py @@ -47,6 +47,9 @@ class PDUStatus(EnumWithDescription): INVALID_INSTANCE_ID = 4, "Invalid instance ID" INSUFFICIENT_AUTHENTICATION = 5, "Insufficient authentication" INVALID_REQUEST = 6, "Invalid request" + # custom error states + TID_MISMATCH = 256, "Transaction ID mismatch" + BAD_CONTROL = 257, "Control field doesn't have expected bits set" def encode_pdu(opcode: OpCode, tid: int, iid: int, data: bytes) -> bytes: @@ -70,9 +73,7 @@ def encode_all_pdus(opcode: OpCode, iids: list[int], data: list[bytes]) -> bytes return req_pdu -def decode_pdu( - expected_tid: int, data: bytes -) -> tuple[int, bytes | PDUStatus | ValueError]: +def decode_pdu(expected_tid: int, data: bytes) -> tuple[int, bytes | PDUStatus]: control, tid, status, body_len = struct.unpack("