| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import time |
| 4 |
|
| 5 |
from habluetooth import BluetoothServiceInfo, BluetoothServiceInfoBleak |
| 6 |
|
| 7 |
from . import generate_advertisement_data, generate_ble_device |
| 8 |
|
| 9 |
SOURCE_LOCAL = "local" |
| 10 |
|
| 11 |
|
| 12 |
def test_model(): |
| 13 |
service_info = BluetoothServiceInfo( |
| 14 |
name="Test", |
| 15 |
address="00:00:00:00:00:00", |
| 16 |
rssi=0, |
| 17 |
manufacturer_data={97: b"\x00\x00\x00\x00\x00\x00"}, |
| 18 |
service_data={}, |
| 19 |
service_uuids=[], |
| 20 |
source=SOURCE_LOCAL, |
| 21 |
) |
| 22 |
|
| 23 |
assert service_info.manufacturer == "RDA Microelectronics" |
| 24 |
assert service_info.manufacturer_id == 97 |
| 25 |
|
| 26 |
service_info = BluetoothServiceInfo( |
| 27 |
name="Test", |
| 28 |
address="00:00:00:00:00:00", |
| 29 |
rssi=0, |
| 30 |
manufacturer_data={954547: b"\x00\x00\x00\x00\x00\x00"}, |
| 31 |
service_data={}, |
| 32 |
service_uuids=[], |
| 33 |
source=SOURCE_LOCAL, |
| 34 |
) |
| 35 |
|
| 36 |
assert service_info.manufacturer is None |
| 37 |
assert service_info.manufacturer_id == 954547 |
| 38 |
|
| 39 |
|
| 40 |
def test_model_from_bleak(): |
| 41 |
switchbot_device = generate_ble_device("44:44:33:11:23:45", "wohand", {}) |
| 42 |
switchbot_adv = generate_advertisement_data( |
| 43 |
local_name="wohand", service_uuids=["cba20d00-224d-11e6-9fb8-0002a5d5c51b"] |
| 44 |
) |
| 45 |
service_info = BluetoothServiceInfo.from_advertisement( |
| 46 |
switchbot_device, switchbot_adv, SOURCE_LOCAL |
| 47 |
) |
| 48 |
|
| 49 |
assert service_info.service_uuids == ["cba20d00-224d-11e6-9fb8-0002a5d5c51b"] |
| 50 |
assert service_info.name == "wohand" |
| 51 |
assert service_info.source == SOURCE_LOCAL |
| 52 |
assert service_info.manufacturer is None |
| 53 |
assert service_info.manufacturer_id is None |
| 54 |
|
| 55 |
|
| 56 |
def test_model_from_scanner(): |
| 57 |
switchbot_device = generate_ble_device("44:44:33:11:23:45", "wohand", {}) |
| 58 |
switchbot_adv = generate_advertisement_data( |
| 59 |
local_name="wohand", service_uuids=["cba20d00-224d-11e6-9fb8-0002a5d5c51b"] |
| 60 |
) |
| 61 |
now = time.monotonic() |
| 62 |
service_info = BluetoothServiceInfoBleak.from_scan( |
| 63 |
SOURCE_LOCAL, switchbot_device, switchbot_adv, now, True |
| 64 |
) |
| 65 |
|
| 66 |
assert service_info.service_uuids == ["cba20d00-224d-11e6-9fb8-0002a5d5c51b"] |
| 67 |
assert service_info.name == "wohand" |
| 68 |
assert service_info.source == SOURCE_LOCAL |
| 69 |
assert service_info.manufacturer is None |
| 70 |
assert service_info.manufacturer_id is None |
| 71 |
assert service_info.time == now |
| 72 |
assert service_info.connectable is True |
| 73 |
|
| 74 |
safe_as_dict = service_info.as_dict() |
| 75 |
assert safe_as_dict == { |
| 76 |
"address": "44:44:33:11:23:45", |
| 77 |
"advertisement": switchbot_adv, |
| 78 |
"device": switchbot_device, |
| 79 |
"connectable": True, |
| 80 |
"manufacturer_data": {}, |
| 81 |
"name": "wohand", |
| 82 |
"raw": None, |
| 83 |
"rssi": -127, |
| 84 |
"service_data": {}, |
| 85 |
"service_uuids": ["cba20d00-224d-11e6-9fb8-0002a5d5c51b"], |
| 86 |
"source": "local", |
| 87 |
"time": now, |
| 88 |
"tx_power": -127, |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
def test_construct_service_info_bleak(): |
| 93 |
switchbot_device = generate_ble_device("44:44:33:11:23:45", "wohand", {}) |
| 94 |
switchbot_adv = generate_advertisement_data( |
| 95 |
local_name="wohand", service_uuids=["cba20d00-224d-11e6-9fb8-0002a5d5c51b"] |
| 96 |
) |
| 97 |
now = time.monotonic() |
| 98 |
service_info = BluetoothServiceInfoBleak( |
| 99 |
name="wohand", |
| 100 |
address="44:44:33:11:23:45", |
| 101 |
rssi=-127, |
| 102 |
manufacturer_data=switchbot_adv.manufacturer_data, |
| 103 |
service_data=switchbot_adv.service_data, |
| 104 |
service_uuids=switchbot_adv.service_uuids, |
| 105 |
source=SOURCE_LOCAL, |
| 106 |
device=switchbot_device, |
| 107 |
advertisement=switchbot_adv, |
| 108 |
connectable=False, |
| 109 |
time=now, |
| 110 |
tx_power=1, |
| 111 |
) |
| 112 |
|
| 113 |
assert service_info.service_uuids == ["cba20d00-224d-11e6-9fb8-0002a5d5c51b"] |
| 114 |
assert service_info.name == "wohand" |
| 115 |
assert service_info.source == SOURCE_LOCAL |
| 116 |
assert service_info.manufacturer is None |
| 117 |
assert service_info.manufacturer_id is None |
| 118 |
assert service_info.time == now |
| 119 |
assert service_info.connectable is False |
| 120 |
|
| 121 |
safe_as_dict = service_info.as_dict() |
| 122 |
assert safe_as_dict == { |
| 123 |
"address": "44:44:33:11:23:45", |
| 124 |
"advertisement": switchbot_adv, |
| 125 |
"device": switchbot_device, |
| 126 |
"connectable": False, |
| 127 |
"raw": None, |
| 128 |
"manufacturer_data": {}, |
| 129 |
"name": "wohand", |
| 130 |
"rssi": -127, |
| 131 |
"service_data": {}, |
| 132 |
"service_uuids": ["cba20d00-224d-11e6-9fb8-0002a5d5c51b"], |
| 133 |
"source": "local", |
| 134 |
"time": now, |
| 135 |
"tx_power": 1, |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
def test_from_device_and_advertisement_data(): |
| 140 |
""" |
| 141 |
Test creating a BluetoothServiceInfoBleak. |
| 142 |
|
| 143 |
From a BLEDevice and AdvertisementData. |
| 144 |
""" |
| 145 |
switchbot_device = generate_ble_device("44:44:33:11:23:45", "wohand", {}) |
| 146 |
switchbot_adv = generate_advertisement_data( |
| 147 |
local_name="wohand", service_uuids=["cba20d00-224d-11e6-9fb8-0002a5d5c51b"] |
| 148 |
) |
| 149 |
now_monotonic = time.monotonic() |
| 150 |
service_info = BluetoothServiceInfoBleak.from_device_and_advertisement_data( |
| 151 |
switchbot_device, switchbot_adv, SOURCE_LOCAL, now_monotonic, True |
| 152 |
) |
| 153 |
|
| 154 |
assert service_info.service_uuids == ["cba20d00-224d-11e6-9fb8-0002a5d5c51b"] |
| 155 |
assert service_info.name == "wohand" |
| 156 |
assert service_info.source == SOURCE_LOCAL |
| 157 |
assert service_info.manufacturer is None |
| 158 |
assert service_info.manufacturer_id is None |
| 159 |
|
| 160 |
safe_as_dict = service_info.as_dict() |
| 161 |
assert safe_as_dict == { |
| 162 |
"address": "44:44:33:11:23:45", |
| 163 |
"advertisement": switchbot_adv, |
| 164 |
"device": switchbot_device, |
| 165 |
"connectable": True, |
| 166 |
"manufacturer_data": {}, |
| 167 |
"name": "wohand", |
| 168 |
"raw": None, |
| 169 |
"rssi": -127, |
| 170 |
"service_data": {}, |
| 171 |
"service_uuids": ["cba20d00-224d-11e6-9fb8-0002a5d5c51b"], |
| 172 |
"source": "local", |
| 173 |
"time": now_monotonic, |
| 174 |
"tx_power": -127, |
| 175 |
} |
| 176 |
assert str(service_info) == ( |
| 177 |
"<BluetoothServiceInfoBleak name=wohand address=44:44:33:11:23:45 rssi=-127 " |
| 178 |
"manufacturer_data={} service_data={} " |
| 179 |
"service_uuids=['cba20d00-224d-11e6-9fb8-0002a5d5c51b'] source=local " |
| 180 |
f"connectable=True time={now_monotonic} tx_power=-127 raw=None>" |
| 181 |
) |
| 182 |
|
| 183 |
|
| 184 |
def test_pyobjc_compat(): |
| 185 |
# pyobjc-style snake_case names intentionally mirror the runtime |
| 186 |
# types we receive from CoreBluetooth so the coercion paths are |
| 187 |
# exercised with realistic class identities. |
| 188 |
class pyobjc_str(str): # noqa: N801 |
| 189 |
__slots__ = () |
| 190 |
|
| 191 |
class pyobjc_int(int): # noqa: N801 |
| 192 |
__slots__ = () |
| 193 |
|
| 194 |
name = pyobjc_str("wohand") |
| 195 |
address = pyobjc_str("44:44:33:11:23:45") |
| 196 |
rssi = pyobjc_int(-127) |
| 197 |
|
| 198 |
assert name == "wohand" |
| 199 |
assert address == "44:44:33:11:23:45" |
| 200 |
assert rssi == -127 |
| 201 |
|
| 202 |
switchbot_device = generate_ble_device(address, name, {}) |
| 203 |
switchbot_adv = generate_advertisement_data( |
| 204 |
local_name=name, service_uuids=["cba20d00-224d-11e6-9fb8-0002a5d5c51b"] |
| 205 |
) |
| 206 |
now = time.monotonic() |
| 207 |
service_info = BluetoothServiceInfoBleak( |
| 208 |
name=str(name), |
| 209 |
address=str(address), |
| 210 |
rssi=rssi, |
| 211 |
manufacturer_data=switchbot_adv.manufacturer_data, |
| 212 |
service_data=switchbot_adv.service_data, |
| 213 |
service_uuids=switchbot_adv.service_uuids, |
| 214 |
source=SOURCE_LOCAL, |
| 215 |
device=switchbot_device, |
| 216 |
advertisement=switchbot_adv, |
| 217 |
connectable=False, |
| 218 |
time=now, |
| 219 |
tx_power=1, |
| 220 |
) |
| 221 |
|
| 222 |
assert service_info.service_uuids == ["cba20d00-224d-11e6-9fb8-0002a5d5c51b"] |
| 223 |
assert service_info.name == "wohand" |
| 224 |
assert service_info.source == SOURCE_LOCAL |
| 225 |
assert service_info.manufacturer is None |
| 226 |
assert service_info.manufacturer_id is None |
| 227 |
assert service_info.time == now |
| 228 |
assert service_info.connectable is False |
| 229 |
|
| 230 |
safe_as_dict = service_info.as_dict() |
| 231 |
assert safe_as_dict == { |
| 232 |
"address": "44:44:33:11:23:45", |
| 233 |
"advertisement": switchbot_adv, |
| 234 |
"device": switchbot_device, |
| 235 |
"connectable": False, |
| 236 |
"manufacturer_data": {}, |
| 237 |
"name": "wohand", |
| 238 |
"raw": None, |
| 239 |
"rssi": -127, |
| 240 |
"service_data": {}, |
| 241 |
"service_uuids": ["cba20d00-224d-11e6-9fb8-0002a5d5c51b"], |
| 242 |
"source": "local", |
| 243 |
"time": now, |
| 244 |
"tx_power": 1, |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
def test_as_connectable(): |
| 249 |
switchbot_device = generate_ble_device("44:44:33:11:23:45", "wohand", {}) |
| 250 |
switchbot_adv = generate_advertisement_data( |
| 251 |
local_name="wohand", service_uuids=["cba20d00-224d-11e6-9fb8-0002a5d5c51b"] |
| 252 |
) |
| 253 |
now = time.monotonic() |
| 254 |
service_info = BluetoothServiceInfoBleak( |
| 255 |
name="wohand", |
| 256 |
address="44:44:33:11:23:45", |
| 257 |
rssi=-127, |
| 258 |
manufacturer_data=switchbot_adv.manufacturer_data, |
| 259 |
service_data=switchbot_adv.service_data, |
| 260 |
service_uuids=switchbot_adv.service_uuids, |
| 261 |
source=SOURCE_LOCAL, |
| 262 |
device=switchbot_device, |
| 263 |
advertisement=switchbot_adv, |
| 264 |
connectable=False, |
| 265 |
time=now, |
| 266 |
tx_power=1, |
| 267 |
raw=b"\x00\x00\x00\x00\x00\x00", |
| 268 |
) |
| 269 |
connectable_service_info = service_info._as_connectable() |
| 270 |
assert connectable_service_info.connectable is True |
| 271 |
assert service_info.connectable is False |
| 272 |
assert connectable_service_info is not service_info |
| 273 |
assert service_info.name == connectable_service_info.name |
| 274 |
assert service_info.address == connectable_service_info.address |
| 275 |
assert service_info.rssi == connectable_service_info.rssi |
| 276 |
assert service_info.manufacturer_data == connectable_service_info.manufacturer_data |
| 277 |
assert service_info.service_data == connectable_service_info.service_data |
| 278 |
assert service_info.service_uuids == connectable_service_info.service_uuids |
| 279 |
assert service_info.source == connectable_service_info.source |
| 280 |
assert service_info.device == connectable_service_info.device |
| 281 |
assert service_info.advertisement == connectable_service_info.advertisement |
| 282 |
assert service_info.time == connectable_service_info.time |
| 283 |
assert service_info.tx_power == connectable_service_info.tx_power |
| 284 |
assert service_info.raw == connectable_service_info.raw |