Add locking state to surepetcare locks (#56830)

This commit is contained in:
Daniel Hjelseth Høyer 2021-09-30 17:16:35 +02:00 committed by GitHub
parent 6af1a835e6
commit 4a2ed97e0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 6 deletions

View File

@ -83,16 +83,28 @@ class SurePetcareLock(SurePetcareEntity, LockEntity):
async def async_lock(self, **kwargs: Any) -> None: async def async_lock(self, **kwargs: Any) -> None:
"""Lock the lock.""" """Lock the lock."""
if self.state == STATE_LOCKED: if self.state != STATE_UNLOCKED:
return return
await self.coordinator.lock_states_callbacks[self._lock_state](self._id) self._attr_is_locking = True
self._attr_is_locked = True
self.async_write_ha_state() self.async_write_ha_state()
try:
await self.coordinator.lock_states_callbacks[self._lock_state](self._id)
self._attr_is_locked = True
finally:
self._attr_is_locking = False
self.async_write_ha_state()
async def async_unlock(self, **kwargs: Any) -> None: async def async_unlock(self, **kwargs: Any) -> None:
"""Unlock the lock.""" """Unlock the lock."""
if self.state == STATE_UNLOCKED: if self.state != STATE_LOCKED:
return return
await self.coordinator.surepy.sac.unlock(self._id) self._attr_is_unlocking = True
self._attr_is_locked = False
self.async_write_ha_state() self.async_write_ha_state()
try:
await self.coordinator.surepy.sac.unlock(self._id)
self._attr_is_locked = False
finally:
self._attr_is_unlocking = False
self.async_write_ha_state()

View File

@ -1,4 +1,6 @@
"""The tests for the Sure Petcare lock platform.""" """The tests for the Sure Petcare lock platform."""
import pytest
from surepy.exceptions import SurePetcareError
from homeassistant.components.surepetcare.const import DOMAIN from homeassistant.components.surepetcare.const import DOMAIN
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
@ -73,3 +75,41 @@ async def test_locks(hass, surepetcare) -> None:
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state.state == "unlocked" assert state.state == "unlocked"
assert surepetcare.unlock.call_count == 1 assert surepetcare.unlock.call_count == 1
async def test_lock_failing(hass, surepetcare) -> None:
"""Test handling of lock failing."""
assert await async_setup_component(hass, DOMAIN, MOCK_CONFIG)
await hass.async_block_till_done()
surepetcare.lock_in.side_effect = SurePetcareError
surepetcare.lock_out.side_effect = SurePetcareError
surepetcare.lock.side_effect = SurePetcareError
for entity_id, unique_id in EXPECTED_ENTITY_IDS.items():
with pytest.raises(SurePetcareError):
await hass.services.async_call(
"lock", "lock", {"entity_id": entity_id}, blocking=True
)
state = hass.states.get(entity_id)
assert state.state == "unlocked"
async def test_unlock_failing(hass, surepetcare) -> None:
"""Test handling of unlock failing."""
assert await async_setup_component(hass, DOMAIN, MOCK_CONFIG)
await hass.async_block_till_done()
entity_id = list(EXPECTED_ENTITY_IDS.keys())[0]
await hass.services.async_call(
"lock", "lock", {"entity_id": entity_id}, blocking=True
)
surepetcare.unlock.side_effect = SurePetcareError
with pytest.raises(SurePetcareError):
await hass.services.async_call(
"lock", "unlock", {"entity_id": entity_id}, blocking=True
)
state = hass.states.get(entity_id)
assert state.state == "locked"