Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matter lock state follow-up #121669

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions homeassistant/components/matter/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async def async_lock(self, **kwargs: Any) -> None:
# the lock should acknowledge the command with an attribute update
# but bad things may happen, so guard against it with a timer.
self._optimistic_timer = self.hass.loop.call_later(
5, self._reset_optimistic_state
30, self._reset_optimistic_state
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We never test that the callback runs after the time.

To make it easier to test this make the time a module level constant that can easily be patched.

)
code: str | None = kwargs.get(ATTR_CODE)
code_bytes = code.encode() if code else None
Expand All @@ -116,7 +116,7 @@ async def async_unlock(self, **kwargs: Any) -> None:
# the lock should acknowledge the command with an attribute update
# but bad things may happen, so guard against it with a timer.
self._optimistic_timer = self.hass.loop.call_later(
5, self._reset_optimistic_state
30, self._reset_optimistic_state
)
code: str | None = kwargs.get(ATTR_CODE)
code_bytes = code.encode() if code else None
Expand All @@ -140,7 +140,7 @@ async def async_open(self, **kwargs: Any) -> None:
# the lock should acknowledge the command with an attribute update
# but bad things may happen, so guard against it with a timer.
self._optimistic_timer = self.hass.loop.call_later(
5, self._reset_optimistic_state
30 if self._attr_is_locked else 5, self._reset_optimistic_state
)
code: str | None = kwargs.get(ATTR_CODE)
code_bytes = code.encode() if code else None
Expand Down
2 changes: 1 addition & 1 deletion tests/components/matter/fixtures/nodes/door-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@
"1/47/65531": [
0, 1, 2, 14, 15, 16, 19, 65528, 65529, 65530, 65531, 65532, 65533
],
"1/257/0": 1,
"1/257/0": 0,
"1/257/1": 0,
"1/257/2": true,
"1/257/3": 1,
Expand Down
11 changes: 8 additions & 3 deletions tests/components/matter/test_door_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

from homeassistant.components.lock import (
STATE_LOCKED,
STATE_OPEN,
STATE_UNLOCKED,
LockEntityFeature,
)
from homeassistant.const import ATTR_CODE, STATE_LOCKING, STATE_UNKNOWN
from homeassistant.const import ATTR_CODE, STATE_LOCKING, STATE_OPENING, STATE_UNKNOWN
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
import homeassistant.helpers.entity_registry as er
Expand Down Expand Up @@ -64,6 +63,7 @@ async def test_lock(
)
matter_client.send_device_command.reset_mock()

await hass.async_block_till_done()
state = hass.states.get("lock.mock_door_lock_lock")
assert state
assert state.state == STATE_LOCKING
Expand Down Expand Up @@ -208,9 +208,14 @@ async def test_lock_with_unbolt(
timed_request_timeout_ms=1000,
)

await hass.async_block_till_done()
state = hass.states.get("lock.mock_door_lock_lock")
assert state
assert state.state == STATE_OPENING

set_node_attribute(door_lock_with_unbolt, 1, 257, 3, 0)
await trigger_subscription_callback(hass, matter_client)

state = hass.states.get("lock.mock_door_lock_lock")
assert state
assert state.state == STATE_OPEN
assert state.state == STATE_LOCKED