1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
|
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from bleak.backends.device import BLEDevice
from switchbot import SwitchBotAdvertisement, SwitchbotEncryptedDevice, SwitchbotModel
from switchbot.devices import relay_switch
from switchbot.devices.device import _merge_data as merge_data
from .test_adv_parser import generate_ble_device
common_params = [
(b";\x00\x00\x00", SwitchbotModel.RELAY_SWITCH_1),
(b"<\x00\x00\x00", SwitchbotModel.RELAY_SWITCH_1PM),
(b">\x00\x00\x00", SwitchbotModel.GARAGE_DOOR_OPENER),
(b"?\x00\x00\x00", SwitchbotModel.PLUG_MINI_EU),
]
@pytest.fixture
def common_parametrize_2pm():
"""Provide common test data."""
return {
"rawAdvData": b"\x00\x00\x00\x00\x00\x00",
"model": SwitchbotModel.RELAY_SWITCH_2PM,
}
def create_device_for_command_testing(
rawAdvData: bytes, model: str, init_data: dict | None = None
):
"""Create a device for command testing."""
ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
if model == SwitchbotModel.GARAGE_DOOR_OPENER:
device_class = relay_switch.SwitchbotGarageDoorOpener
elif model == SwitchbotModel.RELAY_SWITCH_2PM:
device_class = relay_switch.SwitchbotRelaySwitch2PM
else:
device_class = relay_switch.SwitchbotRelaySwitch
device = device_class(
ble_device, "ff", "ffffffffffffffffffffffffffffffff", model=model
)
device.update_from_advertisement(
make_advertisement_data(ble_device, rawAdvData, model, init_data)
)
device._send_command = AsyncMock()
device._check_command_result = MagicMock()
device.update = AsyncMock()
return device
def make_advertisement_data(
ble_device: BLEDevice, rawAdvData: bytes, model: str, init_data: dict | None = None
):
"""Set advertisement data with defaults."""
if init_data is None:
init_data = {}
if model == SwitchbotModel.RELAY_SWITCH_2PM:
return SwitchBotAdvertisement(
address="aa:bb:cc:dd:ee:ff",
data={
"rawAdvData": rawAdvData,
"data": {
1: {
"switchMode": True,
"sequence_number": 99,
"isOn": True,
},
2: {
"switchMode": True,
"sequence_number": 99,
"isOn": False,
},
}
| init_data,
"isEncrypted": False,
},
device=ble_device,
rssi=-80,
active=True,
)
if model == SwitchbotModel.GARAGE_DOOR_OPENER:
return SwitchBotAdvertisement(
address="aa:bb:cc:dd:ee:ff",
data={
"rawAdvData": rawAdvData,
"data": {
"switchMode": True,
"sequence_number": 96,
"isOn": True,
"door_open": False,
}
| init_data,
"isEncrypted": False,
},
device=ble_device,
rssi=-80,
active=True,
)
return SwitchBotAdvertisement(
address="aa:bb:cc:dd:ee:ff",
data={
"rawAdvData": rawAdvData,
"data": {
"switchMode": True,
"sequence_number": 96,
"isOn": True,
}
| init_data,
"isEncrypted": False,
},
device=ble_device,
rssi=-80,
active=True,
)
@pytest.mark.asyncio
@pytest.mark.parametrize(
"init_data",
[
{1: {"isOn": True}, 2: {"isOn": True}},
],
)
async def test_turn_on_2PM(common_parametrize_2pm, init_data):
"""Test turn on command."""
device = create_device_for_command_testing(
common_parametrize_2pm["rawAdvData"], common_parametrize_2pm["model"], init_data
)
await device.turn_on(1)
device._send_command.assert_called_with(
relay_switch.MULTI_CHANNEL_COMMANDS_TURN_ON[common_parametrize_2pm["model"]][1]
)
assert device.is_on(1) is True
await device.turn_on(2)
device._send_command.assert_called_with(
relay_switch.MULTI_CHANNEL_COMMANDS_TURN_ON[common_parametrize_2pm["model"]][2]
)
assert device.is_on(2) is True
@pytest.mark.asyncio
@pytest.mark.parametrize(
"init_data",
[
{1: {"isOn": False}, 2: {"isOn": False}},
],
)
async def test_turn_off_2PM(common_parametrize_2pm, init_data):
"""Test turn off command."""
device = create_device_for_command_testing(
common_parametrize_2pm["rawAdvData"], common_parametrize_2pm["model"], init_data
)
await device.turn_off(1)
device._send_command.assert_called_with(
relay_switch.MULTI_CHANNEL_COMMANDS_TURN_OFF[common_parametrize_2pm["model"]][1]
)
assert device.is_on(1) is False
await device.turn_off(2)
device._send_command.assert_called_with(
relay_switch.MULTI_CHANNEL_COMMANDS_TURN_OFF[common_parametrize_2pm["model"]][2]
)
assert device.is_on(2) is False
@pytest.mark.asyncio
async def test_turn_toggle_2PM(common_parametrize_2pm):
"""Test toggle command."""
device = create_device_for_command_testing(
common_parametrize_2pm["rawAdvData"], common_parametrize_2pm["model"]
)
await device.async_toggle(1)
device._send_command.assert_called_with(
relay_switch.MULTI_CHANNEL_COMMANDS_TOGGLE[common_parametrize_2pm["model"]][1]
)
assert device.is_on(1) is True
await device.async_toggle(2)
device._send_command.assert_called_with(
relay_switch.MULTI_CHANNEL_COMMANDS_TOGGLE[common_parametrize_2pm["model"]][2]
)
assert device.is_on(2) is False
@pytest.mark.asyncio
async def test_get_switch_mode_2PM(common_parametrize_2pm):
"""Test get switch mode."""
device = create_device_for_command_testing(
common_parametrize_2pm["rawAdvData"], common_parametrize_2pm["model"]
)
assert device.switch_mode(1) is True
assert device.switch_mode(2) is True
@pytest.mark.asyncio
@pytest.mark.parametrize(
("info_data", "result"),
[
(
{
"basic_info": b"\x01\x98A\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10",
"channel1_info": b"\x01\x00\x00\x00\x00\x00\x00\x02\x99\x00\xe9\x00\x03\x00\x00",
"channel2_info": b"\x01\x00\x055\x00'<\x02\x9f\x00\xe9\x01,\x00F",
},
[False, 0, 0, 0, 0, True, 0.02, 23, 0.3, 7.0],
),
(
{
"basic_info": b"\x01\x9e\x81\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10",
"channel1_info": b"\x01\x00\x00\x00\x00\x00\x00\x02\x99\x00\xe9\x00\x03\x00\x00",
"channel2_info": b"\x01\x00\x05\xbc\x00'<\x02\xb1\x00\xea\x01-\x00F",
},
[True, 0, 23, 0.1, 0.0, False, 0.02, 0, 0, 0],
),
],
)
async def test_get_basic_info_2PM(common_parametrize_2pm, info_data, result):
"""Test get_basic_info for 2PM devices."""
device = create_device_for_command_testing(
common_parametrize_2pm["rawAdvData"], common_parametrize_2pm["model"]
)
assert device.channel == 2
device.get_current_time_and_start_time = MagicMock(
return_value=("683074d6", "682fba80")
)
async def mock_get_basic_info(arg):
if arg == relay_switch.COMMAND_GET_BASIC_INFO:
return info_data["basic_info"]
if arg == relay_switch.COMMAND_GET_CHANNEL1_INFO.format("683074d6", "682fba80"):
return info_data["channel1_info"]
if arg == relay_switch.COMMAND_GET_CHANNEL2_INFO.format("683074d6", "682fba80"):
return info_data["channel2_info"]
return None
device._get_basic_info = AsyncMock(side_effect=mock_get_basic_info)
info = await device.get_basic_info()
assert info is not None
assert 1 in info
assert 2 in info
assert info[1]["isOn"] == result[0]
assert info[1]["energy"] == result[1]
assert info[1]["voltage"] == result[2]
assert info[1]["current"] == result[3]
assert info[1]["power"] == result[4]
assert info[2]["isOn"] == result[5]
assert info[2]["energy"] == result[6]
assert info[2]["voltage"] == result[7]
assert info[2]["current"] == result[8]
assert info[2]["power"] == result[9]
@pytest.mark.asyncio
@pytest.mark.parametrize(
"info_data",
[
{
"basic_info": None,
"channel1_info": b"\x01\x00\x00\x00\x00\x00\x00\x02\x99\x00\xe9\x00\x03\x00\x00",
"channel2_info": b"\x01\x00\x055\x00'<\x02\x9f\x00\xe9\x01,\x00F",
},
{
"basic_info": b"\x01\x98A\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10",
"channel1_info": None,
"channel2_info": b"\x01\x00\x055\x00'<\x02\x9f\x00\xe9\x01,\x00F",
},
{
"basic_info": b"\x01\x98A\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10",
"channel1_info": b"\x01\x00\x00\x00\x00\x00\x00\x02\x99\x00\xe9\x00\x03\x00\x00",
"channel2_info": None,
},
],
)
async def test_basic_info_exceptions_2PM(common_parametrize_2pm, info_data):
"""Test get_basic_info exceptions."""
device = create_device_for_command_testing(
common_parametrize_2pm["rawAdvData"], common_parametrize_2pm["model"]
)
device.get_current_time_and_start_time = MagicMock(
return_value=("683074d6", "682fba80")
)
async def mock_get_basic_info(arg):
if arg == relay_switch.COMMAND_GET_BASIC_INFO:
return info_data["basic_info"]
if arg == relay_switch.COMMAND_GET_CHANNEL1_INFO.format("683074d6", "682fba80"):
return info_data["channel1_info"]
if arg == relay_switch.COMMAND_GET_CHANNEL2_INFO.format("683074d6", "682fba80"):
return info_data["channel2_info"]
return None
device._get_basic_info = AsyncMock(side_effect=mock_get_basic_info)
info = await device.get_basic_info()
assert info is None
@pytest.mark.asyncio
async def test_get_parsed_data_2PM(common_parametrize_2pm):
"""Test get_parsed_data for 2PM devices."""
device = create_device_for_command_testing(
common_parametrize_2pm["rawAdvData"], common_parametrize_2pm["model"]
)
info = device.get_parsed_data(1)
assert info["isOn"] is True
info = device.get_parsed_data(2)
assert info["isOn"] is False
@pytest.mark.asyncio
@pytest.mark.parametrize(
("rawAdvData", "model"),
common_params,
)
async def test_turn_on(rawAdvData, model):
"""Test turn on command."""
device = create_device_for_command_testing(rawAdvData, model)
await device.turn_on()
device._send_command.assert_awaited_once_with(device._turn_on_command)
assert device.is_on() is True
@pytest.mark.asyncio
@pytest.mark.parametrize(
("rawAdvData", "model"),
common_params,
)
async def test_turn_off(rawAdvData, model):
"""Test turn off command."""
device = create_device_for_command_testing(rawAdvData, model, {"isOn": False})
await device.turn_off()
device._send_command.assert_awaited_once_with(device._turn_off_command)
assert device.is_on() is False
@pytest.mark.asyncio
@pytest.mark.parametrize(
("rawAdvData", "model"),
common_params,
)
async def test_toggle(rawAdvData, model):
"""Test toggle command."""
device = create_device_for_command_testing(rawAdvData, model)
await device.async_toggle()
device._send_command.assert_awaited_once_with(relay_switch.COMMAND_TOGGLE)
assert device.is_on() is True
@pytest.mark.asyncio
@pytest.mark.parametrize(
("rawAdvData", "model", "info_data"),
[
(
b">\x00\x00\x00",
SwitchbotModel.GARAGE_DOOR_OPENER,
{
"basic_info": b"\x01>\x80\x0c\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x10",
"channel1_info": b"\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
},
)
],
)
async def test_get_basic_info_garage_door_opener(rawAdvData, model, info_data):
"""Test get_basic_info for garage door opener."""
device = create_device_for_command_testing(rawAdvData, model)
device.get_current_time_and_start_time = MagicMock(
return_value=("683074d6", "682fba80")
)
async def mock_get_basic_info(arg):
if arg == relay_switch.COMMAND_GET_BASIC_INFO:
return info_data["basic_info"]
if arg == relay_switch.COMMAND_GET_CHANNEL1_INFO.format("683074d6", "682fba80"):
return info_data["channel1_info"]
return None
device._get_basic_info = AsyncMock(side_effect=mock_get_basic_info)
info = await device.get_basic_info()
assert info is not None
assert info["isOn"] is True
assert info["door_open"] is True
@pytest.mark.asyncio
@pytest.mark.parametrize(
"model",
[
SwitchbotModel.RELAY_SWITCH_1,
SwitchbotModel.RELAY_SWITCH_1PM,
SwitchbotModel.GARAGE_DOOR_OPENER,
SwitchbotModel.RELAY_SWITCH_2PM,
],
)
@patch.object(SwitchbotEncryptedDevice, "verify_encryption_key", new_callable=AsyncMock)
async def test_verify_encryption_key(mock_parent_verify, model):
ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
key_id = "ff"
encryption_key = "ffffffffffffffffffffffffffffffff"
mock_parent_verify.return_value = True
result = await relay_switch.SwitchbotRelaySwitch.verify_encryption_key(
device=ble_device,
key_id=key_id,
encryption_key=encryption_key,
model=model,
)
mock_parent_verify.assert_awaited_once_with(
ble_device,
key_id,
encryption_key,
model,
)
assert result is True
@pytest.mark.parametrize(
("old_data", "new_data", "expected_result"),
[
(
{"isOn": True, "sequence_number": 1},
{"isOn": False},
{"isOn": False, "sequence_number": 1},
),
(
{
1: {"current": 0, "voltage": 220, "power": 0},
2: {"current": 1, "voltage": 0, "power": 10},
},
{1: {"current": 1, "power": 10}, 2: {"current": 0, "voltage": 220}},
{
1: {"current": 1, "voltage": 220, "power": 10},
2: {"current": 0, "voltage": 220, "power": 10},
},
),
],
)
def test_merge_data(old_data, new_data, expected_result):
"""Test merging of data dictionaries."""
result = merge_data(old_data, new_data)
assert result == expected_result
@pytest.mark.asyncio
async def test_garage_door_opener_open():
"""Test open the garage door."""
device = create_device_for_command_testing(
b">\x00\x00\x00", SwitchbotModel.GARAGE_DOOR_OPENER
)
await device.open()
device._send_command.assert_awaited_once_with(device._open_command)
@pytest.mark.asyncio
async def test_garage_door_opener_close():
"""Test close the garage door."""
device = create_device_for_command_testing(
b">\x00\x00\x00", SwitchbotModel.GARAGE_DOOR_OPENER
)
await device.close()
device._send_command.assert_awaited_once_with(device._close_command)
@pytest.mark.parametrize(
"door_open",
[
True,
False,
],
)
@pytest.mark.asyncio
async def test_garage_door_opener_door_open(door_open):
"""Test get garage door state."""
device = create_device_for_command_testing(
b">\x00\x00\x00", SwitchbotModel.GARAGE_DOOR_OPENER, {"door_open": door_open}
)
assert device.door_open() is door_open
@pytest.mark.asyncio
async def test_press():
"""Test the press command for garage door opener."""
device = create_device_for_command_testing(
b">\x00\x00\x00", SwitchbotModel.GARAGE_DOOR_OPENER
)
await device.press()
device._send_command.assert_awaited_once_with(device._press_command)
|