From 4bfff1257056cae7862c1aecc8cfdf3d911c7c6a Mon Sep 17 00:00:00 2001 From: Richard Kroegel <42204099+rikroe@users.noreply.github.com> Date: Mon, 3 Jun 2024 07:48:48 +0200 Subject: [PATCH] Set lock state to unkown on BMW API error (#118559) * Revert to previous lock state on BMW API error * Set lock state to unkown on error and force refresh from API --------- Co-authored-by: Richard --- .../components/bmw_connected_drive/lock.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/bmw_connected_drive/lock.py b/homeassistant/components/bmw_connected_drive/lock.py index bbfadcef9db..e138f31ba24 100644 --- a/homeassistant/components/bmw_connected_drive/lock.py +++ b/homeassistant/components/bmw_connected_drive/lock.py @@ -65,11 +65,13 @@ class BMWLock(BMWBaseEntity, LockEntity): try: await self.vehicle.remote_services.trigger_remote_door_lock() except MyBMWAPIError as ex: - self._attr_is_locked = False + # Set the state to unknown if the command fails + self._attr_is_locked = None self.async_write_ha_state() raise HomeAssistantError(ex) from ex - - self.coordinator.async_update_listeners() + finally: + # Always update the listeners to get the latest state + self.coordinator.async_update_listeners() async def async_unlock(self, **kwargs: Any) -> None: """Unlock the car.""" @@ -83,11 +85,13 @@ class BMWLock(BMWBaseEntity, LockEntity): try: await self.vehicle.remote_services.trigger_remote_door_unlock() except MyBMWAPIError as ex: - self._attr_is_locked = True + # Set the state to unknown if the command fails + self._attr_is_locked = None self.async_write_ha_state() raise HomeAssistantError(ex) from ex - - self.coordinator.async_update_listeners() + finally: + # Always update the listeners to get the latest state + self.coordinator.async_update_listeners() @callback def _handle_coordinator_update(self) -> None: