Skip to content

Commit

Permalink
Remove optimistic lock behavior, change to property
Browse files Browse the repository at this point in the history
  • Loading branch information
rikroe committed Mar 31, 2022
1 parent 8e32fdb commit 86aff4f
Showing 1 changed file with 19 additions and 26 deletions.
45 changes: 19 additions & 26 deletions custom_components/bmw_connected_drive/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,40 +53,33 @@ def __init__(
self._sensor_name = sensor_name
self.door_lock_state_available = DOOR_LOCK_STATE in vehicle.available_attributes

def lock(self, **kwargs: Any) -> None:
"""Lock the car."""
_LOGGER.debug("%s: locking doors", self.vehicle.name)
# Optimistic state set here because it takes some time before the
# update callback response
self._attr_is_locked = True
self.schedule_update_ha_state()
self.vehicle.remote_services.trigger_remote_door_lock()

def unlock(self, **kwargs: Any) -> None:
"""Unlock the car."""
_LOGGER.debug("%s: unlocking doors", self.vehicle.name)
# Optimistic state set here because it takes some time before the
# update callback response
self._attr_is_locked = False
self.schedule_update_ha_state()
self.vehicle.remote_services.trigger_remote_door_unlock()
@property
def extra_state_attributes(self):
return {"door_lock_state": self.vehicle.status.door_lock_state.value}

def update(self) -> None:
"""Update state of the lock."""
@property
def is_locked(self):
"""Return true if lock is locked."""
_LOGGER.debug(
"Updating lock data for '%s' of %s", self._attribute, self.vehicle.name
)
vehicle_state = self.vehicle.status
if not self.door_lock_state_available:
self._attr_is_locked = None
return None
else:
self._attr_is_locked = vehicle_state.door_lock_state in {
return vehicle_state.door_lock_state in {
LockState.LOCKED,
LockState.SECURED,
}

result = self._attrs.copy()
if self.door_lock_state_available:
result["door_lock_state"] = vehicle_state.door_lock_state.value
result["last_update_reason"] = vehicle_state.last_update_reason
self._attr_extra_state_attributes = result
def lock(self, **kwargs: Any) -> None:
"""Lock the car."""
_LOGGER.debug("%s: locking doors", self.vehicle.name)
self.vehicle.remote_services.trigger_remote_door_lock()
self.schedule_update_ha_state()

def unlock(self, **kwargs: Any) -> None:
"""Unlock the car."""
_LOGGER.debug("%s: unlocking doors", self.vehicle.name)
self.vehicle.remote_services.trigger_remote_door_unlock()
self.schedule_update_ha_state()

0 comments on commit 86aff4f

Please sign in to comment.