File: test_data.py

package info (click to toggle)
python-yalexs 9.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,120 kB
  • sloc: python: 7,916; makefile: 3; sh: 2
file content (1172 lines) | stat: -rw-r--r-- 39,862 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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
"""Test the manager data module."""

import logging
from datetime import datetime
from typing import Any
from unittest.mock import AsyncMock, Mock, patch

import pytest
from aiohttp import ClientError, ClientResponseError

from yalexs.activity import SOURCE_PUBNUB, SOURCE_WEBSOCKET
from yalexs.capabilities import CapabilitiesResponse
from yalexs.const import Brand
from yalexs.exceptions import YaleApiError
from yalexs.lock import LockDetail, LockOperation
from yalexs.manager.data import YaleXSData


class MockYaleXSData(YaleXSData):
    """Mock implementation of YaleXSData with mocked abstract method."""

    def async_offline_key_discovered(self, detail) -> None:
        """Mock implementation of abstract method."""


class TestPushStateTracking:
    """Test push state tracking functionality."""

    def setup_method(self):
        """Set up test fixtures."""

        # Create a simple object with just the method and state dict we need
        class TestData:
            def __init__(self):
                self._last_push_state = {}

            # Bind the actual method from YaleXSData
            _is_unchanged_push_state = YaleXSData._is_unchanged_push_state

        self.data = TestData()
        self.device_id = "test_device_id"

    def test_pubnub_initial_status_update_sets_baseline(self):
        """Test that the first PubNub status update sets the baseline state."""
        # First status update should set baseline
        message1 = {
            "status": "locked",
            "doorState": "closed",
        }

        # Mock activities that are all status updates
        mock_activity1 = Mock()
        mock_activity1.is_status = True

        # First call - no previous state
        result1 = self.data._is_unchanged_push_state(
            self.device_id, message1, SOURCE_PUBNUB, [mock_activity1]
        )
        # Should not skip (need to process) and should track the state
        assert result1 is False

        # Verify state was tracked
        state_key = f"{self.device_id}:{SOURCE_PUBNUB}"
        assert state_key in self.data._last_push_state
        assert self.data._last_push_state[state_key] == {
            "lock": "locked",
            "door": "closed",
        }

    def test_pubnub_duplicate_status_update_skipped(self):
        """Test that duplicate PubNub status updates are skipped."""
        message = {
            "status": "locked",
            "doorState": "closed",
        }

        # Set initial state
        state_key = f"{self.device_id}:{SOURCE_PUBNUB}"
        self.data._last_push_state[state_key] = {
            "lock": "locked",
            "door": "closed",
        }

        # Mock activities that are all status updates
        mock_activity = Mock()
        mock_activity.is_status = True

        # Same state status update - should be skipped
        result = self.data._is_unchanged_push_state(
            self.device_id, message, SOURCE_PUBNUB, [mock_activity]
        )
        assert result is True  # Should skip

        # State should not have changed
        assert self.data._last_push_state[state_key] == {
            "lock": "locked",
            "door": "closed",
        }

    def test_pubnub_changed_status_update_not_tracked(self):
        """Test that changed PubNub status updates are processed but not tracked."""
        # Set initial state
        state_key = f"{self.device_id}:{SOURCE_PUBNUB}"
        self.data._last_push_state[state_key] = {
            "lock": "locked",
            "door": "closed",
        }

        # Different state in status update
        message = {
            "status": "unlocked",
            "doorState": "open",
        }

        # Mock activities that are all status updates
        mock_activity = Mock()
        mock_activity.is_status = True

        result = self.data._is_unchanged_push_state(
            self.device_id, message, SOURCE_PUBNUB, [mock_activity]
        )
        assert result is False  # Should process (state changed)

        # State should NOT have been updated (status updates don't track)
        assert self.data._last_push_state[state_key] == {
            "lock": "locked",
            "door": "closed",
        }

    def test_pubnub_real_action_updates_tracking(self):
        """Test that real PubNub actions update state tracking."""
        # Set initial state
        state_key = f"{self.device_id}:{SOURCE_PUBNUB}"
        self.data._last_push_state[state_key] = {
            "lock": "locked",
            "door": "closed",
        }

        # Real unlock action
        message = {
            "status": "unlocked",
            "doorState": "closed",
            "info": {"action": "unlock"},
            "callingUserID": "user123",
        }

        # Mock activity that is NOT a status update
        mock_activity = Mock()
        mock_activity.is_status = False

        result = self.data._is_unchanged_push_state(
            self.device_id, message, SOURCE_PUBNUB, [mock_activity]
        )
        assert result is False  # Should process

        # State SHOULD have been updated (real action)
        assert self.data._last_push_state[state_key] == {
            "lock": "unlocked",
            "door": "closed",
        }

    def test_status_update_between_real_actions_doesnt_interfere(self):
        """Test that status updates between real actions don't interfere with detection."""
        state_key = f"{self.device_id}:{SOURCE_PUBNUB}"

        # Step 1: Real unlock action
        message1 = {
            "status": "unlocked",
            "doorState": "closed",
        }
        mock_activity1 = Mock()
        mock_activity1.is_status = False

        result1 = self.data._is_unchanged_push_state(
            self.device_id, message1, SOURCE_PUBNUB, [mock_activity1]
        )
        assert result1 is False
        assert self.data._last_push_state[state_key] == {
            "lock": "unlocked",
            "door": "closed",
        }

        # Step 2: Status update with same state
        message2 = {
            "status": "unlocked",
            "doorState": "closed",
        }
        mock_activity2 = Mock()
        mock_activity2.is_status = True

        result2 = self.data._is_unchanged_push_state(
            self.device_id, message2, SOURCE_PUBNUB, [mock_activity2]
        )
        assert result2 is True  # Should skip (unchanged)
        assert self.data._last_push_state[state_key] == {
            "lock": "unlocked",
            "door": "closed",
        }  # State unchanged

        # Step 3: Real lock action
        message3 = {
            "status": "locked",
            "doorState": "closed",
        }
        mock_activity3 = Mock()
        mock_activity3.is_status = False

        result3 = self.data._is_unchanged_push_state(
            self.device_id, message3, SOURCE_PUBNUB, [mock_activity3]
        )
        assert result3 is False  # Should process (real action with changed state)
        assert self.data._last_push_state[state_key] == {
            "lock": "locked",
            "door": "closed",
        }  # State updated

    def test_websocket_always_tracks_state(self):
        """Test that WebSocket messages always track state changes."""
        state_key = f"{self.device_id}:{SOURCE_WEBSOCKET}"

        # First WebSocket message
        message1 = {
            "lockAction": "locked",
            "doorState": "closed",
        }

        result1 = self.data._is_unchanged_push_state(
            self.device_id, message1, SOURCE_WEBSOCKET, []
        )
        assert result1 is False
        assert self.data._last_push_state[state_key] == {
            "lock": "locked",
            "door": "closed",
        }

        # Same state - should skip
        result2 = self.data._is_unchanged_push_state(
            self.device_id, message1, SOURCE_WEBSOCKET, []
        )
        assert result2 is True

        # Different state - should process and track
        message2 = {
            "lockAction": "unlocked",
            "doorState": "open",
        }
        result3 = self.data._is_unchanged_push_state(
            self.device_id, message2, SOURCE_WEBSOCKET, []
        )
        assert result3 is False
        assert self.data._last_push_state[state_key] == {
            "lock": "unlocked",
            "door": "open",
        }

    def test_separate_tracking_per_source(self):
        """Test that state is tracked separately for each source."""
        pubnub_key = f"{self.device_id}:{SOURCE_PUBNUB}"
        websocket_key = f"{self.device_id}:{SOURCE_WEBSOCKET}"

        # Set PubNub state
        pubnub_message = {
            "status": "locked",
            "doorState": "closed",
        }
        mock_activity = Mock()
        mock_activity.is_status = False

        self.data._is_unchanged_push_state(
            self.device_id, pubnub_message, SOURCE_PUBNUB, [mock_activity]
        )

        # Set different WebSocket state
        websocket_message = {
            "lockAction": "unlocked",
            "doorState": "open",
        }
        self.data._is_unchanged_push_state(
            self.device_id, websocket_message, SOURCE_WEBSOCKET, []
        )

        # Verify states are tracked separately
        assert self.data._last_push_state[pubnub_key] == {
            "lock": "locked",
            "door": "closed",
        }
        assert self.data._last_push_state[websocket_key] == {
            "lock": "unlocked",
            "door": "open",
        }

    def test_unchanged_state_still_processes_newer_activities(self, caplog):
        """Test that unchanged state messages still process if they have newer activities."""

        # Create a more complete mock data object with required methods
        class TestDataWithMethods:
            def __init__(self):
                self._last_push_state = {}
                self._device_detail_by_id = {}
                self.activity_stream = Mock()
                self.activity_stream.async_process_newer_device_activities = Mock(
                    return_value=True
                )
                self.activity_stream.async_schedule_house_id_refresh = Mock()

            # Bind the actual methods from YaleXSData
            _is_unchanged_push_state = YaleXSData._is_unchanged_push_state
            _async_handle_push_message = YaleXSData._async_handle_push_message

            def get_device_detail(self, device_id):
                return self._device_detail_by_id.get(device_id)

            def async_signal_device_id_update(self, device_id):
                pass

        data = TestDataWithMethods()
        device_id = "test_device"

        # Create a mock device
        mock_device = Mock()
        mock_device.device_id = device_id
        mock_device.house_id = "test_house"
        data._device_detail_by_id[device_id] = mock_device

        # Set initial state
        state_key = f"{device_id}:{SOURCE_PUBNUB}"
        data._last_push_state[state_key] = {
            "lock": "locked",
            "door": "closed",
        }

        # Message with same state but newer timestamp (would have newer activities)
        message = {
            "status": "locked",
            "doorState": "closed",
            "callingUserID": "manuallock",
        }

        # Mock activity that is not a status update
        mock_activity = Mock()
        mock_activity.is_status = False
        mock_activity.action = "lock"

        with (
            patch(
                "yalexs.manager.data.activities_from_pubnub_message"
            ) as mock_activities_func,
            caplog.at_level(logging.DEBUG),
        ):
            mock_activities_func.return_value = [mock_activity]

            # Call the push message handler
            data._async_handle_push_message(
                device_id, datetime.now(), message, SOURCE_PUBNUB
            )

            # Verify activities were processed even though state unchanged
            assert data.activity_stream.async_process_newer_device_activities.called
            assert data.activity_stream.async_process_newer_device_activities.call_args[
                0
            ][0] == [mock_activity]

            # Verify we logged that state was unchanged
            assert any(
                "Skipping unchanged" in record.message for record in caplog.records
            )

            # Verify house refresh was NOT scheduled (because unchanged)
            assert not data.activity_stream.async_schedule_house_id_refresh.called


