Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raman325 committed Nov 9, 2023
1 parent df2cd2b commit c914b28
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 10 deletions.
16 changes: 8 additions & 8 deletions homeassistant/components/zwave_js/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,18 @@ async def async_clear_lock_usercode(self, code_slot: int) -> None:
async def async_set_lock_configuration(
self,
operation_type: OperationType,
lock_timeout: int | None,
outside_handles_can_open_door_configuration: list[bool] | None,
inside_handles_can_open_door_configuration: list[bool] | None,
auto_relock_time: int | None,
hold_and_release_time: int | None,
twist_assist: bool | None,
block_to_block: bool | None,
lock_timeout: int | None = None,
outside_handles_can_open_door_configuration: list[bool] | None = None,
inside_handles_can_open_door_configuration: list[bool] | None = None,
auto_relock_time: int | None = None,
hold_and_release_time: int | None = None,
twist_assist: bool | None = None,
block_to_block: bool | None = None,
) -> None:
"""Set the lock configuration."""
params: dict[str, Any] = {"operation_type": operation_type}
for attr, val in (
("lock_timeout", lock_timeout),
("lock_timeout_configuration", lock_timeout),
(
"outside_handles_can_open_door_configuration",
outside_handles_can_open_door_configuration,
Expand Down
103 changes: 101 additions & 2 deletions tests/components/zwave_js/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
SERVICE_LOCK,
SERVICE_UNLOCK,
)
from homeassistant.components.zwave_js.const import DOMAIN as ZWAVE_JS_DOMAIN
from homeassistant.components.zwave_js.const import (
ATTR_LOCK_TIMEOUT,
ATTR_OPERATION_TYPE,
DOMAIN as ZWAVE_JS_DOMAIN,
)
from homeassistant.components.zwave_js.helpers import ZwaveValueMatcher
from homeassistant.components.zwave_js.lock import (
SERVICE_CLEAR_LOCK_USERCODE,
SERVICE_SET_LOCK_CONFIGURATION,
SERVICE_SET_LOCK_USERCODE,
)
from homeassistant.const import (
Expand All @@ -35,7 +40,11 @@


async def test_door_lock(
hass: HomeAssistant, client, lock_schlage_be469, integration
hass: HomeAssistant,
client,
lock_schlage_be469,
integration,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test a lock entity with door lock command class."""
node = lock_schlage_be469
Expand Down Expand Up @@ -158,6 +167,96 @@ async def test_door_lock(

client.async_send_command.reset_mock()

# Test set configuration
client.async_send_command.return_value = {
"response": {"status": 1, "remainingDuration": "default"}
}
caplog.clear()
await hass.services.async_call(
ZWAVE_JS_DOMAIN,
SERVICE_SET_LOCK_CONFIGURATION,
{
ATTR_ENTITY_ID: SCHLAGE_BE469_LOCK_ENTITY,
ATTR_OPERATION_TYPE: "timed",
ATTR_LOCK_TIMEOUT: 1,
},
blocking=True,
)

assert len(client.async_send_command.call_args_list) == 1
args = client.async_send_command.call_args[0][0]
assert args["command"] == "endpoint.invoke_cc_api"
assert args["nodeId"] == 20
assert args["endpoint"] == 0
assert args["args"] == [
{
"insideHandlesCanOpenDoorConfiguration": [True, True, True, True],
"operationType": 2,
"outsideHandlesCanOpenDoorConfiguration": [True, True, True, True],
}
]
assert args["commandClass"] == 98
assert args["methodName"] == "setConfiguration"
assert "Result status" in caplog.text
assert "remaining duration" in caplog.text
assert "setting lock configuration" in caplog.text

client.async_send_command.reset_mock()
client.async_send_command_no_wait.reset_mock()
caplog.clear()

# Put node to sleep and validate that we don't wait for a return or log anything
event = Event(
"sleep",
{
"source": "node",
"event": "sleep",
"nodeId": node.node_id,
},
)
node.receive_event(event)

await hass.services.async_call(
ZWAVE_JS_DOMAIN,
SERVICE_SET_LOCK_CONFIGURATION,
{
ATTR_ENTITY_ID: SCHLAGE_BE469_LOCK_ENTITY,
ATTR_OPERATION_TYPE: "timed",
ATTR_LOCK_TIMEOUT: 1,
},
blocking=True,
)

assert len(client.async_send_command.call_args_list) == 0
assert len(client.async_send_command_no_wait.call_args_list) == 1
args = client.async_send_command_no_wait.call_args[0][0]
assert args["command"] == "endpoint.invoke_cc_api"
assert args["nodeId"] == 20
assert args["endpoint"] == 0
assert args["args"] == [
{
"insideHandlesCanOpenDoorConfiguration": [True, True, True, True],
"operationType": 2,
"outsideHandlesCanOpenDoorConfiguration": [True, True, True, True],
}
]
assert args["commandClass"] == 98
assert args["methodName"] == "setConfiguration"
assert "Result status" not in caplog.text
assert "remaining duration" not in caplog.text
assert "setting lock configuration" not in caplog.text

# Mark node as alive
event = Event(
"alive",
{
"source": "node",
"event": "alive",
"nodeId": node.node_id,
},
)
node.receive_event(event)

client.async_send_command.side_effect = FailedZWaveCommand("test", 1, "test")
# Test set usercode service error handling
with pytest.raises(HomeAssistantError):
Expand Down

0 comments on commit c914b28

Please sign in to comment.