123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674 |
- from __future__ import annotations
- from typing import Any
- from bleak.backends.device import BLEDevice
- from bleak.backends.scanner import AdvertisementData
- from switchbot import LockStatus, SwitchbotModel
- from switchbot.adv_parser import parse_advertisement_data
- from switchbot.models import SwitchBotAdvertisement
- ADVERTISEMENT_DATA_DEFAULTS = {
- "local_name": "",
- "manufacturer_data": {},
- "service_data": {},
- "service_uuids": [],
- "rssi": -127,
- "platform_data": ((),),
- "tx_power": -127,
- }
- BLE_DEVICE_DEFAULTS = {
- "name": None,
- "rssi": -127,
- "details": None,
- }
- def generate_ble_device(
- address: str | None = None,
- name: str | None = None,
- details: Any | None = None,
- rssi: int | None = None,
- **kwargs: Any,
- ) -> BLEDevice:
- """Generate a BLEDevice with defaults."""
- new = kwargs.copy()
- if address is not None:
- new["address"] = address
- if name is not None:
- new["name"] = name
- if details is not None:
- new["details"] = details
- if rssi is not None:
- new["rssi"] = rssi
- for key, value in BLE_DEVICE_DEFAULTS.items():
- new.setdefault(key, value)
- return BLEDevice(**new)
- def generate_advertisement_data(**kwargs: Any) -> AdvertisementData:
- """Generate advertisement data with defaults."""
- new = kwargs.copy()
- for key, value in ADVERTISEMENT_DATA_DEFAULTS.items():
- new.setdefault(key, value)
- return AdvertisementData(**new)
- def test_parse_advertisement_data_curtain():
- """Test parse_advertisement_data for curtain."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xe7\xabF\xac\x8f\x92|\x0f\x00\x11\x04"},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"c\xc0X\x00\x11\x04"},
- rssi=-80,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "rawAdvData": b"c\xc0X\x00\x11\x04",
- "data": {
- "calibration": True,
- "battery": 88,
- "inMotion": False,
- "position": 100,
- "lightLevel": 1,
- "deviceChain": 1,
- },
- "isEncrypted": False,
- "model": "c",
- "modelFriendlyName": "Curtain",
- "modelName": SwitchbotModel.CURTAIN,
- },
- device=ble_device,
- rssi=-80,
- active=True,
- )
- def test_parse_advertisement_data_curtain_passive():
- """Test parse_advertisement_data for curtain passive."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xe7\xabF\xac\x8f\x92|\x0f\x00\x11\x04"},
- service_data={},
- rssi=-80,
- )
- result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.CURTAIN)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "rawAdvData": None,
- "data": {
- "calibration": None,
- "battery": None,
- "inMotion": False,
- "position": 100,
- "lightLevel": 1,
- "deviceChain": 1,
- },
- "isEncrypted": False,
- "model": "c",
- "modelFriendlyName": "Curtain",
- "modelName": SwitchbotModel.CURTAIN,
- },
- device=ble_device,
- rssi=-80,
- active=False,
- )
- def test_parse_advertisement_data_curtain_passive_12_bytes():
- """Test parse_advertisement_data for curtain passive."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xe7\xabF\xac\x8f\x92|\x0f\x00\x11\x04\x00"},
- service_data={},
- rssi=-80,
- )
- result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.CURTAIN)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "rawAdvData": None,
- "data": {
- "calibration": None,
- "battery": None,
- "inMotion": False,
- "position": 100,
- "lightLevel": 1,
- "deviceChain": 1,
- },
- "isEncrypted": False,
- "model": "c",
- "modelFriendlyName": "Curtain",
- "modelName": SwitchbotModel.CURTAIN,
- },
- device=ble_device,
- rssi=-80,
- active=False,
- )
- def test_parse_advertisement_data_curtain_position_zero():
- """Test parse_advertisement_data for curtain position zero."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- local_name="WoCurtain",
- manufacturer_data={89: b"\xc1\xc7'}U\xab"},
- service_data={"00000d00-0000-1000-8000-00805f9b34fb": b"c\xd0\xced\x11\x04"},
- service_uuids=[
- "00001800-0000-1000-8000-00805f9b34fb",
- "00001801-0000-1000-8000-00805f9b34fb",
- "cba20d00-224d-11e6-9fb8-0002a5d5c51b",
- ],
- rssi=-52,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "rawAdvData": b"c\xd0\xced\x11\x04",
- "data": {
- "calibration": True,
- "battery": 78,
- "inMotion": False,
- "position": 0,
- "lightLevel": 1,
- "deviceChain": 1,
- },
- "isEncrypted": False,
- "model": "c",
- "modelFriendlyName": "Curtain",
- "modelName": SwitchbotModel.CURTAIN,
- },
- device=ble_device,
- rssi=-52,
- active=True,
- )
- def test_parse_advertisement_data_curtain_firmware_six_position_100():
- """Test parse_advertisement_data with firmware six for curtain position 100."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- local_name="WoCurtain",
- manufacturer_data={
- 89: b"\xf5\x98\x94\x08\xa0\xe7",
- 2409: b'\xf5\x98\x94\x08\xa0\xe7\x9b\x0f\x00"\x04',
- },
- service_data={
- "00000d00-0000-1000-8000-00805f9b34fb": b"c\xd0H\x00\x12\x04",
- "0000fd3d-0000-1000-8000-00805f9b34fb": b'c\xc0G\x00"\x04',
- },
- service_uuids=[
- "00001800-0000-1000-8000-00805f9b34fb",
- "00001801-0000-1000-8000-00805f9b34fb",
- "cba20d00-224d-11e6-9fb8-0002a5d5c51b",
- ],
- rssi=-62,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "rawAdvData": b'c\xc0G\x00"\x04',
- "data": {
- "calibration": True,
- "battery": 71,
- "inMotion": False,
- "position": 100,
- "lightLevel": 2,
- "deviceChain": 2,
- },
- "isEncrypted": False,
- "model": "c",
- "modelFriendlyName": "Curtain",
- "modelName": SwitchbotModel.CURTAIN,
- },
- device=ble_device,
- rssi=-62,
- active=True,
- )
- def test_parse_advertisement_data_curtain_firmware_six_position_100_other_rssi():
- """Test parse_advertisement_data with firmware six for curtain position 100 other rssi."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- local_name="WoCurtain",
- manufacturer_data={
- 89: b"\xf5\x98\x94\x08\xa0\xe7",
- 2409: b'\xf5\x98\x94\x08\xa0\xe7\xa5\x0fc"\x04',
- },
- service_data={
- "00000d00-0000-1000-8000-00805f9b34fb": b"c\xd0H\x00\x12\x04",
- "0000fd3d-0000-1000-8000-00805f9b34fb": b'c\xc0Gc"\x04',
- },
- service_uuids=[
- "00001800-0000-1000-8000-00805f9b34fb",
- "00001801-0000-1000-8000-00805f9b34fb",
- "cba20d00-224d-11e6-9fb8-0002a5d5c51b",
- ],
- rssi=-67,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "rawAdvData": b'c\xc0Gc"\x04',
- "data": {
- "calibration": True,
- "battery": 71,
- "inMotion": False,
- "position": 1,
- "lightLevel": 2,
- "deviceChain": 2,
- },
- "isEncrypted": False,
- "model": "c",
- "modelFriendlyName": "Curtain",
- "modelName": SwitchbotModel.CURTAIN,
- },
- device=ble_device,
- rssi=-67,
- active=True,
- )
- def test_parse_advertisement_data_curtain_fully_closed():
- """Test parse_advertisement_data with firmware six fully closed."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- local_name="WoCurtain",
- manufacturer_data={2409: b"\xc1\xc7'}U\xab\"\x0fd\x11\x04"},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"c\xc0Sd\x11\x04"},
- service_uuids=[
- "00001800-0000-1000-8000-00805f9b34fb",
- "00001801-0000-1000-8000-00805f9b34fb",
- "cba20d00-224d-11e6-9fb8-0002a5d5c51b",
- ],
- rssi=1,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "rawAdvData": b"c\xc0Sd\x11\x04",
- "data": {
- "calibration": True,
- "battery": 83,
- "inMotion": False,
- "position": 0,
- "lightLevel": 1,
- "deviceChain": 1,
- },
- "isEncrypted": False,
- "model": "c",
- "modelFriendlyName": "Curtain",
- "modelName": SwitchbotModel.CURTAIN,
- },
- device=ble_device,
- rssi=1,
- active=True,
- )
- def test_parse_advertisement_data_curtain_fully_open():
- """Test parse_advertisement_data with firmware six fully open."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- local_name="WoCurtain",
- manufacturer_data={2409: b"\xc1\xc7'}U\xab%\x0f\x00\x11\x04"},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"c\xc0S\x00\x11\x04"},
- service_uuids=[
- "00001800-0000-1000-8000-00805f9b34fb",
- "00001801-0000-1000-8000-00805f9b34fb",
- "cba20d00-224d-11e6-9fb8-0002a5d5c51b",
- ],
- rssi=1,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "rawAdvData": b"c\xc0S\x00\x11\x04",
- "data": {
- "calibration": True,
- "battery": 83,
- "inMotion": False,
- "position": 100,
- "lightLevel": 1,
- "deviceChain": 1,
- },
- "isEncrypted": False,
- "model": "c",
- "modelFriendlyName": "Curtain",
- "modelName": SwitchbotModel.CURTAIN,
- },
- device=ble_device,
- rssi=1,
- active=True,
- )
- def test_parse_advertisement_data_curtain3():
- """Test parse_advertisement_data for curtain 3."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={
- 2409: b"\xaa\xbb\xcc\xdd\xee\xff\xf7\x07\x00\x11\x04\x00\x49"
- },
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"{\xc0\x49\x00\x11\x04"},
- rssi=-80,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "rawAdvData": b"{\xc0\x49\x00\x11\x04",
- "data": {
- "calibration": True,
- "battery": 73,
- "inMotion": False,
- "position": 100,
- "lightLevel": 1,
- "deviceChain": 1,
- },
- "isEncrypted": False,
- "model": "{",
- "modelFriendlyName": "Curtain 3",
- "modelName": SwitchbotModel.CURTAIN,
- },
- device=ble_device,
- rssi=-80,
- active=True,
- )
- def test_parse_advertisement_data_curtain3_passive():
- """Test parse_advertisement_data for curtain passive."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={
- 2409: b"\xaa\xbb\xcc\xdd\xee\xff\xf7\x07\x00\x11\x04\x00\x49"
- },
- service_data={},
- rssi=-80,
- )
- result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.CURTAIN)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "rawAdvData": None,
- "data": {
- "calibration": None,
- "battery": 73,
- "inMotion": False,
- "position": 100,
- "lightLevel": 1,
- "deviceChain": 1,
- },
- "isEncrypted": False,
- "model": "c",
- "modelFriendlyName": "Curtain",
- "modelName": SwitchbotModel.CURTAIN,
- },
- device=ble_device,
- rssi=-80,
- active=False,
- )
- def test_parse_advertisement_data_contact():
- """Test parse_advertisement_data for the contact sensor."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xe7\xabF\xac\x8f\x92|\x0f\x00\x11\x04"},
- service_data={
- "0000fd3d-0000-1000-8000-00805f9b34fb": b"d@d\x05\x00u\x00\xf8\x12"
- },
- rssi=-80,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "rawAdvData": b"d@d\x05\x00u\x00\xf8\x12",
- "data": {
- "button_count": 2,
- "contact_open": True,
- "contact_timeout": True,
- "is_light": True,
- "battery": 100,
- "motion_detected": True,
- "tested": False,
- },
- "isEncrypted": False,
- "model": "d",
- "modelFriendlyName": "Contact Sensor",
- "modelName": SwitchbotModel.CONTACT_SENSOR,
- },
- device=ble_device,
- rssi=-80,
- active=True,
- )
- def test_parse_advertisement_data_empty():
- """Test parse_advertisement_data with empty data does not blow up."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2403: b"\xe7\xabF\xac\x8f\x92|\x0f\x00\x11\x04"},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b""},
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result is None
- def test_new_bot_firmware():
- """Test parsing adv data from new bot firmware."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={89: b"\xd8.\xad\xcd\r\x85"},
- service_data={"00000d00-0000-1000-8000-00805f9b34fb": b"H\x10\xe1"},
- service_uuids=["CBA20D00-224D-11E6-9FB8-0002A5D5C51B"],
- rssi=-90,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "rawAdvData": b"H\x10\xe1",
- "data": {"switchMode": False, "isOn": False, "battery": 97},
- "model": "H",
- "isEncrypted": False,
- "modelFriendlyName": "Bot",
- "modelName": SwitchbotModel.BOT,
- },
- device=ble_device,
- rssi=-90,
- active=True,
- )
- def test_parse_advertisement_data_curtain_firmware_six_fully_closed():
- """Test parse_advertisement_data with firmware six fully closed."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- local_name="WoCurtain",
- manufacturer_data={
- 89: b"\xcc\xf4\xc4\xf9\xacl",
- 2409: b"\xcc\xf4\xc4\xf9\xacl\xeb\x0fd\x12\x04",
- },
- service_data={
- "00000d00-0000-1000-8000-00805f9b34fb": b"c\xd0Yd\x11\x04",
- "0000fd3d-0000-1000-8000-00805f9b34fb": b"c\xc0dd\x12\x04",
- },
- service_uuids=[
- "00001800-0000-1000-8000-00805f9b34fb",
- "00001801-0000-1000-8000-00805f9b34fb",
- "cba20d00-224d-11e6-9fb8-0002a5d5c51b",
- ],
- rssi=-2,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "rawAdvData": b"c\xc0dd\x12\x04",
- "data": {
- "calibration": True,
- "battery": 100,
- "inMotion": False,
- "position": 0,
- "lightLevel": 1,
- "deviceChain": 2,
- },
- "isEncrypted": False,
- "model": "c",
- "modelFriendlyName": "Curtain",
- "modelName": SwitchbotModel.CURTAIN,
- },
- device=ble_device,
- rssi=-2,
- active=True,
- )
- def test_parse_advertisement_data_curtain_firmware_six_fully_open():
- """Test parse_advertisement_data with firmware six fully open."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- local_name="WoCurtain",
- manufacturer_data={
- 89: b"\xcc\xf4\xc4\xf9\xacl",
- 2409: b"\xcc\xf4\xc4\xf9\xacl\xe2\x0f\x00\x12\x04",
- },
- service_data={
- "00000d00-0000-1000-8000-00805f9b34fb": b"c\xd0Yd\x11\x04",
- "0000fd3d-0000-1000-8000-00805f9b34fb": b"c\xc0d\x00\x12\x04",
- },
- service_uuids=[
- "00001800-0000-1000-8000-00805f9b34fb",
- "00001801-0000-1000-8000-00805f9b34fb",
- "cba20d00-224d-11e6-9fb8-0002a5d5c51b",
- ],
- rssi=-2,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "rawAdvData": b"c\xc0d\x00\x12\x04",
- "data": {
- "calibration": True,
- "battery": 100,
- "inMotion": False,
- "position": 100,
- "lightLevel": 1,
- "deviceChain": 2,
- },
- "isEncrypted": False,
- "model": "c",
- "modelFriendlyName": "Curtain",
- "modelName": SwitchbotModel.CURTAIN,
- },
- device=ble_device,
- rssi=-2,
- active=True,
- )
- def test_contact_sensor_mfr():
- """Test parsing adv data from new bot firmware."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xcb9\xcd\xc4=FA,\x00F\x01\x8f\xc4"},
- service_data={
- "0000fd3d-0000-1000-8000-00805f9b34fb": b"d\x00\xda\x04\x00F\x01\x8f\xc4"
- },
- tx_power=-127,
- rssi=-70,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": 90,
- "button_count": 4,
- "contact_open": True,
- "contact_timeout": True,
- "is_light": False,
- "motion_detected": False,
- "tested": False,
- },
- "isEncrypted": False,
- "model": "d",
- "modelFriendlyName": "Contact Sensor",
- "modelName": SwitchbotModel.CONTACT_SENSOR,
- "rawAdvData": b"d\x00\xda\x04\x00F\x01\x8f\xc4",
- },
- device=ble_device,
- rssi=-70,
- active=True,
- )
- def test_contact_sensor_mfr_no_service_data():
- """Test contact sensor with passive data only."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xcb9\xcd\xc4=FA,\x00F\x01\x8f\xc4"},
- service_data={},
- tx_power=-127,
- rssi=-70,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- # Passive detection of contact sensor is not supported
- # anymore since the Switchbot Curtain v3 was released
- # which uses the heuristics for the contact sensor.
- assert result is None
- def test_contact_sensor_srv():
- """Test parsing adv data from new bot firmware."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- service_data={
- "0000fd3d-0000-1000-8000-00805f9b34fb": b"d\x00\xda\x04\x00F\x01\x8f\xc4"
- },
- tx_power=-127,
- rssi=-70,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": 90,
- "button_count": 4,
- "contact_open": True,
- "contact_timeout": True,
- "is_light": False,
- "motion_detected": False,
- "tested": False,
- },
- "isEncrypted": False,
- "model": "d",
- "modelFriendlyName": "Contact Sensor",
- "modelName": SwitchbotModel.CONTACT_SENSOR,
- "rawAdvData": b"d\x00\xda\x04\x00F\x01\x8f\xc4",
- },
- device=ble_device,
- rssi=-70,
- active=True,
- )
- def test_contact_sensor_open():
- """Test parsing mfr adv data from new bot firmware."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xcb9\xcd\xc4=F\x84\x9c\x00\x17\x00QD"},
- service_data={
- "0000fd3d-0000-1000-8000-00805f9b34fb": b"d@\xda\x02\x00\x17\x00QD"
- },
- tx_power=-127,
- rssi=-59,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": 90,
- "button_count": 4,
- "contact_open": True,
- "contact_timeout": False,
- "is_light": False,
- "motion_detected": True,
- "tested": False,
- },
- "isEncrypted": False,
- "model": "d",
- "modelFriendlyName": "Contact Sensor",
- "modelName": SwitchbotModel.CONTACT_SENSOR,
- "rawAdvData": b"d@\xda\x02\x00\x17\x00QD",
- },
- device=ble_device,
- rssi=-59,
- active=True,
- )
- def test_contact_sensor_closed():
- """Test parsing mfr adv data from new bot firmware."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xcb9\xcd\xc4=F\x89\x8c\x00+\x00\x19\x84"},
- service_data={
- "0000fd3d-0000-1000-8000-00805f9b34fb": b"d@\xda\x00\x00+\x00\x19\x84"
- },
- tx_power=-127,
- rssi=-50,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": 90,
- "button_count": 4,
- "contact_open": False,
- "contact_timeout": False,
- "is_light": False,
- "motion_detected": True,
- "tested": False,
- },
- "isEncrypted": False,
- "model": "d",
- "modelFriendlyName": "Contact Sensor",
- "modelName": SwitchbotModel.CONTACT_SENSOR,
- "rawAdvData": b"d@\xda\x00\x00+\x00\x19\x84",
- },
- device=ble_device,
- rssi=-50,
- active=True,
- )
- def test_switchbot_passive():
- """Test parsing switchbot as passive."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={89: bytes.fromhex("d51cfb397856")},
- service_data={},
- tx_power=-127,
- rssi=-50,
- )
- result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.BOT)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": None,
- "switchMode": None,
- "isOn": None,
- },
- "isEncrypted": False,
- "model": "H",
- "modelFriendlyName": "Bot",
- "modelName": SwitchbotModel.BOT,
- "rawAdvData": None,
- },
- device=ble_device,
- rssi=-50,
- active=False,
- )
- def test_bulb_active():
- """Test parsing bulb as active."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\x84\xf7\x03\xb4\xcbz\x03\xe4!\x00\x00"},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"u\x00d"},
- tx_power=-127,
- rssi=-50,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "brightness": 100,
- "color_mode": 1,
- "delay": False,
- "isOn": True,
- "loop_index": 0,
- "preset": False,
- "sequence_number": 3,
- "speed": 0,
- },
- "isEncrypted": False,
- "model": "u",
- "modelFriendlyName": "Color Bulb",
- "modelName": SwitchbotModel.COLOR_BULB,
- "rawAdvData": b"u\x00d",
- },
- device=ble_device,
- rssi=-50,
- active=True,
- )
- def test_wosensor_passive_and_active():
- """Test parsing wosensor as passive with active data as well."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xd7\xc1}]\xebC\xde\x03\x06\x985"},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"T\x00\xe4\x06\x985"},
- tx_power=-127,
- rssi=-50,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": 100,
- "fahrenheit": False,
- "humidity": 53,
- "temp": {"c": 24.6, "f": 76.28},
- "temperature": 24.6,
- },
- "isEncrypted": False,
- "model": "T",
- "modelFriendlyName": "Meter",
- "modelName": SwitchbotModel.METER,
- "rawAdvData": b"T\x00\xe4\x06\x985",
- },
- device=ble_device,
- rssi=-50,
- active=True,
- )
- def test_wosensor_active():
- """Test parsing wosensor with active data as well."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"T\x00\xe4\x06\x985"},
- tx_power=-127,
- rssi=-50,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": 100,
- "fahrenheit": False,
- "humidity": 53,
- "temp": {"c": 24.6, "f": 76.28},
- "temperature": 24.6,
- },
- "isEncrypted": False,
- "model": "T",
- "modelFriendlyName": "Meter",
- "modelName": SwitchbotModel.METER,
- "rawAdvData": b"T\x00\xe4\x06\x985",
- },
- device=ble_device,
- rssi=-50,
- active=True,
- )
- def test_wosensor_passive_only():
- """Test parsing wosensor with only passive data."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xd7\xc1}]\xebC\xde\x03\x06\x985"},
- service_data={},
- tx_power=-127,
- rssi=-50,
- )
- result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.METER)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": None,
- "fahrenheit": False,
- "humidity": 53,
- "temp": {"c": 24.6, "f": 76.28},
- "temperature": 24.6,
- },
- "isEncrypted": False,
- "model": "T",
- "modelFriendlyName": "Meter",
- "modelName": SwitchbotModel.METER,
- "rawAdvData": None,
- },
- device=ble_device,
- rssi=-50,
- active=False,
- )
- def test_wosensor_active_zero_data():
- """Test parsing wosensor with active data but all values are zero."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"T\x00\x00\x00\x00\x00"},
- tx_power=-127,
- rssi=-50,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {},
- "isEncrypted": False,
- "model": "T",
- "rawAdvData": b"T\x00\x00\x00\x00\x00",
- },
- device=ble_device,
- rssi=-50,
- active=True,
- )
- def test_wohub2_passive_and_active():
- """Test parsing wosensor as passive with active data as well."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={
- 2409: b"\xaa\xbb\xcc\xdd\xee\xff\x00\xfffT\x1a\xf1\x82\x07\x9a2\x00"
- },
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"v\x00"},
- tx_power=-127,
- rssi=-50,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "fahrenheit": False,
- "humidity": 50,
- "lightLevel": 2,
- "temp": {"c": 26.7, "f": 80.06},
- "temperature": 26.7,
- },
- "isEncrypted": False,
- "model": "v",
- "modelFriendlyName": "Hub 2",
- "modelName": SwitchbotModel.HUB2,
- "rawAdvData": b"v\x00",
- },
- device=ble_device,
- rssi=-50,
- active=True,
- )
- def test_woiosensor_passive_and_active():
- """Test parsing woiosensor as passive with active data as well."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xaa\xbb\xcc\xdd\xee\xff\xe0\x0f\x06\x985\x00"},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"w\x00\xe4"},
- tx_power=-127,
- rssi=-50,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": 100,
- "fahrenheit": False,
- "humidity": 53,
- "temp": {"c": 24.6, "f": 76.28},
- "temperature": 24.6,
- },
- "isEncrypted": False,
- "model": "w",
- "modelFriendlyName": "Indoor/Outdoor Meter",
- "modelName": SwitchbotModel.IO_METER,
- "rawAdvData": b"w\x00\xe4",
- },
- device=ble_device,
- rssi=-50,
- active=True,
- )
- def test_woiosensor_passive_only():
- """Test parsing woiosensor with only passive data."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xaa\xbb\xcc\xdd\xee\xff\xe0\x0f\x06\x985\x00"},
- service_data={},
- tx_power=-127,
- rssi=-50,
- )
- result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.IO_METER)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": None,
- "fahrenheit": False,
- "humidity": 53,
- "temp": {"c": 24.6, "f": 76.28},
- "temperature": 24.6,
- },
- "isEncrypted": False,
- "model": "w",
- "modelFriendlyName": "Indoor/Outdoor Meter",
- "modelName": SwitchbotModel.IO_METER,
- "rawAdvData": None,
- },
- device=ble_device,
- rssi=-50,
- active=False,
- )
- def test_motion_sensor_clear():
- """Test parsing motion sensor with clear data."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIj\x1c\x00f"},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"s\x00\xe2\x00f\x01"},
- tx_power=-127,
- rssi=-87,
- )
- result = parse_advertisement_data(
- ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
- )
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": 98,
- "iot": 0,
- "is_light": False,
- "led": 0,
- "light_intensity": 1,
- "motion_detected": False,
- "sense_distance": 0,
- "tested": False,
- },
- "isEncrypted": False,
- "model": "s",
- "modelFriendlyName": "Motion Sensor",
- "modelName": SwitchbotModel.MOTION_SENSOR,
- "rawAdvData": b"s\x00\xe2\x00f\x01",
- },
- device=ble_device,
- rssi=-87,
- active=True,
- )
- def test_motion_sensor_clear_passive():
- """Test parsing motion sensor with clear data."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIj\x1c\x00f"},
- service_data={},
- tx_power=-127,
- rssi=-87,
- )
- result = parse_advertisement_data(
- ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
- )
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": None,
- "iot": None,
- "is_light": False,
- "led": None,
- "light_intensity": None,
- "motion_detected": False,
- "sense_distance": None,
- "tested": None,
- },
- "isEncrypted": False,
- "model": "s",
- "modelFriendlyName": "Motion Sensor",
- "modelName": SwitchbotModel.MOTION_SENSOR,
- "rawAdvData": None,
- },
- device=ble_device,
- rssi=-87,
- active=False,
- )
- def test_motion_sensor_motion():
- """Test parsing motion sensor with motion data."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIi\\\x008"},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"s@\xe2\x008\x01"},
- tx_power=-127,
- rssi=-87,
- )
- result = parse_advertisement_data(
- ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
- )
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": 98,
- "iot": 0,
- "is_light": False,
- "led": 0,
- "light_intensity": 1,
- "motion_detected": True,
- "sense_distance": 0,
- "tested": False,
- },
- "isEncrypted": False,
- "model": "s",
- "modelFriendlyName": "Motion Sensor",
- "modelName": SwitchbotModel.MOTION_SENSOR,
- "rawAdvData": b"s@\xe2\x008\x01",
- },
- device=ble_device,
- rssi=-87,
- active=True,
- )
- def test_motion_sensor_motion_passive():
- """Test parsing motion sensor with motion data."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIi\\\x008"},
- service_data={},
- tx_power=-127,
- rssi=-87,
- )
- result = parse_advertisement_data(
- ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
- )
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": None,
- "iot": None,
- "is_light": False,
- "led": None,
- "light_intensity": None,
- "motion_detected": True,
- "sense_distance": None,
- "tested": None,
- },
- "isEncrypted": False,
- "model": "s",
- "modelFriendlyName": "Motion Sensor",
- "modelName": SwitchbotModel.MOTION_SENSOR,
- "rawAdvData": None,
- },
- device=ble_device,
- rssi=-87,
- active=False,
- )
- def test_motion_sensor_is_light_passive():
- """Test parsing motion sensor with motion data."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIs,\x04g"},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"s\x00\xe2\x04g\x02"},
- tx_power=-127,
- rssi=-93,
- )
- result = parse_advertisement_data(
- ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
- )
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": 98,
- "iot": 0,
- "is_light": True,
- "led": 0,
- "light_intensity": 2,
- "motion_detected": False,
- "sense_distance": 0,
- "tested": False,
- },
- "isEncrypted": False,
- "model": "s",
- "modelFriendlyName": "Motion Sensor",
- "modelName": SwitchbotModel.MOTION_SENSOR,
- "rawAdvData": b"s\x00\xe2\x04g\x02",
- },
- device=ble_device,
- rssi=-93,
- active=True,
- )
- def test_motion_sensor_is_light_active():
- """Test parsing motion sensor with motion data."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"s\x00\xe2\x04g\x02"},
- tx_power=-127,
- rssi=-93,
- )
- result = parse_advertisement_data(
- ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
- )
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": 98,
- "iot": 0,
- "is_light": True,
- "led": 0,
- "light_intensity": 2,
- "motion_detected": False,
- "sense_distance": 0,
- "tested": False,
- },
- "isEncrypted": False,
- "model": "s",
- "modelFriendlyName": "Motion Sensor",
- "modelName": SwitchbotModel.MOTION_SENSOR,
- "rawAdvData": b"s\x00\xe2\x04g\x02",
- },
- device=ble_device,
- rssi=-93,
- active=True,
- )
- def test_motion_with_light_detected():
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xc0!\x9a\xe8\xbcIvl\x00,"},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"s@\xe2\x00,\x02"},
- tx_power=-127,
- rssi=-84,
- )
- result = parse_advertisement_data(
- ble_device, adv_data, SwitchbotModel.MOTION_SENSOR
- )
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": 98,
- "iot": 0,
- "is_light": True,
- "led": 0,
- "light_intensity": 2,
- "motion_detected": True,
- "sense_distance": 0,
- "tested": False,
- },
- "isEncrypted": False,
- "model": "s",
- "modelFriendlyName": "Motion Sensor",
- "modelName": SwitchbotModel.MOTION_SENSOR,
- "rawAdvData": b"s@\xe2\x00,\x02",
- },
- device=ble_device,
- rssi=-84,
- active=True,
- )
- def test_parsing_lock_active():
- """Test parsing lock with active data."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xf1\t\x9fE\x1a]\x07\x83\x00 "},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"o\x80d"},
- rssi=-67,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "auto_lock_paused": False,
- "battery": 100,
- "calibration": True,
- "door_open": False,
- "double_lock_mode": False,
- "night_latch": False,
- "status": LockStatus.LOCKED,
- "unclosed_alarm": False,
- "unlocked_alarm": False,
- "update_from_secondary_lock": False,
- },
- "isEncrypted": False,
- "model": "o",
- "modelFriendlyName": "Lock",
- "modelName": SwitchbotModel.LOCK,
- "rawAdvData": b"o\x80d",
- },
- device=ble_device,
- rssi=-67,
- active=True,
- )
- def test_parsing_lock_passive():
- """Test parsing lock with active data."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xf1\t\x9fE\x1a]\x07\x83\x00 "}, rssi=-67
- )
- result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LOCK)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "auto_lock_paused": False,
- "battery": None,
- "calibration": True,
- "door_open": False,
- "double_lock_mode": False,
- "night_latch": False,
- "status": LockStatus.LOCKED,
- "unclosed_alarm": False,
- "unlocked_alarm": False,
- "update_from_secondary_lock": False,
- },
- "isEncrypted": False,
- "model": "o",
- "modelFriendlyName": "Lock",
- "modelName": SwitchbotModel.LOCK,
- "rawAdvData": None,
- },
- device=ble_device,
- rssi=-67,
- active=False,
- )
- def test_parsing_lock_pro_active():
- """Test parsing lock pro with active data."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xc8\xf5,\xd9-V\x07\x82\x00d\x00\x00"},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"$\x80d"},
- rssi=-80,
- )
- result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LOCK_PRO)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": 100,
- "calibration": True,
- "status": LockStatus.LOCKED,
- "update_from_secondary_lock": False,
- "door_open": False,
- "double_lock_mode": False,
- "unclosed_alarm": False,
- "unlocked_alarm": False,
- "auto_lock_paused": False,
- "night_latch": False,
- },
- "model": "$",
- "isEncrypted": False,
- "modelFriendlyName": "Lock Pro",
- "modelName": SwitchbotModel.LOCK_PRO,
- "rawAdvData": b"$\x80d",
- },
- device=ble_device,
- rssi=-80,
- active=True,
- )
- def test_parsing_lock_pro_passive():
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: bytes.fromhex("aabbccddeeff208200640000")}, rssi=-67
- )
- result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LOCK_PRO)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": None,
- "calibration": True,
- "status": LockStatus.LOCKED,
- "update_from_secondary_lock": False,
- "door_open": False,
- "double_lock_mode": False,
- "unclosed_alarm": False,
- "unlocked_alarm": False,
- "auto_lock_paused": False,
- "night_latch": False,
- },
- "model": "$",
- "isEncrypted": False,
- "modelFriendlyName": "Lock Pro",
- "modelName": SwitchbotModel.LOCK_PRO,
- "rawAdvData": None,
- },
- device=ble_device,
- rssi=-67,
- active=False,
- )
- def test_parsing_lock_pro_passive_nightlatch_disabled():
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: bytes.fromhex("aabbccddeeff0a8200630000")}, rssi=-67
- )
- result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LOCK_PRO)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": None,
- "calibration": True,
- "status": LockStatus.LOCKED,
- "update_from_secondary_lock": False,
- "door_open": False,
- "double_lock_mode": False,
- "unclosed_alarm": False,
- "unlocked_alarm": False,
- "auto_lock_paused": False,
- "night_latch": False,
- },
- "model": "$",
- "isEncrypted": False,
- "modelFriendlyName": "Lock Pro",
- "modelName": SwitchbotModel.LOCK_PRO,
- "rawAdvData": None,
- },
- device=ble_device,
- rssi=-67,
- active=False,
- )
- def test_parsing_lock_active_old_firmware():
- """Test parsing lock with active data. Old firmware."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xf1\t\x9fE\x1a]\x07\x83\x00"},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"o\x80d"},
- rssi=-67,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "auto_lock_paused": False,
- "battery": 100,
- "calibration": True,
- "door_open": False,
- "double_lock_mode": False,
- "night_latch": False,
- "status": LockStatus.LOCKED,
- "unclosed_alarm": False,
- "unlocked_alarm": False,
- "update_from_secondary_lock": False,
- },
- "isEncrypted": False,
- "model": "o",
- "modelFriendlyName": "Lock",
- "modelName": SwitchbotModel.LOCK,
- "rawAdvData": b"o\x80d",
- },
- device=ble_device,
- rssi=-67,
- active=True,
- )
- def test_parsing_lock_passive_old_firmware():
- """Test parsing lock with active data. Old firmware."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xf1\t\x9fE\x1a]\x07\x83\x00"}, rssi=-67
- )
- result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.LOCK)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "auto_lock_paused": False,
- "battery": None,
- "calibration": True,
- "door_open": False,
- "double_lock_mode": False,
- "night_latch": False,
- "status": LockStatus.LOCKED,
- "unclosed_alarm": False,
- "unlocked_alarm": False,
- "update_from_secondary_lock": False,
- },
- "isEncrypted": False,
- "model": "o",
- "modelFriendlyName": "Lock",
- "modelName": SwitchbotModel.LOCK,
- "rawAdvData": None,
- },
- device=ble_device,
- rssi=-67,
- active=False,
- )
- def test_meter_pro_active() -> None:
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xb0\xe9\xfeR\xdd\x84\x06d\x08\x97,\x00\x05"},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"4\x00d"},
- rssi=-67,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": 100,
- "fahrenheit": False,
- "humidity": 44,
- "temp": {"c": 23.8, "f": 74.84},
- "temperature": 23.8,
- },
- "isEncrypted": False,
- "model": "4",
- "modelFriendlyName": "Meter",
- "modelName": SwitchbotModel.METER_PRO,
- "rawAdvData": b"4\x00d",
- },
- device=ble_device,
- rssi=-67,
- active=True,
- )
- def test_meter_pro_passive() -> None:
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xb0\xe9\xfeR\xdd\x84\x06d\x08\x97,\x00\x05"},
- rssi=-67,
- )
- result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.METER_PRO)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": None,
- "fahrenheit": False,
- "humidity": 44,
- "temp": {"c": 23.8, "f": 74.84},
- "temperature": 23.8,
- },
- "isEncrypted": False,
- "model": "4",
- "modelFriendlyName": "Meter",
- "modelName": SwitchbotModel.METER_PRO,
- "rawAdvData": None,
- },
- device=ble_device,
- rssi=-67,
- active=False,
- )
- def test_meter_pro_c_active() -> None:
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={
- 2409: b"\xb0\xe9\xfeT2\x15\xb7\xe4\x07\x9b\xa4\x007\x02\xd5\x00"
- },
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"5\x00d"},
- rssi=-67,
- )
- result = parse_advertisement_data(ble_device, adv_data)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": 100,
- "fahrenheit": True,
- "humidity": 36,
- "temp": {"c": 27.7, "f": 81.86},
- "temperature": 27.7,
- "co2": 725,
- },
- "isEncrypted": False,
- "model": "5",
- "modelFriendlyName": "Meter",
- "modelName": SwitchbotModel.METER_PRO_C,
- "rawAdvData": b"5\x00d",
- },
- device=ble_device,
- rssi=-67,
- active=True,
- )
- def test_meter_pro_c_passive() -> None:
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={
- 2409: b"\xb0\xe9\xfeT2\x15\xb7\xe4\x07\x9b\xa4\x007\x02\xd5\x00"
- },
- rssi=-67,
- )
- result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.METER_PRO_C)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {
- "battery": None,
- "fahrenheit": True,
- "humidity": 36,
- "temp": {"c": 27.7, "f": 81.86},
- "temperature": 27.7,
- "co2": 725,
- },
- "isEncrypted": False,
- "model": "5",
- "modelFriendlyName": "Meter",
- "modelName": SwitchbotModel.METER_PRO_C,
- "rawAdvData": None,
- },
- device=ble_device,
- rssi=-67,
- active=False,
- )
- def test_parse_advertisement_data_keypad():
- """Test parse_advertisement_data for the keypad."""
- ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
- adv_data = generate_advertisement_data(
- manufacturer_data={2409: b"\xeb\x13\x02\xe6#\x0f\x8fd\x00\x00\x00\x00"},
- service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"y\x00d"},
- rssi=-67,
- )
- result = parse_advertisement_data(ble_device, adv_data, SwitchbotModel.KEYPAD)
- assert result == SwitchBotAdvertisement(
- address="aa:bb:cc:dd:ee:ff",
- data={
- "data": {"attempt_state": 143, "battery": 100},
- "isEncrypted": False,
- "model": "y",
- "modelFriendlyName": "Keypad",
- "modelName": SwitchbotModel.KEYPAD,
- "rawAdvData": b"y\x00d",
- },
- device=ble_device,
- rssi=-67,
- active=True,
- )
|