File: test_capabilities.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 (191 lines) | stat: -rw-r--r-- 6,197 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
"""Tests for device capabilities functionality."""

from typing import Any

import pytest
from aiohttp import ClientSession
from aioresponses import aioresponses

from yalexs.api_async import ApiAsync
from yalexs.api_common import API_GET_CAPABILITIES_URL, ApiCommon
from yalexs.capabilities import CapabilitiesResponse
from yalexs.const import DEFAULT_BRAND
from yalexs.lock import LockDetail

ACCESS_TOKEN = "test-token"


@pytest.mark.asyncio
async def test_async_get_device_capabilities() -> None:
    """Test fetching device capabilities from the API."""
    capabilities_response: CapabilitiesResponse = {
        "lock": {
            "concurrentBLE": 2,
            "batteryType": "AA",
            "doorSense": True,
            "hasMagnetometer": False,
            "hasIntegratedWiFi": False,
            "scheduledSmartAlerts": True,
            "standalone": False,
            "bluetooth": True,
            "slotRange": None,
            "integratedKeypad": True,
            "entryCodeSlots": True,
            "pinSlotMax": 100,
            "pinSlotMin": 1,
            "supportsRFID": True,
            "supportsRFIDLegacy": False,
            "supportsRFIDCredential": True,
            "supportsRFIDOnlyAccess": True,
            "supportsRFIDWithCode": False,
            "supportsSecureMode": False,
            "supportsSecureModeCodeDisable": False,
            "supportsSecureModeMobileControl": False,
            "supportsFingerprintCredential": True,
            "supportsDeliveryMode": False,
            "supportsSchedulePerUser": True,
            "supportsFingerprintOnlyAccess": True,
            "batteryLifeMS": 21513600000,
            "supportedPartners": [],
            "unlatch": True,
        }
    }

    serial_number = "TEST123"

    with aioresponses() as mock:
        mock.get(
            ApiCommon(DEFAULT_BRAND).get_brand_url(API_GET_CAPABILITIES_URL)
            + f"?serialNumber={serial_number}&topLevelHost=true",
            payload=capabilities_response,
        )

        async with ClientSession() as session:
            api = ApiAsync(session)
            capabilities = await api.async_get_lock_capabilities(
                ACCESS_TOKEN, serial_number
            )

            assert capabilities == capabilities_response
            assert capabilities["lock"]["unlatch"] is True


def test_lock_detail_unlatch_supported_with_capabilities() -> None:
    """Test that LockDetail uses capabilities for unlatch_supported when available."""
    lock_data: dict[str, Any] = {
        "LockID": "test-lock-id",
        "LockName": "Test Lock",
        "HouseID": "test-house",
        "SerialNumber": "ABC123",
        "currentFirmwareVersion": "1.0.0",
        "Type": 5,  # Type that doesn't normally support unlatch
        "battery": 0.85,
        "LockStatus": {"status": "locked", "doorState": "closed"},
    }

    # Create lock detail without capabilities
    lock_detail = LockDetail(lock_data)

    # Should be False based on Type
    assert lock_detail.unlatch_supported is False

    # Set capabilities that indicate unlatch is supported
    capabilities: CapabilitiesResponse = {
        "lock": {
            "unlatch": True,
            "doorSense": True,
            "batteryType": "AA",
        }
    }
    lock_detail.set_capabilities(capabilities)

    # Now should be True based on capabilities
    assert lock_detail.unlatch_supported is True


def test_lock_detail_unlatch_supported_fallback_to_type() -> None:
    """Test that LockDetail falls back to Type-based check when no capabilities."""
    lock_data: dict[str, Any] = {
        "LockID": "test-lock-id",
        "LockName": "Test Lock",
        "HouseID": "test-house",
        "SerialNumber": "ABC123",
        "currentFirmwareVersion": "1.0.0",
        "Type": 17,  # Type 17 supports unlatch
        "battery": 0.85,
        "LockStatus": {"status": "locked", "doorState": "closed"},
    }

    # Create lock detail without capabilities
    lock_detail = LockDetail(lock_data)

    # Should be True based on Type 17
    assert lock_detail.unlatch_supported is True


def test_lock_detail_unlatch_supported_capabilities_override() -> None:
    """Test that capabilities override Type-based unlatch support."""
    lock_data: dict[str, Any] = {
        "LockID": "test-lock-id",
        "LockName": "Test Lock",
        "HouseID": "test-house",
        "SerialNumber": "ABC123",
        "currentFirmwareVersion": "1.0.0",
        "Type": 17,  # Type 17 normally supports unlatch
        "battery": 0.85,
        "LockStatus": {"status": "locked", "doorState": "closed"},
    }

    # Create lock detail
    lock_detail = LockDetail(lock_data)

    # Should be True based on Type 17
    assert lock_detail.unlatch_supported is True

    # Set capabilities that indicate unlatch is NOT supported
    capabilities: CapabilitiesResponse = {
        "lock": {
            "unlatch": False,  # Override: not supported
            "doorSense": True,
            "batteryType": "AA",
        }
    }
    lock_detail.set_capabilities(capabilities)

    # Now should be False based on capabilities override
    assert lock_detail.unlatch_supported is False


def test_lock_detail_set_capabilities() -> None:
    """Test setting capabilities on a lock detail."""
    lock_data: dict[str, Any] = {
        "LockID": "test-lock-id",
        "LockName": "Test Lock",
        "HouseID": "test-house",
        "SerialNumber": "ABC123",
        "currentFirmwareVersion": "1.0.0",
        "Type": 5,
        "battery": 0.85,
        "LockStatus": {"status": "locked", "doorState": "closed"},
    }

    lock_detail = LockDetail(lock_data)

    # Initially no capabilities
    assert lock_detail._capabilities is None

    # Set capabilities
    capabilities: CapabilitiesResponse = {
        "lock": {
            "unlatch": True,
            "doorSense": True,
            "batteryType": "AA",
            "pinSlotMax": 100,
            "pinSlotMin": 1,
        }
    }
    lock_detail.set_capabilities(capabilities)

    # Verify capabilities are stored
    assert lock_detail._capabilities == capabilities
    assert lock_detail._capabilities["lock"]["unlatch"] is True