@pytest.mark.asyncio
async def test_fetch_lock_capabilities() -> None:
    """Test that lock capabilities are fetched and set correctly."""
    # Create mock gateway and API
    mock_gateway = Mock()
    mock_gateway.async_get_access_token = AsyncMock(return_value="test-token")

    mock_api = Mock()
    mock_gateway.api = mock_api
    mock_gateway.api.brand = Brand.YALE_HOME  # Set brand for capability fetching

    # Create MockYaleXSData instance
    data = MockYaleXSData(mock_gateway)

    # Create mock lock details
    lock_detail_1 = Mock(spec=LockDetail)
    lock_detail_1.device_name = "Front Door"
    lock_detail_1.set_capabilities = Mock()

    lock_detail_2 = Mock(spec=LockDetail)
    lock_detail_2.device_name = "Back Door"
    lock_detail_2.set_capabilities = Mock()

    # Set up device details
    # Note: lock_id is the serial number for locks
    data._device_detail_by_id = {
        "SERIAL1": lock_detail_1,
        "SERIAL2": lock_detail_2,
        "doorbell1": Mock(),  # Not a lock, should be skipped
    }
    data._locks_by_id = {
        "SERIAL1": Mock(),
        "SERIAL2": Mock(),
    }

    # Mock API responses
    capabilities_1: CapabilitiesResponse = {
        "lock": {
            "unlatch": True,
            "doorSense": True,
            "batteryType": "AA",
        }
    }
    capabilities_2: CapabilitiesResponse = {
        "lock": {
            "unlatch": False,
            "doorSense": False,
            "batteryType": "CR123",
        }
    }

    # Configure mock API
    async def mock_get_capabilities(token: str, serial: str) -> CapabilitiesResponse:
        if serial == "SERIAL1":
            return capabilities_1
        if serial == "SERIAL2":
            return capabilities_2
        raise ValueError(f"Unknown serial: {serial}")

    mock_api.async_get_lock_capabilities = AsyncMock(side_effect=mock_get_capabilities)

    # Call the method
    await data._async_fetch_lock_capabilities()

    # Verify API was called with correct parameters
    assert mock_api.async_get_lock_capabilities.call_count == 2
    mock_api.async_get_lock_capabilities.assert_any_call("test-token", "SERIAL1")
    mock_api.async_get_lock_capabilities.assert_any_call("test-token", "SERIAL2")

    # Verify capabilities were set on lock details
    lock_detail_1.set_capabilities.assert_called_once_with(capabilities_1)
    lock_detail_2.set_capabilities.assert_called_once_with(capabilities_2)


