File: test_blerpc.py

package info (click to toggle)
python-aioshelly 13.17.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 732 kB
  • sloc: python: 6,867; makefile: 7; sh: 3
file content (642 lines) | stat: -rw-r--r-- 19,725 bytes parent folder | download
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
"""Tests for rpc_device.blerpc module."""

from __future__ import annotations

import asyncio
from typing import Any
from unittest.mock import AsyncMock, MagicMock, Mock, patch

import pytest
from bleak.backends.device import BLEDevice
from bleak.exc import BleakError

from aioshelly.exceptions import (
    BleCharacteristicNotFoundError,
    BleConnectionError,
    DeviceConnectionError,
    DeviceConnectionTimeoutError,
    RpcCallError,
)
from aioshelly.json import json_bytes
from aioshelly.rpc_device import blerpc
from aioshelly.rpc_device.blerpc import (
    DATA_CHARACTERISTIC_UUID,
    RPC_SERVICE_UUID,
    RX_POLL_MAX_ATTEMPTS,
    BleRPC,
)


@pytest.fixture
def ble_device() -> BLEDevice:
    """Create mock BLE device."""
    device = MagicMock(spec=BLEDevice)
    device.address = "AA:BB:CC:DD:EE:FF"
    device.name = "ShellyPlus1-Test"
    return device


@pytest.fixture
def mock_ble_client() -> MagicMock:
    """Create mock BLE client."""
    client = AsyncMock()
    client.is_connected = True

    # Mock services with characteristics
    service = Mock()
    characteristic = Mock()
    characteristic.uuid = DATA_CHARACTERISTIC_UUID

    services = Mock()
    services.get_service.return_value = service
    services.get_characteristic.return_value = characteristic
    client.services = services

    return client


@pytest.fixture
def mock_establish_connection(mock_ble_client: MagicMock) -> Any:
    """Patch establish_connection to return mock client."""
    with patch(
        "aioshelly.rpc_device.blerpc.establish_connection",
        return_value=mock_ble_client,
    ) as mock_conn:
        yield mock_conn


@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_establish_connection")
async def test_blerpc_connect(ble_device: BLEDevice) -> None:
    """Test BLE RPC connection."""
    ble_rpc = BleRPC(ble_device)
    await ble_rpc.connect()
    assert ble_rpc.connected is True


@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_establish_connection")
async def test_blerpc_connect_already_connected(ble_device: BLEDevice) -> None:
    """Test BLE RPC connect when already connected."""
    ble_rpc = BleRPC(ble_device)
    await ble_rpc.connect()

    with pytest.raises(RuntimeError, match="Already connected"):
        await ble_rpc.connect()


@pytest.mark.asyncio
async def test_blerpc_connect_failure(
    ble_device: BLEDevice, mock_establish_connection: MagicMock
) -> None:
    """Test BLE RPC connection failure."""
    ble_rpc = BleRPC(ble_device)
    mock_establish_connection.side_effect = BleakError("Connection failed")

    with pytest.raises(BleConnectionError, match="Failed to connect"):
        await ble_rpc.connect()


@pytest.mark.asyncio
async def test_blerpc_connect_missing_characteristic(
    ble_device: BLEDevice, mock_establish_connection: MagicMock
) -> None:
    """Test BLE RPC connection with missing characteristic."""
    ble_rpc = BleRPC(ble_device)

    client = AsyncMock()
    client.is_connected = True

    # Mock services but missing characteristic
    service = Mock()
    services = Mock()
    services.get_service.return_value = service
    services.get_characteristic.return_value = None  # Missing characteristic
    client.services = services
    client.clear_cache = AsyncMock()
    client.disconnect = AsyncMock()

    mock_establish_connection.return_value = client

    with pytest.raises(BleCharacteristicNotFoundError):
        await ble_rpc.connect()

    # Should have tried twice (with cache clear)
    assert client.clear_cache.call_count == 1
    assert client.disconnect.call_count == 2


@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_establish_connection")
async def test_blerpc_disconnect(ble_device: BLEDevice) -> None:
    """Test BLE RPC disconnection."""
    ble_rpc = BleRPC(ble_device)
    await ble_rpc.connect()
    assert ble_rpc.connected is True

    await ble_rpc.disconnect()
    assert ble_rpc.connected is False


