diff --git a/custom_components/fordpass/lock.py b/custom_components/fordpass/lock.py index ff39a88..9a05e55 100644 --- a/custom_components/fordpass/lock.py +++ b/custom_components/fordpass/lock.py @@ -8,25 +8,27 @@ _LOGGER = logging.getLogger(__name__) - async def async_setup_entry(hass, config_entry, async_add_entities): """Add the lock from the config.""" entry = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR] lock = Lock(entry) - if "doorLockStatus" in lock.coordinator.data["metrics"] and lock.coordinator.data["metrics"]["doorLockStatus"] and lock.coordinator.data["metrics"]["doorLockStatus"][0]["value"] != "ERROR": + + door_lock_status = lock.coordinator.data.get("metrics", {}).get("doorLockStatus", []) + all_doors_status = next((item for item in door_lock_status if item.get("vehicleDoor") == "ALL_DOORS"), None) + + if all_doors_status and all_doors_status.get("value") != "ERROR": async_add_entities([lock], False) else: _LOGGER.debug("Ford model doesn't support remote locking") - class Lock(FordPassEntity, LockEntity): """Defines the vehicle's lock.""" def __init__(self, coordinator): """Initialize.""" self._device_id = "fordpass_doorlock" self.coordinator = coordinator - self.data = coordinator.data["metrics"] + self.data = coordinator.data.get("metrics", {}) # Required for HA 2022.7 self.coordinator_context = object() @@ -61,9 +63,11 @@ async def async_unlock(self, **kwargs): @property def is_locked(self): """Determine if the lock is locked.""" - if self.coordinator.data["metrics"] is None or self.coordinator.data["metrics"]["doorLockStatus"] is None: + door_lock_status = self.coordinator.data.get("metrics", {}).get("doorLockStatus", []) + all_doors_status = next((item for item in door_lock_status if item.get("vehicleDoor") == "ALL_DOORS"), None) + if not all_doors_status: return None - return self.coordinator.data["metrics"]["doorLockStatus"][0]["value"] == "LOCKED" or self.coordinator.data["metrics"]["doorLockStatus"][0]["value"] == "DOUBLE_LOCKED" + return all_doors_status.get("value") == "LOCKED" or all_doors_status.get("value") == "DOUBLE_LOCKED" @property def icon(self):