@pytest.mark.asyncio
async def test_fetch_lock_capabilities_with_error(
    caplog: pytest.LogCaptureFixture,
) -> None:
    """Test that capability fetch errors are handled gracefully."""
    # Create mock gateway and API
    mock_gateway = Mock()
    mock_gateway.async_get_access_token = AsyncMock(return_value="test-token")

    mock_api = Mock()
    mock_gateway.api = mock_api
    mock_gateway.api.brand = Brand.YALE_HOME  # Set brand for capability fetching

    # Create MockYaleXSData instance
    data = MockYaleXSData(mock_gateway)

    # Create mock lock detail
    lock_detail = Mock(spec=LockDetail)
    lock_detail.device_name = "Front Door"
    lock_detail.set_capabilities = Mock()

    # Set up device details (lock_id is serial number)
    data._device_detail_by_id = {
        "SERIAL1": lock_detail,
    }
    data._locks_by_id = {
        "SERIAL1": Mock(),
    }

    # Mock API to raise an error

    mock_api.async_get_lock_capabilities = AsyncMock(
        side_effect=ClientError("API Error")
    )

    # Call the method with logging
    with caplog.at_level(logging.WARNING):
        await data._async_fetch_lock_capabilities()

    # Verify API was called
    mock_api.async_get_lock_capabilities.assert_called_once_with(
        "test-token", "SERIAL1"
    )

    # Verify capabilities were NOT set due to error
    lock_detail.set_capabilities.assert_not_called()

    # Verify error was logged
    assert "Failed to fetch capabilities for lock Front Door" in caplog.text
    assert "API Error" in caplog.text