@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_establish_connection")
async def test_blerpc_call(ble_device: BLEDevice, mock_ble_client: MagicMock) -> None:
    """Test BLE RPC call."""
    # Create mock RPC response
    mock_response: dict[str, Any] = {
        "id": 1,
        "result": {"name": "Test Device", "model": "Test"},
    }
    ble_rpc = BleRPC(ble_device)

    # Mock GATT operations
    response_data = json_bytes(mock_response)
    mock_ble_client.write_gatt_char = AsyncMock()
    mock_ble_client.read_gatt_char = AsyncMock()

    # Mock RX control (frame length)
    frame_length = len(response_data)
    mock_ble_client.read_gatt_char.side_effect = [
        frame_length.to_bytes(4, "big"),  # RX control returns length
        response_data,  # Data characteristic returns response
    ]

    await ble_rpc.connect()
    result = await ble_rpc.call("Shelly.GetDeviceInfo")

    assert result == mock_response["result"]


@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_establish_connection")
async def test_blerpc_call_with_params(
    ble_device: BLEDevice, mock_ble_client: MagicMock
) -> None:
    """Test BLE RPC call with parameters."""
    # Create mock RPC response
    mock_response: dict[str, Any] = {
        "id": 1,
        "result": {"wifi": {"enable": True}, "sys": {"device": {"name": "Test"}}},
    }
    ble_rpc = BleRPC(ble_device)

    # Mock GATT operations
    response_data = json_bytes(mock_response)
    mock_ble_client.write_gatt_char = AsyncMock()
    mock_ble_client.read_gatt_char = AsyncMock()

    # Mock RX control (frame length) and data
    frame_length = len(response_data)
    mock_ble_client.read_gatt_char.side_effect = [
        frame_length.to_bytes(4, "big"),
        response_data,
    ]

    await ble_rpc.connect()
    result = await ble_rpc.call("Shelly.GetConfig", {"id": 0})

    assert result == mock_response["result"]


@pytest.mark.asyncio
async def test_blerpc_call_not_connected(ble_device: BLEDevice) -> None:
    """Test BLE RPC call when not connected."""
    ble_rpc = BleRPC(ble_device)

    with pytest.raises(DeviceConnectionError, match="Not connected"):
        await ble_rpc.call("Shelly.GetDeviceInfo")


@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_establish_connection")
async def test_blerpc_call_timeout(
    ble_device: BLEDevice, mock_ble_client: MagicMock
) -> None:
    """Test BLE RPC call timeout."""
    ble_rpc = BleRPC(ble_device)

    # Mock GATT operations that never return
    async def slow_read(*args: Any, **kwargs: Any) -> bytes:  # noqa: ARG001
        await asyncio.sleep(1)  # Sleep longer than timeout
        return b""

    mock_ble_client.write_gatt_char = AsyncMock()
    mock_ble_client.read_gatt_char = AsyncMock(side_effect=slow_read)

    await ble_rpc.connect()

    with pytest.raises(DeviceConnectionTimeoutError, match="timed out"):
        await ble_rpc.call("Shelly.GetDeviceInfo", timeout=0.01)


@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_establish_connection")
async def test_blerpc_call_error_response(
    ble_device: BLEDevice, mock_ble_client: MagicMock
) -> None:
    """Test BLE RPC call with error response."""
    ble_rpc = BleRPC(ble_device)

    # Mock error response
    error_response: dict[str, Any] = {
        "id": 1,
        "error": {"code": 404, "message": "Not found"},
    }
    response_data = json_bytes(error_response)

    mock_ble_client.write_gatt_char = AsyncMock()
    mock_ble_client.read_gatt_char = AsyncMock()

    frame_length = len(response_data)
    mock_ble_client.read_gatt_char.side_effect = [
        frame_length.to_bytes(4, "big"),
        response_data,
    ]

    await ble_rpc.connect()

    with pytest.raises(RpcCallError, match="Not found"):
        await ble_rpc.call("Shelly.GetDeviceInfo")


