12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649 |
- 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,
- )
|