@pytest.mark.asyncio
async def test_fetch_lock_capabilities_skips_non_locks() -> None:
    """Test that non-lock devices are skipped when fetching capabilities."""
    # Create mock gateway and API
    mock_gateway = Mock()
    mock_gateway.async_get_access_token = AsyncMock(return_value="test-token")

    mock_api = Mock()
    mock_gateway.api = mock_api
    mock_gateway.api.brand = Brand.YALE_HOME  # Set brand for capability fetching

    # Create MockYaleXSData instance
    data = MockYaleXSData(mock_gateway)

    # Create mock lock detail
    lock_detail = Mock(spec=LockDetail)
    lock_detail.device_name = "Front Door"
    lock_detail.set_capabilities = Mock()

    # Set up device details with mixed devices
    data._device_detail_by_id = {
        "SERIAL1": lock_detail,  # This is a lock
        "doorbell1": Mock(),  # This is not a lock
        "doorbell2": Mock(),  # This is not a lock
    }
    data._locks_by_id = {
        "SERIAL1": Mock(),  # Only this one is a lock
    }

    # Mock API response
    capabilities: CapabilitiesResponse = {
        "lock": {
            "unlatch": True,
            "doorSense": True,
            "batteryType": "AA",
        }
    }

    mock_api.async_get_lock_capabilities = AsyncMock(return_value=capabilities)

    # Call the method
    await data._async_fetch_lock_capabilities()

    # Verify API was called only once (for the lock, not the doorbells)
    mock_api.async_get_lock_capabilities.assert_called_once_with(
        "test-token", "SERIAL1"
    )

    # Verify capabilities were set only on the lock
    lock_detail.set_capabilities.assert_called_once_with(capabilities)


@pytest.mark.asyncio
async def test_fetch_lock_capabilities_sequential_execution() -> None:
    """Test that capabilities are fetched sequentially, not in parallel."""
    # Create mock gateway and API
    mock_gateway = Mock()
    mock_gateway.async_get_access_token = AsyncMock(return_value="test-token")

    mock_api = Mock()
    mock_gateway.api = mock_api
    mock_gateway.api.brand = Brand.YALE_HOME  # Set brand for capability fetching

    # Create MockYaleXSData instance
    data = MockYaleXSData(mock_gateway)

    # Create mock lock details
    lock_detail_1 = Mock(spec=LockDetail)
    lock_detail_1.device_name = "Front Door"
    lock_detail_1.set_capabilities = Mock()

    lock_detail_2 = Mock(spec=LockDetail)
    lock_detail_2.device_name = "Back Door"
    lock_detail_2.set_capabilities = Mock()

    lock_detail_3 = Mock(spec=LockDetail)
    lock_detail_3.device_name = "Side Door"
    lock_detail_3.set_capabilities = Mock()

    # Set up device details
    data._device_detail_by_id = {
        "SERIAL1": lock_detail_1,
        "SERIAL2": lock_detail_2,
        "SERIAL3": lock_detail_3,
    }
    data._locks_by_id = {
        "SERIAL1": Mock(),
        "SERIAL2": Mock(),
        "SERIAL3": Mock(),
    }

    # Track call order
    call_order: list[str] = []

    async def mock_get_capabilities(token: str, serial: str) -> CapabilitiesResponse:
        call_order.append(serial)
        return {"lock": {"unlatch": True}}

    mock_api.async_get_lock_capabilities = AsyncMock(side_effect=mock_get_capabilities)

    # Call the method
    await data._async_fetch_lock_capabilities()

    # Verify all were called in sequence
    assert call_order == ["SERIAL1", "SERIAL2", "SERIAL3"]
    assert mock_api.async_get_lock_capabilities.call_count == 3


