diff --git a/src/data/lock.ts b/src/data/lock.ts index 26d42178023f..17acf134877f 100644 --- a/src/data/lock.ts +++ b/src/data/lock.ts @@ -26,6 +26,10 @@ export function isLocked(stateObj: LockEntity) { return stateObj.state === "locked"; } +export function isUnlocked(stateObj: LockEntity) { + return stateObj.state === "unlocked"; +} + export function isUnlocking(stateObj: LockEntity) { return stateObj.state === "unlocking"; } @@ -38,15 +42,40 @@ export function isJammed(stateObj: LockEntity) { return stateObj.state === "jammed"; } -export function isAvailable(stateObj: LockEntity) { +export function isOpen(stateObj: LockEntity) { + return stateObj.state === "open"; +} + +export function isOpening(stateObj: LockEntity) { + return stateObj.state === "opening"; +} + +export function isWaiting(stateObj: LockEntity) { + return ["opening", "unlocking", "locking"].includes(stateObj.state); +} + +export function canOpen(stateObj: LockEntity) { + if (stateObj.state === UNAVAILABLE) { + return false; + } + const assumedState = stateObj.attributes.assumed_state === true; + return assumedState || (!isOpen(stateObj) && !isWaiting(stateObj)); +} + +export function canLock(stateObj: LockEntity) { + if (stateObj.state === UNAVAILABLE) { + return false; + } + const assumedState = stateObj.attributes.assumed_state === true; + return assumedState || (!isLocked(stateObj) && !isWaiting(stateObj)); +} + +export function canUnlock(stateObj: LockEntity) { if (stateObj.state === UNAVAILABLE) { return false; } const assumedState = stateObj.attributes.assumed_state === true; - return ( - assumedState || - (!isLocking(stateObj) && !isUnlocking(stateObj) && !isJammed(stateObj)) - ); + return assumedState || (!isUnlocked(stateObj) && !isWaiting(stateObj)); } export const callProtectedLockService = async ( diff --git a/src/dialogs/more-info/controls/more-info-lock.ts b/src/dialogs/more-info/controls/more-info-lock.ts index 2236a523518b..233a3df6cde0 100644 --- a/src/dialogs/more-info/controls/more-info-lock.ts +++ b/src/dialogs/more-info/controls/more-info-lock.ts @@ -13,7 +13,7 @@ import { LockEntity, LockEntityFeature, callProtectedLockService, - isAvailable, + canOpen, isJammed, } from "../../../data/lock"; import "../../../state-control/lock/ha-state-control-lock-toggle"; @@ -22,9 +22,9 @@ import "../components/ha-more-info-state-header"; import { moreInfoControlStyle } from "../components/more-info-control-style"; const CONFIRM_TIMEOUT_SECOND = 5; -const OPENED_TIMEOUT_SECOND = 3; +const DONE_TIMEOUT_SECOND = 2; -type ButtonState = "normal" | "confirm" | "success"; +type ButtonState = "normal" | "confirm" | "done"; @customElement("more-info-lock") class MoreInfoLock extends LitElement { @@ -54,7 +54,7 @@ class MoreInfoLock extends LitElement { callProtectedLockService(this, this.hass, this.stateObj!, "open"); - this._setButtonState("success", OPENED_TIMEOUT_SECOND); + this._setButtonState("done", DONE_TIMEOUT_SECOND); } private _resetButtonState() { @@ -115,16 +115,16 @@ class MoreInfoLock extends LitElement { ${supportsOpen ? html`