@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_establish_connection")
async def test_blerpc_call_chunked_response(
    ble_device: BLEDevice, mock_ble_client: MagicMock
) -> None:
    """Test BLE RPC call with chunked response (>254 bytes)."""
    # Create large mock response (> 254 bytes to trigger chunking)
    large_result = {"data": "x" * 300, "name": "Test Device"}
    mock_response: dict[str, Any] = {
        "id": 1,
        "result": large_result,
    }
    ble_rpc = BleRPC(ble_device)

    # Mock GATT operations
    response_data = json_bytes(mock_response)
    frame_length = len(response_data)

    # Split response into chunks (simulate BLE characteristic size limit)
    chunk_size = 254
    chunks = [
        response_data[i : i + chunk_size]
        for i in range(0, len(response_data), chunk_size)
    ]

    mock_ble_client.write_gatt_char = AsyncMock()
    mock_ble_client.read_gatt_char = AsyncMock()

    # Mock RX control returns frame length, then each chunk
    mock_ble_client.read_gatt_char.side_effect = [
        frame_length.to_bytes(4, "big"),
        *chunks,
    ]

    await ble_rpc.connect()
    result = await ble_rpc.call("Shelly.GetDeviceInfo")

    assert result == large_result


@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_establish_connection")
async def test_blerpc_call_incomplete_data(
    ble_device: BLEDevice, mock_ble_client: MagicMock
) -> None:
    """Test BLE RPC call with incomplete data."""
    ble_rpc = BleRPC(ble_device)

    mock_ble_client.write_gatt_char = AsyncMock()
    mock_ble_client.read_gatt_char = AsyncMock()

    # Mock RX control says 1000 bytes, but only return 100 bytes then empty
    mock_ble_client.read_gatt_char.side_effect = [
        (1000).to_bytes(4, "big"),  # Says 1000 bytes available
        b"x" * 100,  # Only 100 bytes returned
        b"",  # Empty chunk (no more data)
    ]

    await ble_rpc.connect()

    with pytest.raises(DeviceConnectionError, match="Incomplete data"):
        await ble_rpc.call("Shelly.GetDeviceInfo")


@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_establish_connection")
async def test_blerpc_call_invalid_json(
    ble_device: BLEDevice, mock_ble_client: MagicMock
) -> None:
    """Test BLE RPC call with invalid JSON response."""
    ble_rpc = BleRPC(ble_device)

    # Mock GATT operations with invalid JSON
    invalid_json = b"not valid json"
    mock_ble_client.write_gatt_char = AsyncMock()
    mock_ble_client.read_gatt_char = AsyncMock()

    frame_length = len(invalid_json)
    mock_ble_client.read_gatt_char.side_effect = [
        frame_length.to_bytes(4, "big"),
        invalid_json,
    ]

    await ble_rpc.connect()

    with pytest.raises(DeviceConnectionError, match="Invalid JSON"):
        await ble_rpc.call("Shelly.GetDeviceInfo")


@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_establish_connection")
async def test_blerpc_call_id_mismatch(
    ble_device: BLEDevice, mock_ble_client: MagicMock
) -> None:
    """Test BLE RPC call with response ID mismatch."""
    ble_rpc = BleRPC(ble_device)

    # Mock response with wrong ID
    wrong_id_response: dict[str, Any] = {
        "id": 999,  # Wrong ID
        "result": {"test": "data"},
    }
    response_data = json_bytes(wrong_id_response)

    mock_ble_client.write_gatt_char = AsyncMock()
    mock_ble_client.read_gatt_char = AsyncMock()

    frame_length = len(response_data)
    mock_ble_client.read_gatt_char.side_effect = [
        frame_length.to_bytes(4, "big"),
        response_data,
    ]

    await ble_rpc.connect()

    with pytest.raises(RpcCallError, match="Response ID mismatch"):
        await ble_rpc.call("Shelly.GetDeviceInfo")