@pytest.mark.asyncio
async def test_august_brand_does_not_fetch_capabilities():
    """Test that August brand does not fetch device capabilities."""
    # Create mock gateway with August brand
    mock_gateway = AsyncMock()
    mock_gateway.brand = Brand.AUGUST  # August brand
    mock_gateway.access_token = "test-token"
    mock_gateway.async_get_access_token = AsyncMock(return_value="test-token")

    mock_api = Mock()
    mock_gateway.api = mock_api
    mock_gateway.api.brand = Brand.AUGUST  # Set August brand for API

    # Create MockYaleXSData instance
    data = MockYaleXSData(mock_gateway)

    # Set up test locks
    lock1 = {
        "LockID": "ABC1",
        "LockName": "Lock 1",
        "HouseID": "house1",
        "SerialNumber": "SERIAL1",
        "Type": 5,
        "battery": 0.8,
        "currentFirmwareVersion": "1.0.0",
        "LockStatus": {"status": "locked"},
    }
    lock2 = {
        "LockID": "ABC2",
        "LockName": "Lock 2",
        "HouseID": "house1",
        "SerialNumber": "SERIAL2",
        "Type": 17,
        "battery": 0.9,
        "currentFirmwareVersion": "1.0.0",
        "LockStatus": {"status": "unlocked"},
    }

    data._lock_details = {
        "ABC1": LockDetail(lock1),
        "ABC2": LockDetail(lock2),
    }

    # Mock the capabilities fetch - should not be called
    mock_api.async_get_lock_capabilities = AsyncMock()

    # Call the method
    await data._async_fetch_lock_capabilities()

    # Verify the capabilities method was NOT called for August brand
    assert mock_api.async_get_lock_capabilities.call_count == 0


@pytest.mark.asyncio
async def test_fetch_lock_capabilities_handles_404_and_409_errors(
    caplog: pytest.LogCaptureFixture,
) -> None:
    """Test that 404 and 409 errors are handled gracefully when fetching capabilities."""
    # Create mock gateway and API
    mock_gateway = Mock()
    mock_gateway.async_get_access_token = AsyncMock(return_value="test-token")

    mock_api = Mock()
    mock_gateway.api = mock_api
    mock_gateway.api.brand = Brand.YALE_HOME  # Set brand for capability fetching

    # Create MockYaleXSData instance
    data = MockYaleXSData(mock_gateway)

    # Create mock lock details
    lock_detail_404 = Mock(spec=LockDetail)
    lock_detail_404.device_name = "Lock 404"
    lock_detail_404.set_capabilities = Mock()

    lock_detail_409 = Mock(spec=LockDetail)
    lock_detail_409.device_name = "Lock 409"
    lock_detail_409.set_capabilities = Mock()

    # Set up device details
    data._device_detail_by_id = {
        "SERIAL404": lock_detail_404,
        "SERIAL409": lock_detail_409,
    }
    data._locks_by_id = {
        "SERIAL404": Mock(),
        "SERIAL409": Mock(),
    }

    # Mock API to raise 404 and 409 errors
    async def mock_get_capabilities(token: str, serial: str) -> None:
        if serial == "SERIAL404":
            error = YaleApiError(
                "The operation failed with error code 404: Device info not found.",
                ClientResponseError(
                    request_info=Mock(),
                    history=(),
                    status=404,
                    message="Device info not found",
                ),
            )
            raise error
        if serial == "SERIAL409":
            error = YaleApiError(
                "The operation failed with error code 409: Cannot infer deviceType from serial number.",
                ClientResponseError(
                    request_info=Mock(),
                    history=(),
                    status=409,
                    message="Cannot infer deviceType from serial number.",
                ),
            )
            raise error

    mock_api.async_get_lock_capabilities = AsyncMock(side_effect=mock_get_capabilities)

    # Call the method with debug logging
    with caplog.at_level(logging.DEBUG):
        await data._async_fetch_lock_capabilities()

    # Verify API was called for both locks
    assert mock_api.async_get_lock_capabilities.call_count == 2

    # Verify capabilities were NOT set due to errors
    lock_detail_404.set_capabilities.assert_not_called()
    lock_detail_409.set_capabilities.assert_not_called()

    # Verify debug messages were logged (not warnings)
    assert "Cannot fetch capabilities for lock Lock 404 (HTTP 404)" in caplog.text
    assert "Cannot fetch capabilities for lock Lock 409 (HTTP 409)" in caplog.text
    # Verify no warning logs for these expected errors
    warning_records = [r for r in caplog.records if r.levelno >= logging.WARNING]
    assert len(warning_records) == 0


