diff --git a/homeassistant/components/surepetcare/lock.py b/homeassistant/components/surepetcare/lock.py index e5b31150152..8351eea161b 100644 --- a/homeassistant/components/surepetcare/lock.py +++ b/homeassistant/components/surepetcare/lock.py @@ -83,16 +83,28 @@ class SurePetcareLock(SurePetcareEntity, LockEntity): async def async_lock(self, **kwargs: Any) -> None: """Lock the lock.""" - if self.state == STATE_LOCKED: + if self.state != STATE_UNLOCKED: return - await self.coordinator.lock_states_callbacks[self._lock_state](self._id) - self._attr_is_locked = True + self._attr_is_locking = True 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: """Unlock the lock.""" - if self.state == STATE_UNLOCKED: + if self.state != STATE_LOCKED: return - await self.coordinator.surepy.sac.unlock(self._id) - self._attr_is_locked = False + self._attr_is_unlocking = True 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() diff --git a/tests/components/surepetcare/test_lock.py b/tests/components/surepetcare/test_lock.py index 3a29ced9ace..a2c4ebad0b3 100644 --- a/tests/components/surepetcare/test_lock.py +++ b/tests/components/surepetcare/test_lock.py @@ -1,4 +1,6 @@ """The tests for the Sure Petcare lock platform.""" +import pytest +from surepy.exceptions import SurePetcareError from homeassistant.components.surepetcare.const import DOMAIN 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) assert state.state == "unlocked" 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"