@pytest.mark.asyncio
async def test_blerpc_connect_missing_rpc_service(
    ble_device: BLEDevice, mock_establish_connection: MagicMock
) -> None:
    """Test BLE RPC connection with missing RPC service."""
    ble_rpc = BleRPC(ble_device)

    client = AsyncMock()
    client.is_connected = True

    # Mock services but missing RPC service
    services = Mock()
    services.get_service.return_value = None  # Missing RPC service
    client.services = services
    client.clear_cache = AsyncMock()
    client.disconnect = AsyncMock()

    mock_establish_connection.return_value = client

    with pytest.raises(
        BleCharacteristicNotFoundError,
        match=f"RPC service {RPC_SERVICE_UUID} not found",
    ):
        await ble_rpc.connect()

    # Should have tried twice (with cache clear)
    assert client.clear_cache.call_count == 1
    assert client.disconnect.call_count == 2


@pytest.mark.asyncio
async def test_blerpc_connect_service_discovery_error(
    ble_device: BLEDevice, mock_establish_connection: MagicMock
) -> None:
    """Test BLE RPC connection with service discovery error."""
    ble_rpc = BleRPC(ble_device)

    client = AsyncMock()
    client.is_connected = True

    # Mock services that raises BleakError during access
    client.services = Mock()
    client.services.get_service.side_effect = BleakError("Service discovery failed")
    client.disconnect = AsyncMock()

    mock_establish_connection.return_value = client

    with pytest.raises(BleConnectionError, match="Failed to verify RPC service"):
        await ble_rpc.connect()

    # Should have disconnected
    assert client.disconnect.call_count == 1


@pytest.mark.asyncio
async def test_blerpc_connect_os_error(
    ble_device: BLEDevice, mock_establish_connection: MagicMock
) -> None:
    """Test BLE RPC connection with OS error during service discovery."""
    ble_rpc = BleRPC(ble_device)

    client = AsyncMock()
    client.is_connected = True

    # Mock services that raises OSError during access
    client.services = Mock()
    client.services.get_service.side_effect = OSError("OS error")
    client.disconnect = AsyncMock()

    mock_establish_connection.return_value = client

    with pytest.raises(BleConnectionError, match="Failed to verify RPC service"):
        await ble_rpc.connect()

    # Should have disconnected
    assert client.disconnect.call_count == 1


@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_establish_connection")
async def test_blerpc_disconnect_callback(
    ble_device: BLEDevice, mock_ble_client: MagicMock
) -> None:
    """Test BLE RPC disconnect callback."""
    ble_rpc = BleRPC(ble_device)

    await ble_rpc.connect()
    assert ble_rpc.connected is True

    # Simulate disconnect callback being called
    ble_rpc._on_disconnect(mock_ble_client)

    assert ble_rpc.connected is False


@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_establish_connection")
async def test_blerpc_call_invalid_response(
    ble_device: BLEDevice, mock_ble_client: MagicMock
) -> None:
    """Test BLE RPC call with response that has neither error nor result."""
    ble_rpc = BleRPC(ble_device)

    # Mock response with neither error nor result
    invalid_response: dict[str, Any] = {
        "id": 1,
        # No error or result field
    }
    response_data = json_bytes(invalid_response)

    mock_ble_client.write_gatt_char = AsyncMock()
    mock_ble_client.read_gatt_char = AsyncMock()

    frame_length = len(response_data)
    mock_ble_client.read_gatt_char.side_effect = [
        frame_length.to_bytes(4, "big"),
        response_data,
    ]

    await ble_rpc.connect()

    with pytest.raises(RpcCallError, match="Invalid response"):
        await ble_rpc.call("Shelly.GetDeviceInfo")


@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_establish_connection")
async def test_blerpc_call_zero_frame_length_timeout(
    ble_device: BLEDevice, mock_ble_client: MagicMock
) -> None:
    """Test BLE RPC call with zero frame length that times out after polling."""
    ble_rpc = BleRPC(ble_device)

    mock_ble_client.write_gatt_char = AsyncMock()
    mock_ble_client.read_gatt_char = AsyncMock()

    # Mock RX control always returns frame length of 0 (never ready)
    mock_ble_client.read_gatt_char.side_effect = [
        (0).to_bytes(4, "big")
    ] * RX_POLL_MAX_ATTEMPTS

    await ble_rpc.connect()

    # Patch RX_POLL_INTERVAL to 0 to avoid slow test
    with (
        patch.object(blerpc, "RX_POLL_INTERVAL", 0),
        pytest.raises(
            DeviceConnectionError,
            match=(
                f"No response data available after {RX_POLL_MAX_ATTEMPTS} poll attempts"
            ),
        ),
    ):
        await ble_rpc.call("Shelly.GetDeviceInfo")