@pytest.mark.asyncio
async def test_fetch_lock_capabilities_handles_other_errors_with_warning(
    caplog: pytest.LogCaptureFixture,
) -> None:
    """Test that non-404/409 errors are logged as warnings when fetching capabilities."""
    # Create mock gateway and API
    mock_gateway = Mock()
    mock_gateway.async_get_access_token = AsyncMock(return_value="test-token")

    mock_api = Mock()
    mock_gateway.api = mock_api
    mock_gateway.api.brand = Brand.YALE_HOME  # Set brand for capability fetching

    # Create MockYaleXSData instance
    data = MockYaleXSData(mock_gateway)

    # Create mock lock details
    lock_detail_401 = Mock(spec=LockDetail)
    lock_detail_401.device_name = "Lock 401"
    lock_detail_401.set_capabilities = Mock()

    lock_detail_500 = Mock(spec=LockDetail)
    lock_detail_500.device_name = "Lock 500"
    lock_detail_500.set_capabilities = Mock()

    # Set up device details
    data._device_detail_by_id = {
        "SERIAL401": lock_detail_401,
        "SERIAL500": lock_detail_500,
    }
    data._locks_by_id = {
        "SERIAL401": Mock(),
        "SERIAL500": Mock(),
    }

    # Mock API to raise 401 and 500 errors
    async def mock_get_capabilities(token: str, serial: str) -> None:
        if serial == "SERIAL401":
            error = YaleApiError(
                "The operation failed with error code 401: Unauthorized.",
                ClientResponseError(
                    request_info=Mock(),
                    history=(),
                    status=401,
                    message="Unauthorized",
                ),
            )
            raise error
        if serial == "SERIAL500":
            error = YaleApiError(
                "The operation failed with error code 500: Internal Server Error.",
                ClientResponseError(
                    request_info=Mock(),
                    history=(),
                    status=500,
                    message="Internal Server Error",
                ),
            )
            raise error

    mock_api.async_get_lock_capabilities = AsyncMock(side_effect=mock_get_capabilities)

    # Call the method with warning logging
    with caplog.at_level(logging.WARNING):
        await data._async_fetch_lock_capabilities()

    # Verify API was called for both locks
    assert mock_api.async_get_lock_capabilities.call_count == 2

    # Verify capabilities were NOT set due to errors
    lock_detail_401.set_capabilities.assert_not_called()
    lock_detail_500.set_capabilities.assert_not_called()

    # Verify warning messages were logged for non-404/409 errors
    assert "Failed to fetch capabilities for lock Lock 401 (HTTP 401)" in caplog.text
    assert "Failed to fetch capabilities for lock Lock 500 (HTTP 500)" in caplog.text

    # Verify these are logged as warnings, not debug
    warning_records = [r for r in caplog.records if r.levelno >= logging.WARNING]
    assert len(warning_records) == 2


@pytest.mark.asyncio
async def test_fetch_lock_capabilities_handles_network_errors(
    caplog: pytest.LogCaptureFixture,
) -> None:
    """Test that network errors like ClientError and TimeoutError are handled gracefully."""
    # Create mock gateway and API
    mock_gateway = Mock()
    mock_gateway.async_get_access_token = AsyncMock(return_value="test-token")

    mock_api = Mock()
    mock_gateway.api = mock_api
    mock_gateway.api.brand = Brand.YALE_HOME  # Set brand for capability fetching

    # Create MockYaleXSData instance
    data = MockYaleXSData(mock_gateway)

    # Create mock lock details
    lock_detail_timeout = Mock(spec=LockDetail)
    lock_detail_timeout.device_name = "Lock Timeout"
    lock_detail_timeout.set_capabilities = Mock()

    lock_detail_network = Mock(spec=LockDetail)
    lock_detail_network.device_name = "Lock Network"
    lock_detail_network.set_capabilities = Mock()

    # Set up device details
    data._device_detail_by_id = {
        "SERIAL_TIMEOUT": lock_detail_timeout,
        "SERIAL_NETWORK": lock_detail_network,
    }
    data._locks_by_id = {
        "SERIAL_TIMEOUT": Mock(),
        "SERIAL_NETWORK": Mock(),
    }

    # Mock API to raise TimeoutError and ClientError
    async def mock_get_capabilities(token: str, serial: str) -> None:
        if serial == "SERIAL_TIMEOUT":
            raise TimeoutError("Request timed out")
        if serial == "SERIAL_NETWORK":
            raise ClientError("Network error")

    mock_api.async_get_lock_capabilities = AsyncMock(side_effect=mock_get_capabilities)

    # Call the method with warning logging
    with caplog.at_level(logging.WARNING):
        await data._async_fetch_lock_capabilities()

    # Verify API was called for both locks
    assert mock_api.async_get_lock_capabilities.call_count == 2

    # Verify capabilities were NOT set due to errors
    lock_detail_timeout.set_capabilities.assert_not_called()
    lock_detail_network.set_capabilities.assert_not_called()

    # Verify warning messages were logged for network errors
    assert (
        "Failed to fetch capabilities for lock Lock Timeout: Request timed out"
        in caplog.text
    )
    assert (
        "Failed to fetch capabilities for lock Lock Network: Network error"
        in caplog.text
    )

    # Verify these are logged as warnings
    warning_records = [r for r in caplog.records if r.levelno >= logging.WARNING]
    assert len(warning_records) == 2


