Skip to content

Commit

Permalink
Remove validation from lock util functions (#1092)
Browse files Browse the repository at this point in the history
* Remove validation from lock util functions

* Expect listed types

* Fix test
  • Loading branch information
raman325 authored Oct 13, 2024
1 parent 6555362 commit 068a98a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 29 deletions.
17 changes: 1 addition & 16 deletions test/util/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,6 @@ async def test_set_usercode(lock_schlage_be469, mock_command, uuid4):
# assert no new command calls
assert len(ack_commands) == 1

# Test invalid code length
with pytest.raises(ValueError):
await set_usercode(node, 1, "123")

# assert no new command calls
assert len(ack_commands) == 1


async def test_set_usercodes(
lock_schlage_be469: Node, mock_command: MockCommandProtocol, uuid4: str
Expand All @@ -118,8 +111,7 @@ async def test_set_usercodes(
{"response": {"status": 255}},
)

# Test wrong types to ensure values get converted
assert await set_usercodes(node, {"1": 1234}) == SupervisionResult(
assert await set_usercodes(node, {1: "1234"}) == SupervisionResult(
{"status": SupervisionStatus.SUCCESS}
)
assert len(ack_commands) == 1
Expand All @@ -141,13 +133,6 @@ async def test_set_usercodes(
],
}

# Test invalid code length
with pytest.raises(ValueError):
await set_usercodes(node, {1: "123"})

# assert no new command calls
assert len(ack_commands) == 1


async def test_set_usercodes_invalid(
lock_schlage_be469: Node, mock_command: MockCommandProtocol
Expand Down
16 changes: 3 additions & 13 deletions zwave_js_server/util/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,29 +143,19 @@ async def set_usercode(
) -> SetValueResult | None:
"""Set the usercode to index X on the lock."""
value = get_code_slot_value(node, code_slot, LOCK_USERCODE_PROPERTY)
usercode = str(usercode)

if len(usercode) < 4:
raise ValueError("User code must be at least 4 digits")

return await node.async_set_value(value, usercode)


async def set_usercodes(node: Node, codes: dict[int, str]) -> SupervisionResult | None:
"""Set the usercode to index X on the lock."""
cc_api_codes = [
{
LOCK_USERCODE_ID_PROPERTY: int(code_slot),
LOCK_USERCODE_ID_PROPERTY: code_slot,
LOCK_USERCODE_STATUS_PROPERTY: CodeSlotStatus.ENABLED,
LOCK_USERCODE_PROPERTY: str(usercode),
LOCK_USERCODE_PROPERTY: usercode,
}
for code_slot, usercode in codes.items()
if len(str(usercode)) >= 4
]

if len(cc_api_codes) < len(codes):
raise ValueError("User codes must be at least 4 digits")

# https://zwave-js.github.io/node-zwave-js/#/api/CCs/UserCode?id=setmany
data = await node.async_invoke_cc_api(
CommandClass.USER_CODE, "setMany", cc_api_codes, wait_for_result=True
Expand All @@ -180,7 +170,7 @@ async def set_usercodes(node: Node, codes: dict[int, str]) -> SupervisionResult
async def clear_usercode(node: Node, code_slot: int) -> SetValueResult | None:
"""Clear a code slot on the lock."""
value = get_code_slot_value(node, code_slot, LOCK_USERCODE_STATUS_PROPERTY)
return await node.async_set_value(value, CodeSlotStatus.AVAILABLE.value)
return await node.async_set_value(value, CodeSlotStatus.AVAILABLE)


async def set_configuration(
Expand Down

0 comments on commit 068a98a

Please sign in to comment.