@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_establish_connection")
async def test_blerpc_call_zero_frame_length_then_success(
    ble_device: BLEDevice, mock_ble_client: MagicMock
) -> None:
    """Test BLE RPC call with zero frame length that eventually succeeds."""
    ble_rpc = BleRPC(ble_device)

    response_data = b'{"id":1,"result":{"name":"Test Device"}}'

    mock_ble_client.write_gatt_char = AsyncMock()
    mock_ble_client.read_gatt_char = AsyncMock()

    # Mock RX control returns 0 a few times, then actual frame length
    mock_ble_client.read_gatt_char.side_effect = [
        (0).to_bytes(4, "big"),  # First poll: not ready
        (0).to_bytes(4, "big"),  # Second poll: not ready
        len(response_data).to_bytes(4, "big"),  # Third poll: ready
        response_data,  # Data read
    ]

    await ble_rpc.connect()

    # Patch RX_POLL_INTERVAL to 0 to avoid slow test
    with patch.object(blerpc, "RX_POLL_INTERVAL", 0):
        result = await ble_rpc.call("Shelly.GetDeviceInfo")

    assert result == {"name": "Test Device"}


@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_establish_connection")
async def test_blerpc_call_invalid_frame_length_data(
    ble_device: BLEDevice, mock_ble_client: MagicMock
) -> None:
    """Test BLE RPC call with invalid frame length data."""
    ble_rpc = BleRPC(ble_device)

    mock_ble_client.write_gatt_char = AsyncMock()
    mock_ble_client.read_gatt_char = AsyncMock()

    # Mock RX control returns insufficient bytes
    mock_ble_client.read_gatt_char.side_effect = [
        b"\x00\x00",  # Only 2 bytes instead of 4
    ]

    await ble_rpc.connect()

    with pytest.raises(DeviceConnectionError, match="Invalid frame length data"):
        await ble_rpc.call("Shelly.GetDeviceInfo")


@pytest.mark.asyncio
async def test_blerpc_disconnect_when_not_connected(ble_device: BLEDevice) -> None:
    """Test disconnect when client is None."""
    ble_rpc = BleRPC(ble_device)

    # Should not raise an error
    await ble_rpc.disconnect()
    assert ble_rpc.connected is False


@pytest.mark.asyncio
async def test_blerpc_verify_rpc_service_no_client(ble_device: BLEDevice) -> None:
    """Test _verify_rpc_service when client is None."""
    ble_rpc = BleRPC(ble_device)

    # _client is None, should raise RuntimeError
    with pytest.raises(RuntimeError, match="Client not initialized"):
        await ble_rpc._verify_rpc_service()


@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_establish_connection")
async def test_blerpc_call_bleak_error(
    ble_device: BLEDevice, mock_ble_client: MagicMock
) -> None:
    """Test BLE RPC call with BleakError during transmission."""
    ble_rpc = BleRPC(ble_device)

    # Mock write_gatt_char to raise BleakError
    mock_ble_client.write_gatt_char = AsyncMock(
        side_effect=BleakError("Bluetooth error")
    )

    await ble_rpc.connect()

    with pytest.raises(DeviceConnectionError, match="BLE RPC call failed"):
        await ble_rpc.call("Shelly.GetDeviceInfo")


@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_establish_connection")
async def test_blerpc_call_os_error(
    ble_device: BLEDevice, mock_ble_client: MagicMock
) -> None:
    """Test BLE RPC call with OSError during transmission."""
    ble_rpc = BleRPC(ble_device)

    # Mock write_gatt_char to raise OSError
    mock_ble_client.write_gatt_char = AsyncMock(side_effect=OSError("IO error"))

    await ble_rpc.connect()

    with pytest.raises(DeviceConnectionError, match="BLE RPC call failed"):
        await ble_rpc.call("Shelly.GetDeviceInfo")