@pytest.mark.asyncio
async def test_async_operate_lock_wait_mode() -> None:
    """Test async_operate_lock LOCK operation when waiting for response."""
    mock_gateway = Mock()
    mock_gateway.async_get_access_token = AsyncMock(return_value="test_token")
    mock_api = Mock()

    data = MockYaleXSData(mock_gateway, mock_api)

    # Mock the individual lock operation methods
    data.async_lock = AsyncMock(return_value=["lock_activity"])
    data.async_lock_async = AsyncMock(return_value="lock_request_id")

    device_id = "test_device"

    # Mock device detail without unlatch support
    mock_detail = Mock(spec=LockDetail)
    mock_detail.unlatch_supported = False
    data.get_device_detail = Mock(return_value=mock_detail)

    result = await data.async_operate_lock(
        device_id, LockOperation.LOCK, push_updates_connected=False
    )

    assert result == ["lock_activity"]
    data.async_lock.assert_called_once_with(device_id)
    data.async_lock_async.assert_not_called()


@pytest.mark.asyncio
async def test_async_operate_lock_push_mode() -> None:
    """Test async_operate_lock LOCK operation with push updates (no wait)."""
    mock_gateway = Mock()
    mock_gateway.async_get_access_token = AsyncMock(return_value="test_token")
    mock_api = Mock()

    data = MockYaleXSData(mock_gateway, mock_api)

    # Mock the individual lock operation methods
    data.async_lock = AsyncMock(return_value=["lock_activity"])
    data.async_lock_async = AsyncMock(return_value="lock_request_id")

    device_id = "test_device"

    # Mock device detail without unlatch support
    mock_detail = Mock(spec=LockDetail)
    mock_detail.unlatch_supported = False
    data.get_device_detail = Mock(return_value=mock_detail)

    result = await data.async_operate_lock(
        device_id, LockOperation.LOCK, push_updates_connected=True, hyper_bridge=True
    )

    assert result == []  # Returns empty list when not waiting
    data.async_lock.assert_not_called()
    data.async_lock_async.assert_called_once_with(device_id, True)


@pytest.mark.asyncio
async def test_async_operate_unlock_no_unlatch_support() -> None:
    """Test async_operate_lock UNLOCK operation when device doesn't support unlatch."""
    mock_gateway = Mock()
    mock_gateway.async_get_access_token = AsyncMock(return_value="test_token")
    mock_api = Mock()

    data = MockYaleXSData(mock_gateway, mock_api)

    # Mock the individual lock operation methods
    data.async_unlock = AsyncMock(return_value=["unlock_activity"])
    data.async_unlatch = AsyncMock(return_value=["unlatch_activity"])

    device_id = "test_device"

    # Mock device detail without unlatch support
    mock_detail = Mock(spec=LockDetail)
    mock_detail.unlatch_supported = False
    data.get_device_detail = Mock(return_value=mock_detail)

    result = await data.async_operate_lock(
        device_id, LockOperation.UNLOCK, push_updates_connected=False
    )

    assert result == ["unlock_activity"]
    data.async_unlock.assert_called_once_with(device_id)
    data.async_unlatch.assert_not_called()


@pytest.mark.asyncio
async def test_async_operate_unlock_with_unlatch_support() -> None:
    """Test async_operate_lock UNLOCK operation when device supports unlatch (should call unlatch)."""
    mock_gateway = Mock()
    mock_gateway.async_get_access_token = AsyncMock(return_value="test_token")
    mock_api = Mock()

    data = MockYaleXSData(mock_gateway, mock_api)

    # Mock the individual lock operation methods
    data.async_unlock = AsyncMock(return_value=["unlock_activity"])
    data.async_unlatch = AsyncMock(return_value=["unlatch_activity"])

    device_id = "test_device"

    # Mock device detail WITH unlatch support
    mock_detail = Mock(spec=LockDetail)
    mock_detail.unlatch_supported = True
    data.get_device_detail = Mock(return_value=mock_detail)

    result = await data.async_operate_lock(
        device_id, LockOperation.UNLOCK, push_updates_connected=False
    )

    # When unlatch is supported, UNLOCK should call unlatch!
    assert result == ["unlatch_activity"]
    data.async_unlatch.assert_called_once_with(device_id)
    data.async_unlock.assert_not_called()


@pytest.mark.asyncio
async def test_async_operate_open_no_unlatch_support() -> None:
    """Test async_operate_lock OPEN operation when device doesn't support unlatch."""
    mock_gateway = Mock()
    mock_gateway.async_get_access_token = AsyncMock(return_value="test_token")
    mock_api = Mock()

    data = MockYaleXSData(mock_gateway, mock_api)

    # Mock the individual lock operation methods
    data.async_unlock = AsyncMock(return_value=["unlock_activity"])
    data.async_unlatch = AsyncMock(return_value=["unlatch_activity"])

    device_id = "test_device"

    # Mock device detail without unlatch support
    mock_detail = Mock(spec=LockDetail)
    mock_detail.unlatch_supported = False
    data.get_device_detail = Mock(return_value=mock_detail)

    result = await data.async_operate_lock(
        device_id, LockOperation.OPEN, push_updates_connected=False
    )

    assert result == ["unlatch_activity"]
    data.async_unlatch.assert_called_once_with(device_id)
    data.async_unlock.assert_not_called()


