From 4241879d7955d41b034fd7f4777e3fac852a8ad1 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Mon, 11 Mar 2024 19:49:02 +0000 Subject: [PATCH 01/11] Add default code to alarm_control_panel --- src/data/entity_registry.ts | 6 +++++ .../entity-registry-settings-editor.ts | 27 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/data/entity_registry.ts b/src/data/entity_registry.ts index 2ebfd157785f..02b72bf47782 100644 --- a/src/data/entity_registry.ts +++ b/src/data/entity_registry.ts @@ -96,6 +96,10 @@ export interface LockEntityOptions { default_code?: string | null; } +export interface AlarmControlPanelEntityOptions { + default_code?: string | null; +} + export interface WeatherEntityOptions { precipitation_unit?: string | null; pressure_unit?: string | null; @@ -112,6 +116,7 @@ export interface SwitchAsXEntityOptions { export interface EntityRegistryOptions { number?: NumberEntityOptions; sensor?: SensorEntityOptions; + alarm_control_panel?: AlarmControlPanelEntityOptions; lock?: LockEntityOptions; weather?: WeatherEntityOptions; light?: LightEntityOptions; @@ -134,6 +139,7 @@ export interface EntityRegistryEntryUpdateParams { | SensorEntityOptions | NumberEntityOptions | LockEntityOptions + | AlarmControlPanelEntityOptions | WeatherEntityOptions | LightEntityOptions; aliases?: string[]; diff --git a/src/panels/config/entities/entity-registry-settings-editor.ts b/src/panels/config/entities/entity-registry-settings-editor.ts index b4e8edd88573..a63850941ac4 100644 --- a/src/panels/config/entities/entity-registry-settings-editor.ts +++ b/src/panels/config/entities/entity-registry-settings-editor.ts @@ -59,6 +59,7 @@ import { updateDeviceRegistryEntry, } from "../../../data/device_registry"; import { + AlarmControlPanelEntityOptions, EntityRegistryEntry, EntityRegistryEntryUpdateParams, ExtEntityRegistryEntry, @@ -257,6 +258,10 @@ export class EntityRegistrySettingsEditor extends LitElement { this._defaultCode = this.entry.options?.lock?.default_code; } + if (domain === "alarm_control_panel") { + this._defaultCode = this.entry.options?.alarm_control_panel?.default_code; + } + if (domain === "weather") { const stateObj: HassEntity | undefined = this.hass.states[this.entry.entity_id]; @@ -583,6 +588,19 @@ export class EntityRegistrySettingsEditor extends LitElement { > ` : ""} + ${domain === "alarm_control_panel" + ? html` + + ` + : ""} ${domain === "sensor" && this._deviceClass && stateObj?.attributes.unit_of_measurement && @@ -1071,6 +1089,15 @@ export class EntityRegistrySettingsEditor extends LitElement { params.options = this.entry.options?.[domain] || {}; (params.options as LockEntityOptions).default_code = this._defaultCode; } + if ( + domain === "alarm_control_panel" && + this.entry.options?.[domain]?.default_code !== this._defaultCode + ) { + params.options_domain = domain; + params.options = this.entry.options?.[domain] || {}; + (params.options as AlarmControlPanelEntityOptions).default_code = + this._defaultCode; + } if ( domain === "weather" && (stateObj?.attributes?.precipitation_unit !== this._precipitation_unit || From 8d1d38b1abe03acfaf232c281127ff1211a24a47 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Mon, 11 Mar 2024 20:48:46 +0000 Subject: [PATCH 02/11] Don't show keypad if default code --- ...state-control-alarm_control_panel-modes.ts | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/state-control/alarm_control_panel/ha-state-control-alarm_control_panel-modes.ts b/src/state-control/alarm_control_panel/ha-state-control-alarm_control_panel-modes.ts index 585d14d9ca81..911de1033c4b 100644 --- a/src/state-control/alarm_control_panel/ha-state-control-alarm_control_panel-modes.ts +++ b/src/state-control/alarm_control_panel/ha-state-control-alarm_control_panel-modes.ts @@ -15,6 +15,7 @@ import { import { UNAVAILABLE } from "../../data/entity"; import { showEnterCodeDialog } from "../../dialogs/enter-code/show-enter-code-dialog"; import { HomeAssistant } from "../../types"; +import { getExtendedEntityRegistryEntry } from "../../data/entity_registry"; @customElement("ha-state-control-alarm_control_panel-modes") export class HaStateControlAlarmControlPanelModes extends LitElement { @@ -56,19 +57,30 @@ export class HaStateControlAlarmControlPanelModes extends LitElement { ) { const disarm = mode === "disarmed"; - const response = await showEnterCodeDialog(this, { - codeFormat: this.stateObj!.attributes.code_format, - title: this.hass!.localize( - `ui.card.alarm_control_panel.${disarm ? "disarm" : "arm"}` - ), - submitText: this.hass!.localize( - `ui.card.alarm_control_panel.${disarm ? "disarmn" : "arm"}` - ), - }); - if (response == null) { - throw new Error("cancel"); + const AlarmControlPanelRegistryEntry = + await getExtendedEntityRegistryEntry( + this.hass, + this.stateObj.entity_id + ).catch(() => undefined); + const defaultCode = + AlarmControlPanelRegistryEntry?.options?.alarm_control_panel + ?.default_code; + + if (!defaultCode) { + const response = await showEnterCodeDialog(this, { + codeFormat: this.stateObj!.attributes.code_format, + title: this.hass!.localize( + `ui.card.alarm_control_panel.${disarm ? "disarm" : "arm"}` + ), + submitText: this.hass!.localize( + `ui.card.alarm_control_panel.${disarm ? "disarmn" : "arm"}` + ), + }); + if (response == null) { + throw new Error("cancel"); + } + code = response; } - code = response; } await this.hass!.callService("alarm_control_panel", service, { From b32acd68ffd1bf9213b3b9a9d871ff2c525c4903 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Tue, 16 Apr 2024 19:28:15 +0000 Subject: [PATCH 03/11] Fix comments --- .../ha-state-control-alarm_control_panel-modes.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/state-control/alarm_control_panel/ha-state-control-alarm_control_panel-modes.ts b/src/state-control/alarm_control_panel/ha-state-control-alarm_control_panel-modes.ts index 911de1033c4b..be5408ce0c52 100644 --- a/src/state-control/alarm_control_panel/ha-state-control-alarm_control_panel-modes.ts +++ b/src/state-control/alarm_control_panel/ha-state-control-alarm_control_panel-modes.ts @@ -57,13 +57,13 @@ export class HaStateControlAlarmControlPanelModes extends LitElement { ) { const disarm = mode === "disarmed"; - const AlarmControlPanelRegistryEntry = + const alarmControlPanelRegistryEntry = await getExtendedEntityRegistryEntry( this.hass, this.stateObj.entity_id ).catch(() => undefined); const defaultCode = - AlarmControlPanelRegistryEntry?.options?.alarm_control_panel + alarmControlPanelRegistryEntry?.options?.alarm_control_panel ?.default_code; if (!defaultCode) { @@ -73,7 +73,7 @@ export class HaStateControlAlarmControlPanelModes extends LitElement { `ui.card.alarm_control_panel.${disarm ? "disarm" : "arm"}` ), submitText: this.hass!.localize( - `ui.card.alarm_control_panel.${disarm ? "disarmn" : "arm"}` + `ui.card.alarm_control_panel.${disarm ? "disarm" : "arm"}` ), }); if (response == null) { From cc8221ea2bd9de4b66574d23be9bc579dc0f1410 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Mon, 6 May 2024 18:42:03 +0000 Subject: [PATCH 04/11] Review --- .../lovelace/cards/hui-alarm-panel-card.ts | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/panels/lovelace/cards/hui-alarm-panel-card.ts b/src/panels/lovelace/cards/hui-alarm-panel-card.ts index ecfce5fac28c..517ad4535930 100644 --- a/src/panels/lovelace/cards/hui-alarm-panel-card.ts +++ b/src/panels/lovelace/cards/hui-alarm-panel-card.ts @@ -30,6 +30,10 @@ import type { HomeAssistant } from "../../../types"; import { findEntities } from "../common/find-entities"; import { createEntityNotFoundWarning } from "../components/hui-warning"; import type { LovelaceCard } from "../types"; +import { + ExtEntityRegistryEntry, + getExtendedEntityRegistryEntry, +} from "../../../data/entity_registry"; import { AlarmPanelCardConfig, AlarmPanelCardConfigState } from "./types"; const BUTTONS = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "", "0", "clear"]; @@ -99,6 +103,8 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard { @state() private _config?: AlarmPanelCardConfig; + @state() private _entry?: ExtEntityRegistryEntry | null; + @query("#alarmCode") private _input?: HaTextField; public async getCardSize(): Promise { @@ -143,6 +149,7 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard { ) { applyThemesOnElement(this, this.hass.themes, this._config.theme); } + this._loadEntityRegistryEntry(); } protected shouldUpdate(changedProps: PropertyValues): boolean { @@ -165,6 +172,20 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard { ); } + private async _loadEntityRegistryEntry() { + if (!this._config!.entity) { + return; + } + try { + this._entry = await getExtendedEntityRegistryEntry( + this.hass!, + this._config!.entity + ); + } catch (e) { + this._entry = null; + } + } + protected render() { if (!this._config || !this.hass) { return nothing; @@ -184,6 +205,8 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard { const stateLabel = this._stateDisplay(stateObj.state); + const defaultCode = this._entry?.options?.alarm_control_panel?.default_code; + return html`

@@ -222,7 +245,7 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard { ` )} - ${!stateObj.attributes.code_format + ${!stateObj.attributes.code_format || defaultCode! ? nothing : html` `} - ${stateObj.attributes.code_format !== FORMAT_NUMBER + ${stateObj.attributes.code_format !== FORMAT_NUMBER || defaultCode! ? nothing : html`
From e9514d201054f41ae7271687409434902dda0c5a Mon Sep 17 00:00:00 2001 From: G Johansson Date: Tue, 7 May 2024 18:24:19 +0000 Subject: [PATCH 05/11] fix render --- src/panels/lovelace/cards/hui-alarm-panel-card.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/panels/lovelace/cards/hui-alarm-panel-card.ts b/src/panels/lovelace/cards/hui-alarm-panel-card.ts index 517ad4535930..868e4afc494f 100644 --- a/src/panels/lovelace/cards/hui-alarm-panel-card.ts +++ b/src/panels/lovelace/cards/hui-alarm-panel-card.ts @@ -149,7 +149,6 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard { ) { applyThemesOnElement(this, this.hass.themes, this._config.theme); } - this._loadEntityRegistryEntry(); } protected shouldUpdate(changedProps: PropertyValues): boolean { @@ -186,6 +185,11 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard { } } + public async firstUpdated(changedProps: PropertyValues) { + super.firstUpdated(changedProps); + await this._loadEntityRegistryEntry(); + } + protected render() { if (!this._config || !this.hass) { return nothing; From 3121f26e11f6ba11fd0eb038fc3346816dfbf9f6 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Sun, 26 May 2024 14:28:41 +0000 Subject: [PATCH 06/11] review comments --- src/panels/lovelace/cards/hui-alarm-panel-card.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/panels/lovelace/cards/hui-alarm-panel-card.ts b/src/panels/lovelace/cards/hui-alarm-panel-card.ts index 868e4afc494f..c7965a782799 100644 --- a/src/panels/lovelace/cards/hui-alarm-panel-card.ts +++ b/src/panels/lovelace/cards/hui-alarm-panel-card.ts @@ -249,7 +249,7 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard { ` )}
- ${!stateObj.attributes.code_format || defaultCode! + ${!stateObj.attributes.code_format || defaultCode ? nothing : html` `} - ${stateObj.attributes.code_format !== FORMAT_NUMBER || defaultCode! + ${stateObj.attributes.code_format !== FORMAT_NUMBER || defaultCode ? nothing : html`
From 13880e5769bbddb5feafd07ccdf5424fd4087414 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Sun, 26 May 2024 14:42:35 +0000 Subject: [PATCH 07/11] handle update --- src/panels/lovelace/cards/hui-alarm-panel-card.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/panels/lovelace/cards/hui-alarm-panel-card.ts b/src/panels/lovelace/cards/hui-alarm-panel-card.ts index c7965a782799..ca2327c1193b 100644 --- a/src/panels/lovelace/cards/hui-alarm-panel-card.ts +++ b/src/panels/lovelace/cards/hui-alarm-panel-card.ts @@ -140,7 +140,13 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard { const oldConfig = changedProps.get("_config") as | AlarmPanelCardConfig | undefined; + const entityId = this._config?.entity; + if (entityId) { + if (oldHass?.entities[entityId] !== this.hass?.entities[entityId]) { + this._loadEntityRegistryEntry(); + } + } if ( !oldHass || !oldConfig || From a96db68722eedc09591a3e133d9de9182a57a7ac Mon Sep 17 00:00:00 2001 From: G Johansson Date: Sun, 26 May 2024 15:55:16 +0000 Subject: [PATCH 08/11] Fix --- src/panels/lovelace/cards/hui-alarm-panel-card.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/panels/lovelace/cards/hui-alarm-panel-card.ts b/src/panels/lovelace/cards/hui-alarm-panel-card.ts index ca2327c1193b..c5b11775b42a 100644 --- a/src/panels/lovelace/cards/hui-alarm-panel-card.ts +++ b/src/panels/lovelace/cards/hui-alarm-panel-card.ts @@ -140,11 +140,12 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard { const oldConfig = changedProps.get("_config") as | AlarmPanelCardConfig | undefined; - const entityId = this._config?.entity; - - if (entityId) { - if (oldHass?.entities[entityId] !== this.hass?.entities[entityId]) { - this._loadEntityRegistryEntry(); + if (changedProps.has("hass")) { + const entityId = this._config?.entity; + if (entityId) { + if (oldHass?.entities[entityId] !== this.hass?.entities[entityId]) { + this._loadEntityRegistryEntry(); + } } } if ( From db434f606ab6e2896f6c1e014333f0ab6b44eaf1 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Sun, 26 May 2024 18:39:22 +0000 Subject: [PATCH 09/11] subscribe entity reg --- .../lovelace/cards/hui-alarm-panel-card.ts | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/src/panels/lovelace/cards/hui-alarm-panel-card.ts b/src/panels/lovelace/cards/hui-alarm-panel-card.ts index c5b11775b42a..1e4456008518 100644 --- a/src/panels/lovelace/cards/hui-alarm-panel-card.ts +++ b/src/panels/lovelace/cards/hui-alarm-panel-card.ts @@ -1,4 +1,4 @@ -import { HassEntity } from "home-assistant-js-websocket"; +import { HassEntity, UnsubscribeFunc } from "home-assistant-js-websocket"; import { CSSResultGroup, LitElement, @@ -33,6 +33,7 @@ import type { LovelaceCard } from "../types"; import { ExtEntityRegistryEntry, getExtendedEntityRegistryEntry, + subscribeEntityRegistry, } from "../../../data/entity_registry"; import { AlarmPanelCardConfig, AlarmPanelCardConfigState } from "./types"; @@ -107,6 +108,20 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard { @query("#alarmCode") private _input?: HaTextField; + private _unsubEntityRegistry?: UnsubscribeFunc; + + public connectedCallback() { + super.connectedCallback(); + } + + public disconnectedCallback() { + super.disconnectedCallback(); + if (this._unsubEntityRegistry) { + this._unsubEntityRegistry(); + this._unsubEntityRegistry = undefined; + } + } + public async getCardSize(): Promise { if (!this._config || !this.hass) { return 9; @@ -140,14 +155,7 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard { const oldConfig = changedProps.get("_config") as | AlarmPanelCardConfig | undefined; - if (changedProps.has("hass")) { - const entityId = this._config?.entity; - if (entityId) { - if (oldHass?.entities[entityId] !== this.hass?.entities[entityId]) { - this._loadEntityRegistryEntry(); - } - } - } + if ( !oldHass || !oldConfig || @@ -183,9 +191,18 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard { return; } try { - this._entry = await getExtendedEntityRegistryEntry( - this.hass!, - this._config!.entity + this._unsubEntityRegistry = subscribeEntityRegistry( + this.hass!.connection, + async (entries) => { + if ( + entries.some((entry) => entry.entity_id === this._config!.entity) + ) { + this._entry = await getExtendedEntityRegistryEntry( + this.hass!, + this._config!.entity + ); + } + } ); } catch (e) { this._entry = null; From e4fe88a5119c2a4af64240a867e1412d3faa0f53 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Wed, 29 May 2024 09:47:56 +0200 Subject: [PATCH 10/11] Fix subscription --- .../lovelace/cards/hui-alarm-panel-card.ts | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/panels/lovelace/cards/hui-alarm-panel-card.ts b/src/panels/lovelace/cards/hui-alarm-panel-card.ts index 1e4456008518..225f30cea6e9 100644 --- a/src/panels/lovelace/cards/hui-alarm-panel-card.ts +++ b/src/panels/lovelace/cards/hui-alarm-panel-card.ts @@ -112,14 +112,12 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard { public connectedCallback() { super.connectedCallback(); + this._subscribeEntityEntry(); } public disconnectedCallback() { super.disconnectedCallback(); - if (this._unsubEntityRegistry) { - this._unsubEntityRegistry(); - this._unsubEntityRegistry = undefined; - } + this._unsubscribeEntityRegistry(); } public async getCardSize(): Promise { @@ -144,6 +142,7 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard { } this._config = { ...config }; + this._subscribeEntityEntry(); } protected updated(changedProps: PropertyValues): void { @@ -186,8 +185,15 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard { ); } - private async _loadEntityRegistryEntry() { - if (!this._config!.entity) { + private async _unsubscribeEntityRegistry() { + if (this._unsubEntityRegistry) { + this._unsubEntityRegistry(); + this._unsubEntityRegistry = undefined; + } + } + + private async _subscribeEntityEntry() { + if (!this._config?.entity) { return; } try { @@ -209,11 +215,6 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard { } } - public async firstUpdated(changedProps: PropertyValues) { - super.firstUpdated(changedProps); - await this._loadEntityRegistryEntry(); - } - protected render() { if (!this._config || !this.hass) { return nothing; From 120283de2c78f4ec5a6aab9c9b6f0505fb8bb176 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Wed, 29 May 2024 10:01:30 +0200 Subject: [PATCH 11/11] Check default code in more info and card feature --- src/data/alarm_control_panel.ts | 51 ++++++++++++++++++- .../controls/more-info-alarm_control_panel.ts | 30 ++++------- .../hui-alarm-modes-card-feature.ts | 34 +------------ ...state-control-alarm_control_panel-modes.ts | 46 +---------------- 4 files changed, 64 insertions(+), 97 deletions(-) diff --git a/src/data/alarm_control_panel.ts b/src/data/alarm_control_panel.ts index 64a8d53feeb0..acde378a1335 100644 --- a/src/data/alarm_control_panel.ts +++ b/src/data/alarm_control_panel.ts @@ -10,8 +10,10 @@ import { HassEntityAttributeBase, HassEntityBase, } from "home-assistant-js-websocket"; -import { HomeAssistant } from "../types"; import { supportsFeature } from "../common/entity/supports-feature"; +import { showEnterCodeDialog } from "../dialogs/enter-code/show-enter-code-dialog"; +import { HomeAssistant } from "../types"; +import { getExtendedEntityRegistryEntry } from "./entity_registry"; export const FORMAT_TEXT = "text"; export const FORMAT_NUMBER = "number"; @@ -103,3 +105,50 @@ export const supportedAlarmModes = (stateObj: AlarmControlPanelEntity) => const feature = ALARM_MODES[mode].feature; return !feature || supportsFeature(stateObj, feature); }); + +export const setProtectedAlarmControlPanelMode = async ( + element: HTMLElement, + hass: HomeAssistant, + stateObj: AlarmControlPanelEntity, + mode: AlarmMode +) => { + const { service } = ALARM_MODES[mode]; + + let code: string | undefined; + + if ( + (mode !== "disarmed" && + stateObj.attributes.code_arm_required && + stateObj.attributes.code_format) || + (mode === "disarmed" && stateObj.attributes.code_format) + ) { + const entry = await getExtendedEntityRegistryEntry( + hass, + stateObj.entity_id + ).catch(() => undefined); + const defaultCode = entry?.options?.alarm_control_panel?.default_code; + + if (!defaultCode) { + const disarm = mode === "disarmed"; + + const response = await showEnterCodeDialog(element, { + codeFormat: stateObj.attributes.code_format, + title: hass.localize( + `ui.card.alarm_control_panel.${disarm ? "disarm" : "arm"}` + ), + submitText: hass.localize( + `ui.card.alarm_control_panel.${disarm ? "disarm" : "arm"}` + ), + }); + if (response == null) { + throw new Error("Code dialog closed"); + } + code = response; + } + } + + await hass.callService("alarm_control_panel", service, { + entity_id: stateObj.entity_id, + code, + }); +}; diff --git a/src/dialogs/more-info/controls/more-info-alarm_control_panel.ts b/src/dialogs/more-info/controls/more-info-alarm_control_panel.ts index 3a28b4f4b757..430565433ffa 100644 --- a/src/dialogs/more-info/controls/more-info-alarm_control_panel.ts +++ b/src/dialogs/more-info/controls/more-info-alarm_control_panel.ts @@ -4,10 +4,12 @@ import { styleMap } from "lit/directives/style-map"; import { stateColorCss } from "../../../common/entity/state_color"; import "../../../components/ha-control-button"; import "../../../components/ha-state-icon"; -import { AlarmControlPanelEntity } from "../../../data/alarm_control_panel"; +import { + AlarmControlPanelEntity, + setProtectedAlarmControlPanelMode, +} from "../../../data/alarm_control_panel"; import "../../../state-control/alarm_control_panel/ha-state-control-alarm_control_panel-modes"; import type { HomeAssistant } from "../../../types"; -import { showEnterCodeDialog } from "../../enter-code/show-enter-code-dialog"; import "../components/ha-more-info-state-header"; import { moreInfoControlStyle } from "../components/more-info-control-style"; @@ -18,24 +20,12 @@ class MoreInfoAlarmControlPanel extends LitElement { @property({ attribute: false }) public stateObj?: AlarmControlPanelEntity; private async _disarm() { - let code: string | undefined; - - if (this.stateObj!.attributes.code_format) { - const response = await showEnterCodeDialog(this, { - codeFormat: this.stateObj!.attributes.code_format, - title: this.hass.localize("ui.card.alarm_control_panel.disarm"), - submitText: this.hass.localize("ui.card.alarm_control_panel.disarm"), - }); - if (response == null) { - return; - } - code = response; - } - - this.hass.callService("alarm_control_panel", "alarm_disarm", { - entity_id: this.stateObj!.entity_id, - code, - }); + setProtectedAlarmControlPanelMode( + this, + this.hass, + this.stateObj!, + "disarmed" + ); } protected render() { diff --git a/src/panels/lovelace/card-features/hui-alarm-modes-card-feature.ts b/src/panels/lovelace/card-features/hui-alarm-modes-card-feature.ts index 926fdcea2509..9b8ef39c997a 100644 --- a/src/panels/lovelace/card-features/hui-alarm-modes-card-feature.ts +++ b/src/panels/lovelace/card-features/hui-alarm-modes-card-feature.ts @@ -16,10 +16,10 @@ import { ALARM_MODES, AlarmControlPanelEntity, AlarmMode, + setProtectedAlarmControlPanelMode, supportedAlarmModes, } from "../../../data/alarm_control_panel"; import { UNAVAILABLE } from "../../../data/entity"; -import { showEnterCodeDialog } from "../../../dialogs/enter-code/show-enter-code-dialog"; import { HomeAssistant } from "../../../types"; import { LovelaceCardFeature, LovelaceCardFeatureEditor } from "../types"; import { filterModes } from "./common/filter-modes"; @@ -115,37 +115,7 @@ class HuiAlarmModeCardFeature } private async _setMode(mode: AlarmMode) { - const { service } = ALARM_MODES[mode]; - - let code: string | undefined; - - if ( - (mode !== "disarmed" && - this.stateObj!.attributes.code_arm_required && - this.stateObj!.attributes.code_format) || - (mode === "disarmed" && this.stateObj!.attributes.code_format) - ) { - const disarm = mode === "disarmed"; - - const response = await showEnterCodeDialog(this, { - codeFormat: this.stateObj!.attributes.code_format, - title: this.hass!.localize( - `ui.card.alarm_control_panel.${disarm ? "disarm" : "arm"}` - ), - submitText: this.hass!.localize( - `ui.card.alarm_control_panel.${disarm ? "disarm" : "arm"}` - ), - }); - if (response == null) { - throw new Error("cancel"); - } - code = response; - } - - await this.hass!.callService("alarm_control_panel", service, { - entity_id: this.stateObj!.entity_id, - code, - }); + setProtectedAlarmControlPanelMode(this, this.hass!, this.stateObj!, mode); } protected render(): TemplateResult | null { diff --git a/src/state-control/alarm_control_panel/ha-state-control-alarm_control_panel-modes.ts b/src/state-control/alarm_control_panel/ha-state-control-alarm_control_panel-modes.ts index be5408ce0c52..c93ac8b013e4 100644 --- a/src/state-control/alarm_control_panel/ha-state-control-alarm_control_panel-modes.ts +++ b/src/state-control/alarm_control_panel/ha-state-control-alarm_control_panel-modes.ts @@ -11,11 +11,10 @@ import { ALARM_MODES, AlarmControlPanelEntity, AlarmMode, + setProtectedAlarmControlPanelMode, } from "../../data/alarm_control_panel"; import { UNAVAILABLE } from "../../data/entity"; -import { showEnterCodeDialog } from "../../dialogs/enter-code/show-enter-code-dialog"; import { HomeAssistant } from "../../types"; -import { getExtendedEntityRegistryEntry } from "../../data/entity_registry"; @customElement("ha-state-control-alarm_control_panel-modes") export class HaStateControlAlarmControlPanelModes extends LitElement { @@ -45,48 +44,7 @@ export class HaStateControlAlarmControlPanelModes extends LitElement { } private async _setMode(mode: AlarmMode) { - const { service } = ALARM_MODES[mode]; - - let code: string | undefined; - - if ( - (mode !== "disarmed" && - this.stateObj!.attributes.code_arm_required && - this.stateObj!.attributes.code_format) || - (mode === "disarmed" && this.stateObj!.attributes.code_format) - ) { - const disarm = mode === "disarmed"; - - const alarmControlPanelRegistryEntry = - await getExtendedEntityRegistryEntry( - this.hass, - this.stateObj.entity_id - ).catch(() => undefined); - const defaultCode = - alarmControlPanelRegistryEntry?.options?.alarm_control_panel - ?.default_code; - - if (!defaultCode) { - const response = await showEnterCodeDialog(this, { - codeFormat: this.stateObj!.attributes.code_format, - title: this.hass!.localize( - `ui.card.alarm_control_panel.${disarm ? "disarm" : "arm"}` - ), - submitText: this.hass!.localize( - `ui.card.alarm_control_panel.${disarm ? "disarm" : "arm"}` - ), - }); - if (response == null) { - throw new Error("cancel"); - } - code = response; - } - } - - await this.hass!.callService("alarm_control_panel", service, { - entity_id: this.stateObj!.entity_id, - code, - }); + setProtectedAlarmControlPanelMode(this, this.hass!, this.stateObj!, mode); } private async _valueChanged(ev: CustomEvent) {