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
|
"""Test lock utility functions."""
import copy
import pytest
from test.common import MockCommandProtocol
from zwave_js_server.const import SupervisionStatus
from zwave_js_server.const.command_class.lock import (
ATTR_CODE_SLOT,
ATTR_IN_USE,
ATTR_NAME,
ATTR_USERCODE,
LOCK_USERCODE_ID_PROPERTY,
LOCK_USERCODE_PROPERTY,
LOCK_USERCODE_STATUS_PROPERTY,
CodeSlotStatus,
DoorLockCCConfigurationSetOptions,
OperationType,
)
from zwave_js_server.exceptions import NotFoundError
from zwave_js_server.model.node import Node
from zwave_js_server.model.value import SupervisionResult
from zwave_js_server.util.lock import (
clear_usercode,
get_code_slots,
get_usercode,
get_usercode_from_node,
get_usercodes,
set_configuration,
set_usercode,
set_usercodes,
)
from .const import CODE_SLOTS
def test_get_code_slots(lock_schlage_be469):
"""Test get_code_slots utility function."""
node = lock_schlage_be469
assert get_code_slots(node) == [
{k: v for k, v in code_slot.items() if k != ATTR_USERCODE}
for code_slot in CODE_SLOTS
]
def test_get_usercode(lock_schlage_be469):
"""Test get_usercode utility function."""
node = lock_schlage_be469
# Test in use slot
slot = get_usercode(node, 1)
assert all(char == "*" for char in slot[ATTR_USERCODE])
# Test unused slot
assert get_usercode(node, 29)[ATTR_USERCODE] == ""
# Test unknown slot
assert get_usercode(node, 30)[ATTR_USERCODE] is None
# Test invalid slot
with pytest.raises(NotFoundError):
get_usercode(node, 100)
def test_get_usercodes(lock_schlage_be469):
"""Test get_usercodes utility function."""
node = lock_schlage_be469
assert get_usercodes(node) == CODE_SLOTS
async def test_set_usercode(lock_schlage_be469, mock_command, uuid4):
"""Test set_usercode utility function."""
node = lock_schlage_be469
ack_commands = mock_command(
{"command": "node.set_value", "nodeId": node.node_id},
{"result": {"status": 255}},
)
# Test valid code
await set_usercode(node, 1, "1234")
assert len(ack_commands) == 1
assert ack_commands[0] == {
"command": "node.set_value",
"nodeId": 20,
"messageId": uuid4,
"valueId": {
"commandClass": 99,
"endpoint": 0,
"property": "userCode",
"propertyKey": 1,
},
"value": "1234",
}
# Test invalid code slot
with pytest.raises(NotFoundError):
await set_usercode(node, 100, "1234")
# assert no new command calls
assert len(ack_commands) == 1
async def test_set_usercodes(
lock_schlage_be469: Node, mock_command: MockCommandProtocol, uuid4: str
) -> None:
"""Test set_usercodes utility function."""
node = lock_schlage_be469
ack_commands = mock_command(
{"command": "endpoint.invoke_cc_api", "endpoint": 0, "nodeId": node.node_id},
{"response": {"status": 255}},
)
assert await set_usercodes(node, {1: "1234"}) == SupervisionResult(
{"status": SupervisionStatus.SUCCESS}
)
assert len(ack_commands) == 1
assert ack_commands[0] == {
"command": "endpoint.invoke_cc_api",
"commandClass": 99,
"endpoint": 0,
"methodName": "setMany",
"nodeId": 20,
"messageId": uuid4,
"args": [
[
{
LOCK_USERCODE_STATUS_PROPERTY: CodeSlotStatus.ENABLED,
LOCK_USERCODE_ID_PROPERTY: 1,
LOCK_USERCODE_PROPERTY: "1234",
}
]
],
}
async def test_set_usercodes_invalid(
lock_schlage_be469: Node, mock_command: MockCommandProtocol
) -> None:
"""Test set_usercodes utility function with invalid response."""
node = lock_schlage_be469
mock_command(
{"command": "endpoint.invoke_cc_api", "endpoint": 0, "nodeId": node.node_id},
{"response": {}},
)
with pytest.raises(ValueError):
assert await set_usercodes(node, {"1": 1234})
async def test_clear_usercode(lock_schlage_be469, mock_command, uuid4):
"""Test clear_usercode utility function."""
node = lock_schlage_be469
ack_commands = mock_command(
{"command": "node.set_value", "nodeId": node.node_id},
{"result": {"status": 255}},
)
# Test valid code
await clear_usercode(node, 1)
assert len(ack_commands) == 1
assert ack_commands[0] == {
"command": "node.set_value",
"nodeId": 20,
"messageId": uuid4,
"valueId": {
"commandClass": 99,
"endpoint": 0,
"property": "userIdStatus",
"propertyKey": 1,
},
"value": 0,
}
# Test invalid code slot
with pytest.raises(NotFoundError):
await clear_usercode(node, 100)
# assert no new command calls
assert len(ack_commands) == 1
async def test_get_usercode_from_node(lock_schlage_be469, mock_command, uuid4):
"""Test get_usercode_from_node utility function."""
node = lock_schlage_be469
ack_commands = mock_command(
{"command": "endpoint.invoke_cc_api", "nodeId": node.node_id, "endpoint": 0},
{"response": {"userIdStatus": 1, "userCode": "**********"}},
)
# Test valid code
assert await get_usercode_from_node(node, 1) == {
ATTR_NAME: "User Code (1)",
ATTR_CODE_SLOT: 1,
ATTR_IN_USE: True,
ATTR_USERCODE: "**********",
}
assert len(ack_commands) == 1
assert ack_commands[0] == {
"command": "endpoint.invoke_cc_api",
"nodeId": 20,
"endpoint": 0,
"commandClass": 99,
"messageId": uuid4,
"methodName": "get",
"args": [1],
}
async def test_set_configuration_empty_response(
lock_schlage_be469, mock_command, uuid4
):
"""Test set_configuration utility function without response."""
node = lock_schlage_be469
ack_commands = mock_command(
{"command": "endpoint.invoke_cc_api", "nodeId": node.node_id, "endpoint": 0},
{},
)
await set_configuration(
node.endpoints[0],
DoorLockCCConfigurationSetOptions(OperationType.CONSTANT),
)
assert len(ack_commands) == 1
assert ack_commands[0] == {
"command": "endpoint.invoke_cc_api",
"nodeId": 20,
"endpoint": 0,
"commandClass": 98,
"messageId": uuid4,
"methodName": "setConfiguration",
"args": [
{
"insideHandlesCanOpenDoorConfiguration": [True, True, True, True],
"operationType": 1,
"outsideHandlesCanOpenDoorConfiguration": [True, True, True, True],
}
],
}
with pytest.raises(ValueError):
await set_configuration(
node.endpoints[0],
DoorLockCCConfigurationSetOptions(OperationType.CONSTANT, 1),
)
with pytest.raises(ValueError):
await set_configuration(
node.endpoints[0],
DoorLockCCConfigurationSetOptions(OperationType.TIMED),
)
async def test_set_configuration_with_response(lock_schlage_be469, mock_command, uuid4):
"""Test set_configuration utility function with response."""
node = lock_schlage_be469
ack_commands = mock_command(
{"command": "endpoint.invoke_cc_api", "nodeId": node.node_id, "endpoint": 0},
{"response": {"status": 0}},
)
result = await set_configuration(
node.endpoints[0],
DoorLockCCConfigurationSetOptions(OperationType.CONSTANT),
)
assert result.status == SupervisionStatus.NO_SUPPORT
assert len(ack_commands) == 1
assert ack_commands[0] == {
"command": "endpoint.invoke_cc_api",
"nodeId": 20,
"endpoint": 0,
"commandClass": 98,
"messageId": uuid4,
"methodName": "setConfiguration",
"args": [
{
"insideHandlesCanOpenDoorConfiguration": [True, True, True, True],
"operationType": 1,
"outsideHandlesCanOpenDoorConfiguration": [True, True, True, True],
}
],
}
async def test_set_configuration_v4(
driver, lock_ultraloq_ubolt_pro_state, mock_command, uuid4
):
"""Test v4 properties in set_configuration utility function."""
node_state = copy.deepcopy(lock_ultraloq_ubolt_pro_state)
node_state["values"].remove(
{
"endpoint": 0,
"commandClass": 98,
"commandClassName": "Door Lock",
"property": "twistAssist",
"propertyName": "twistAssist",
"ccVersion": 4,
"metadata": {
"type": "boolean",
"readable": True,
"writeable": True,
"label": "Twist Assist enabled",
},
"value": False,
},
)
node = Node(driver.client, node_state)
ack_commands = mock_command(
{"command": "endpoint.invoke_cc_api", "nodeId": node.node_id, "endpoint": 0},
{},
)
await set_configuration(
node.endpoints[0],
DoorLockCCConfigurationSetOptions(
OperationType.CONSTANT, hold_and_release_time=2, block_to_block=True
),
)
assert len(ack_commands) == 1
assert ack_commands[0] == {
"command": "endpoint.invoke_cc_api",
"nodeId": 34,
"endpoint": 0,
"commandClass": 98,
"messageId": uuid4,
"methodName": "setConfiguration",
"args": [
{
"insideHandlesCanOpenDoorConfiguration": [True, True, True, True],
"operationType": 1,
"outsideHandlesCanOpenDoorConfiguration": [True, True, True, True],
"autoRelockTime": 0,
"holdAndReleaseTime": 2,
"blockToBlock": True,
}
],
}
# Test a property that isn't on the node
with pytest.raises(ValueError):
await set_configuration(
node.endpoints[0],
DoorLockCCConfigurationSetOptions(
OperationType.CONSTANT, twist_assist=True
),
)
@pytest.mark.usefixtures("driver")
async def test_set_configuration_timed_lock(
timed_lock: Node, mock_command: MockCommandProtocol, uuid4: str
) -> None:
"""Test a timed lock in set_configuration utility function."""
node = timed_lock
ack_commands = mock_command(
{"command": "endpoint.invoke_cc_api", "nodeId": node.node_id, "endpoint": 0},
{},
)
await set_configuration(
node.endpoints[0],
DoorLockCCConfigurationSetOptions(
OperationType.TIMED, lock_timeout_configuration=42
),
)
assert len(ack_commands) == 1
assert ack_commands[0] == {
"command": "endpoint.invoke_cc_api",
"nodeId": 7,
"endpoint": 0,
"commandClass": 98,
"messageId": uuid4,
"methodName": "setConfiguration",
"args": [
{
"operationType": OperationType.TIMED,
"autoRelockTime": 0,
"blockToBlock": True,
"holdAndReleaseTime": 0,
"twistAssist": True,
"insideHandlesCanOpenDoorConfiguration": [True, True, True, True],
"outsideHandlesCanOpenDoorConfiguration": [True, True, True, True],
"lockTimeoutConfiguration": 42,
}
],
}
|