Created
December 20, 2021 08:31
-
-
Save adrianlzt/49f8f5c563e97e516b6f4c8d8368d39e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from ble_monitor.ble_parser import BleParser | |
| import struct | |
| class TestXiaomi: | |
| """Tests for the Xiaomi parser""" | |
| def test_Xiaomi_RTCGQ02LM(self): | |
| """Simulando el dispositivo y añadiendo algún sensor adicional (switch)""" | |
| data_string = "02" # ext_packet=True si 0x0D | |
| data_string += "000000" # sin uso | |
| data_string += "0fc4e044ef54" # mac invertida | |
| adstruct = "16" # adstruct type | |
| adstruct += "95fe" # uuid 0xFE95, Xiaomi | |
| adstruct += "5059" # frctrl invertido, 0x5950, lo marco como no encriptado (el cuarto bit, empezando por el final) | |
| adstruct += "8d0a" # device id, invertido, RTCGQ02LM | |
| adstruct += "17" # packet id, HACE FALTA ROTARLO, no enviar siempre el mismo valor o lo descartará: https://github.com/custom-components/ble_monitor/blob/67d940ad90fa7e5f22f570088037655fef7b2ccf/custom_components/ble_monitor/ble_parser/xiaomi.py#L724 | |
| adstruct += "0fc4e044ef54" # mac invertida | |
| # adstruct motion payload | |
| adstruct += "0300" # obj0003 type (motion timer) | |
| adstruct += "01" # obj length | |
| adstruct += "01" # motion, 1 = on, 0 = off | |
| # adstruct button payload | |
| adstruct += "0110" # obj1001 type (button) | |
| adstruct += "03" # obj length | |
| adstruct += "0000" # button type + value | |
| adstruct += "05" # press type: 0 = single press, 1 = double press, 5, short press, 6 = long press | |
| # adstruct switch payload | |
| adstruct += "1210" # obj1012 type (switch) | |
| adstruct += "01" # obj length | |
| adstruct += "a1" # switch, podemos poner lo que queramos, devolvera entre 0 y 255 | |
| # adstruct prefijado con su size | |
| payload = "{:02x}".format(int(len(adstruct)/2)) + adstruct | |
| # Metemos el payload en el data_string prefijando con su longitud | |
| data_string += "{:02x}".format(int(len(payload)/2)) + payload | |
| data_string += "bb" # rssi | |
| # Prefijamos con 0x043e (no se que es, no se usa) y la longitud del data_string | |
| data_string = "043e{:02x}".format(int(len(data_string)/2)) + data_string | |
| data = bytes(bytearray.fromhex(data_string)) | |
| ble_parser = BleParser() | |
| breakpoint() | |
| sensor_msg, _ = ble_parser.parse_data(data) | |
| assert sensor_msg == { | |
| 'rssi': -69, | |
| 'mac': '54EF44E0C40F', | |
| 'type': 'RTCGQ02LM', | |
| 'packet': 23, | |
| 'firmware': 'Xiaomi (MiBeacon V5)', | |
| 'data': True, | |
| 'motion': 1, | |
| 'motion timer': 1, | |
| 'button': 'short press', | |
| 'switch': 161 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment