From 172814204353e5055564f7cfb78dde3e4d9ac412 Mon Sep 17 00:00:00 2001 From: Jumpmaster2 <79658586+Jumpmaster2@users.noreply.github.com> Date: Thu, 5 Nov 2026 15:53:52 +0000 Subject: [PATCH] Lock: silent first retry, report only if the retry fails (0.6.2) Some locks report "unlocked" a few seconds after "locked" on the first lock command and hold on the retry. The first fallback (or jam) now only writes a logbook entry and retries; a push comes only if the retry falls back, jams, cannot run (door open, lock unavailable) or the lock is not "locked" LOCK_SETTLE seconds after the retry. Disarming cancels retry and check. Co-Authored-By: Claude Opus 5.5 --- README.en.md | 2 +- README.md | 2 +- custom_components/alarm_assistant/engine.py | 45 ++++++++++++++----- .../alarm_assistant/manifest.json | 2 +- custom_components/alarm_assistant/messages.py | 9 ++-- tests/ha/test_engine.py | 28 +++++++++++- 6 files changed, 68 insertions(+), 20 deletions(-) diff --git a/README.en.md b/README.en.md index 9b36d8e..1f7aa7b 100644 --- a/README.en.md +++ b/README.en.md @@ -50,7 +50,7 @@ The integration is event driven: it only reacts to state changes of the configur ### Door lock and children * Unlocking or opening the lock disarms. A lock that comes back from "unavailable" (radio, bridge, battery) as unlocked does not count as unlocking. -* If the lock falls back after locking or reports "jammed", the panel stays armed, a message is sent and locking is retried once. +* If the lock falls back after locking or reports "jammed", the panel stays armed, locking is retried silently. A message is sent only if the retry also fails. * Arming locks the door when it is closed and no children are alone at home. * **Children alone at home** (switch) prevents automatic arming and locking and controls the camera. It is reset a configurable time after unlocking and at night. diff --git a/README.md b/README.md index d0caaa0..c310b7d 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Die Integration arbeitet ereignisgesteuert: Sie reagiert nur auf Zustandswechsel ### Türschloss und Kinder * Entriegeln oder Öffnen des Schlosses schaltet unscharf. Meldet sich ein Schloss nach "nicht verfügbar" (Funk, Bridge, Batterie) als entriegelt zurück, zählt das nicht als Entriegeln. -* Fällt das Schloss nach dem Abschließen zurück oder meldet es "blockiert", bleibt die Anlage scharf, es kommt eine Meldung und ein zweiter Versuch. +* Fällt das Schloss nach dem Abschließen zurück oder meldet es "blockiert", bleibt die Anlage scharf, es folgt ein stiller zweiter Versuch. Erst wenn auch dieser scheitert, kommt eine Meldung. * Beim Scharfschalten wird abgeschlossen, wenn die Tür zu ist und keine Kinder alleine zuhause sind. * **Kinder alleine zuhause** (Schalter) verhindert automatisches Scharfschalten und Abschließen und steuert die Kamera. Er wird eine einstellbare Zeit nach dem Entriegeln und nachts zurückgesetzt. diff --git a/custom_components/alarm_assistant/engine.py b/custom_components/alarm_assistant/engine.py index d0cddce..f3ab4c6 100644 --- a/custom_components/alarm_assistant/engine.py +++ b/custom_components/alarm_assistant/engine.py @@ -285,6 +285,7 @@ def __init__(self, hass: HomeAssistant, entry: AlarmConfigEntry) -> None: # door lock: when we last sent "lock" and whether we already retried self.lock_sent: datetime | None = None self.lock_retried = False + self.lock_failure_reported = False # call self.call_block_until: datetime | None = None self.call_acked = False @@ -1036,6 +1037,8 @@ def _armed(self, automatic: bool) -> None: else: self._log("log_auto_armed" if automatic else "log_armed") self.lock_retried = False + self.lock_failure_reported = False + self._cancel("lock_check") self._lock_door() for entity_id in self._filter("armed_on", self.opt_list(CONF_ARM_ON_ENTITIES)): self.config_entry.async_create_task( @@ -1095,20 +1098,42 @@ def _lock_bounced(self, old: State | None, new: State) -> bool: def _lock_failed(self) -> None: self.lock_sent = None - self._log("log_lock_failed") - retry = not self.lock_retried - self._notify( - self._push(CONF_NOTIFY), - self._mark(self.t("title_lock_failed")), - self._mark(self.t("push_lock_retry" if retry else "push_lock_failed")), - ) - if retry: + if not self.lock_retried: self.lock_retried = True + self._log("log_lock_retry", seconds=LOCK_RETRY) self._schedule( "lock_retry", dt_util.utcnow() + timedelta(seconds=LOCK_RETRY), - lambda _now: self._lock_door() if self.state in sm.ARMED_STATES else None, + self._retry_lock, ) + elif not self.lock_failure_reported: + self.lock_failure_reported = True + self._cancel("lock_check") + self._log("log_lock_failed") + self._notify( + self._push(CONF_NOTIFY), + self._mark(self.t("title_lock_failed")), + self._mark(self.t("push_lock_failed")), + ) + + def _retry_lock(self, _now: datetime) -> None: + lock = self.opt(CONF_LOCK) + if self.state not in sm.ARMED_STATES or not lock or not self.opt(CONF_LOCK_ON_ARM): + return + if self.kids_home or not self._filter("lock", [lock]): + return + door = self.opt(CONF_LOCK_DOOR) + if (door and self._value(door) != sm.OFF) or self._value(lock) in (*INVALID, None): + self._lock_failed() + return + self._lock_door() + self._schedule( + "lock_check", dt_util.utcnow() + timedelta(seconds=LOCK_SETTLE), self._check_lock + ) + + def _check_lock(self, _now: datetime) -> None: + if self.state in sm.ARMED_STATES and self._value(self.opt(CONF_LOCK)) != "locked": + self._lock_failed() def disarm(self, by_lock: bool = False) -> None: if self.state == sm.DISARMED: @@ -1116,7 +1141,7 @@ def disarm(self, by_lock: bool = False) -> None: if self.state == sm.TRIGGERED: self._sirens_off() self.lock_sent = None - for key in ("exit", "entry", "trigger_end", "lock_retry"): + for key in ("exit", "entry", "trigger_end", "lock_retry", "lock_check"): self._cancel(key) self.state = sm.DISARMED self.bypassed = [] diff --git a/custom_components/alarm_assistant/manifest.json b/custom_components/alarm_assistant/manifest.json index baae75d..69ba1c4 100644 --- a/custom_components/alarm_assistant/manifest.json +++ b/custom_components/alarm_assistant/manifest.json @@ -10,5 +10,5 @@ "issue_tracker": "https://github.com/Jumpmaster2/Alarm-Assistant/issues", "requirements": [], "single_config_entry": true, - "version": "0.6.1" + "version": "0.6.2" } diff --git a/custom_components/alarm_assistant/messages.py b/custom_components/alarm_assistant/messages.py index 966ff65..b64c086 100644 --- a/custom_components/alarm_assistant/messages.py +++ b/custom_components/alarm_assistant/messages.py @@ -36,11 +36,6 @@ "en": "Alarm armed, these open sensors are ignored until disarming: {sensors}.", }, "title_lock_failed": {"de": "Tür nicht abgeschlossen", "en": "Door not locked"}, - "push_lock_retry": { - "de": "Das Schloss hat sich nicht verriegelt. Die Alarmanlage bleibt scharf, " - "Abschließen wird gleich noch einmal versucht.", - "en": "The lock did not engage. The alarm stays armed, locking is retried shortly.", - }, "push_lock_failed": { "de": "Das Schloss hat sich wieder nicht verriegelt. Die Alarmanlage bleibt scharf, " "die Tür ist aber nicht abgeschlossen.", @@ -142,6 +137,10 @@ }, "log_auto_armed": {"de": "Automatisch scharf geschaltet", "en": "Armed automatically"}, "log_locked": {"de": "Tür abgeschlossen", "en": "Door locked"}, + "log_lock_retry": { + "de": "Abschließen nicht bestätigt, neuer Versuch in {seconds} s", + "en": "Locking not confirmed, retrying in {seconds} s", + }, "log_lock_failed": { "de": "Abschließen fehlgeschlagen, bleibt scharf", "en": "Locking failed, stays armed", diff --git a/tests/ha/test_engine.py b/tests/ha/test_engine.py index 33b3b77..d38d573 100644 --- a/tests/ha/test_engine.py +++ b/tests/ha/test_engine.py @@ -242,7 +242,7 @@ async def test_lock_falling_back_does_not_disarm( await advance(hass, freezer, 3) await set_state(hass, LOCK, "unlocked") assert _state(hass) == "armed_away" - assert any("retried" in c.data["message"] for c in calls["notify"]) + assert not any("lock" in c.data["message"].lower() for c in calls["notify"]) # one retry, a second failure only warns await advance(hass, freezer, 11) assert len(calls["lock"]) == 2 @@ -258,6 +258,30 @@ async def test_lock_falling_back_does_not_disarm( assert _state(hass) == "disarmed" +@pytest.mark.parametrize("outcome", ["locked", "unchanged", "disarmed", "jammed"]) +async def test_lock_retry_outcomes( + hass: HomeAssistant, base_states, calls, freezer: FrozenDateTimeFactory, outcome: str +) -> None: + freezer.move_to(START) + await setup_entry(hass, make_entry(**REACTIONS)) + await _arm(hass) + if outcome == "jammed": + await set_state(hass, LOCK, "jammed") + else: + await set_state(hass, LOCK, "locking") + await set_state(hass, LOCK, "unlocked") + assert not any("lock" in c.data["message"].lower() for c in calls["notify"]) + if outcome == "disarmed": + await _disarm(hass) + await advance(hass, freezer, 11) + assert len(calls["lock"]) == (1 if outcome == "disarmed" else 2) + if outcome in ("locked", "jammed"): + await set_state(hass, LOCK, "locked") + await advance(hass, freezer, 25) + failures = [c for c in calls["notify"] if "again" in c.data["message"]] + assert len(failures) == (1 if outcome == "unchanged" else 0) + + async def test_code( hass: HomeAssistant, base_states, calls, freezer: FrozenDateTimeFactory ) -> None: @@ -802,7 +826,7 @@ async def test_jammed_lock_warns_and_retries( await set_state(hass, LOCK, "locking") await set_state(hass, LOCK, "jammed") assert _state(hass) == "armed_away" - assert any("retried" in c.data["message"] for c in calls["notify"]) + assert not any("lock" in c.data["message"].lower() for c in calls["notify"]) await advance(hass, freezer, 11) assert len(calls["lock"]) == 2