@pytest.mark.asyncio
async def test_async_operate_open_with_unlatch_support() -> None:
    """Test async_operate_lock OPEN operation when device supports unlatch (should call unlock)."""
    mock_gateway = Mock()
    mock_gateway.async_get_access_token = AsyncMock(return_value="test_token")
    mock_api = Mock()

    data = MockYaleXSData(mock_gateway, mock_api)

    # Mock the individual lock operation methods
    data.async_unlock = AsyncMock(return_value=["unlock_activity"])
    data.async_unlatch = AsyncMock(return_value=["unlatch_activity"])

    device_id = "test_device"

    # Mock device detail WITH unlatch support
    mock_detail = Mock(spec=LockDetail)
    mock_detail.unlatch_supported = True
    data.get_device_detail = Mock(return_value=mock_detail)

    result = await data.async_operate_lock(
        device_id, LockOperation.OPEN, push_updates_connected=False
    )

    # When unlatch is supported, OPEN should call unlock!
    assert result == ["unlock_activity"]
    data.async_unlock.assert_called_once_with(device_id)
    data.async_unlatch.assert_not_called()


@pytest.mark.asyncio
async def test_async_operate_lock_all_operations_with_push() -> None:
    """Test async_operate_lock all operations with push updates enabled."""
    mock_gateway = Mock()
    mock_gateway.async_get_access_token = AsyncMock(return_value="test_token")
    mock_api = Mock()

    data = MockYaleXSData(mock_gateway, mock_api)

    # Mock all async operation methods
    data.async_lock_async = AsyncMock(return_value="lock_request_id")
    data.async_unlock_async = AsyncMock(return_value="unlock_request_id")
    data.async_unlatch_async = AsyncMock(return_value="unlatch_request_id")

    device_id = "test_device"

    # Mock device detail with unlatch support
    mock_detail = Mock(spec=LockDetail)
    mock_detail.unlatch_supported = True
    data.get_device_detail = Mock(return_value=mock_detail)

    # Test LOCK operation
    result = await data.async_operate_lock(
        device_id, LockOperation.LOCK, push_updates_connected=True
    )
    assert result == []
    data.async_lock_async.assert_called_once_with(device_id, True)

    # Reset mocks
    data.async_lock_async.reset_mock()
    data.async_unlock_async.reset_mock()
    data.async_unlatch_async.reset_mock()

    # Test UNLOCK (should call unlatch_async when unlatch supported)
    result = await data.async_operate_lock(
        device_id, LockOperation.UNLOCK, push_updates_connected=True
    )
    assert result == []
    data.async_unlatch_async.assert_called_once_with(device_id, True)

    # Reset mocks
    data.async_lock_async.reset_mock()
    data.async_unlock_async.reset_mock()
    data.async_unlatch_async.reset_mock()

    # Test OPEN (should call unlock_async when unlatch supported)
    result = await data.async_operate_lock(
        device_id, LockOperation.OPEN, push_updates_connected=True
    )
    assert result == []
    data.async_unlock_async.assert_called_once_with(device_id, True)


@pytest.mark.asyncio
async def test_async_operate_lock_invalid_operation() -> None:
    """Test async_operate_lock with invalid operation raises ValueError."""
    mock_gateway = Mock()
    mock_gateway.async_get_access_token = AsyncMock(return_value="test_token")
    mock_api = Mock()

    data = MockYaleXSData(mock_gateway, mock_api)

    device_id = "test_device"

    # Mock device detail
    mock_detail = Mock(spec=LockDetail)
    mock_detail.unlatch_supported = False
    data.get_device_detail = Mock(return_value=mock_detail)

    # Create an invalid operation
    invalid_op: Any = Mock()
    invalid_op.value = "invalid"

    with pytest.raises(ValueError, match="Invalid operation"):
        await data.async_operate_lock(
            device_id, invalid_op, push_updates_connected=False
        )


@pytest.mark.asyncio
async def test_async_operate_lock_no_device_detail() -> None:
    """Test async_operate_lock when get_device_detail returns None."""
    mock_gateway = Mock()
    mock_gateway.async_get_access_token = AsyncMock(return_value="test_token")
    mock_api = Mock()

    data = MockYaleXSData(mock_gateway, mock_api)

    # Mock the individual lock operation methods
    data.async_unlock = AsyncMock(return_value=["unlock_activity"])
    data.async_unlatch = AsyncMock(return_value=["unlatch_activity"])

    device_id = "test_device"

    # Mock get_device_detail to return None
    data.get_device_detail = Mock(return_value=None)

    result = await data.async_operate_lock(
        device_id, LockOperation.UNLOCK, push_updates_connected=False
    )

    # Should use normal mapping (unlock -> unlock)
    assert result == ["unlock_activity"]
    data.async_unlock.assert_called_once_with(device_id)
    data.async_unlatch.assert_not_called()