From 4085bf9fee5735c9329dfc1f092949cc524614a5 Mon Sep 17 00:00:00 2001 From: rogelio-o Date: Tue, 25 Oct 2022 19:52:44 +0200 Subject: [PATCH 1/3] feat(alarm): show keypad only for disarming --- docs/cards/alarm-control-panel.md | 31 ++++++++++--------- .../alarm-control-panel-card-config.ts | 2 ++ .../alarm-control-panel-card-editor.ts | 1 + .../alarm-control-panel-card.ts | 3 +- src/translations/en.json | 5 +-- src/translations/es.json | 5 +-- 6 files changed, 27 insertions(+), 20 deletions(-) diff --git a/docs/cards/alarm-control-panel.md b/docs/cards/alarm-control-panel.md index 15151d839..f54c83318 100644 --- a/docs/cards/alarm-control-panel.md +++ b/docs/cards/alarm-control-panel.md @@ -11,18 +11,19 @@ An alarm control panel card allows you to control a alarm panel entity. All the options are available in the lovelace editor but you can use `yaml` if you want. -| Name | Type | Default | Description | -| :------------------ | :-------------------------------------------------- | :----------------------------- | :---------------------------------------------------------------------------------- | -| `entity` | string | Required | Alarm control panel entity | -| `icon` | string | Optional | Custom icon | -| `name` | string | Optional | Custom name | -| `layout` | string | Optional | Layout of the card. Vertical, horizontal and default layout are supported | -| `fill_container` | boolean | `false` | Fill container or not. Useful when card is in a grid, vertical or horizontal layout | -| `primary_info` | `name` `state` `last-changed` `last-updated` `none` | `name` | Info to show as primary info | -| `secondary_info` | `name` `state` `last-changed` `last-updated` `none` | `state` | Info to show as secondary info | -| `icon_type` | `icon` `entity-picture` `none` | `icon` | Type of icon to display | -| `states` | list | `["armed_home", "armed_away"]` | List of arm states to display | -| `show_keypad` | boolean | `false` | Show the keypad | -| `tap_action` | action | `more-info` | Home assistant action to perform on tap | -| `hold_action` | action | `more-info` | Home assistant action to perform on hold | -| `double_tap_action` | action | `more-info` | Home assistant action to perform on double_tap | +| Name | Type | Default | Description | +| :------------------- | :-------------------------------------------------- | :----------------------------- | :---------------------------------------------------------------------------------- | +| `entity` | string | Required | Alarm control panel entity | +| `icon` | string | Optional | Custom icon | +| `name` | string | Optional | Custom name | +| `layout` | string | Optional | Layout of the card. Vertical, horizontal and default layout are supported | +| `fill_container` | boolean | `false` | Fill container or not. Useful when card is in a grid, vertical or horizontal layout | +| `primary_info` | `name` `state` `last-changed` `last-updated` `none` | `name` | Info to show as primary info | +| `secondary_info` | `name` `state` `last-changed` `last-updated` `none` | `state` | Info to show as secondary info | +| `icon_type` | `icon` `entity-picture` `none` | `icon` | Type of icon to display | +| `states` | list | `["armed_home", "armed_away"]` | List of arm states to display | +| `show_keypad` | boolean | `false` | Show the keypad | +| `show_keypad_disarm` | boolean | `false` | Show the keypad for disarming | +| `tap_action` | action | `more-info` | Home assistant action to perform on tap | +| `hold_action` | action | `more-info` | Home assistant action to perform on hold | +| `double_tap_action` | action | `more-info` | Home assistant action to perform on double_tap | diff --git a/src/cards/alarm-control-panel-card/alarm-control-panel-card-config.ts b/src/cards/alarm-control-panel-card/alarm-control-panel-card-config.ts index 73080779e..a0e983300 100644 --- a/src/cards/alarm-control-panel-card/alarm-control-panel-card-config.ts +++ b/src/cards/alarm-control-panel-card/alarm-control-panel-card-config.ts @@ -14,6 +14,7 @@ export type AlarmControlPanelCardConfig = LovelaceCardConfig & ActionsSharedConfig & { states?: string[]; show_keypad?: boolean; + show_keypad_disarm?: boolean; }; export const alarmControlPanelCardCardConfigStruct = assign( @@ -22,5 +23,6 @@ export const alarmControlPanelCardCardConfigStruct = assign( object({ states: optional(array()), show_keypad: optional(boolean()), + show_keypad_disarm: optional(boolean()), }) ); diff --git a/src/cards/alarm-control-panel-card/alarm-control-panel-card-editor.ts b/src/cards/alarm-control-panel-card/alarm-control-panel-card-editor.ts index b2f5c3c7f..780ce9461 100644 --- a/src/cards/alarm-control-panel-card/alarm-control-panel-card-editor.ts +++ b/src/cards/alarm-control-panel-card/alarm-control-panel-card-editor.ts @@ -37,6 +37,7 @@ const computeSchema = memoizeOne((localize: LocalizeFunc): HaFormSchema[] => [ ]) as [string, string][], }, { name: "show_keypad", selector: { boolean: {} } }, + { name: "show_keypad_disarm", selector: { boolean: {} } }, ...computeActionsFormSchema(actions), ]); diff --git a/src/cards/alarm-control-panel-card/alarm-control-panel-card.ts b/src/cards/alarm-control-panel-card/alarm-control-panel-card.ts index 667c4c1e3..7d9a7840d 100644 --- a/src/cards/alarm-control-panel-card/alarm-control-panel-card.ts +++ b/src/cards/alarm-control-panel-card/alarm-control-panel-card.ts @@ -146,9 +146,10 @@ export class AlarmControlPanelCard extends MushroomBaseCard implements LovelaceC private get _hasCode(): boolean { const entityId = this._config?.entity; if (!entityId) return false; + const entity = this.hass.states[entityId]; const stateObj = this.hass.states[entityId] as HassEntity | undefined; if (!stateObj) return false; - return hasCode(stateObj) && Boolean(this._config?.show_keypad); + return hasCode(stateObj) && (Boolean(this._config?.show_keypad) || (Boolean(this._config?.show_keypad_disarm) && !isDisarmed(entity))); } protected render() { diff --git a/src/translations/en.json b/src/translations/en.json index da549f595..ff1114f51 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -71,7 +71,8 @@ "show_tilt_position_control": "Tilt control?" }, "alarm_control_panel": { - "show_keypad": "Show keypad" + "show_keypad": "Show keypad", + "show_keypad_disarm": "Show keypad for disarming" }, "template": { "primary": "Primary information", @@ -177,4 +178,4 @@ "card": { "not_found": "Entity not found" } -} +} \ No newline at end of file diff --git a/src/translations/es.json b/src/translations/es.json index c8d034cef..53a8e6085 100644 --- a/src/translations/es.json +++ b/src/translations/es.json @@ -71,7 +71,8 @@ "show_tilt_position_control": "¿Control de inclinación?" }, "alarm_control_panel": { - "show_keypad": "Mostrar teclado" + "show_keypad": "Mostrar teclado", + "show_keypad_disarm": "Mostrar teclado para desarmar" }, "template": { "primary": "Información primaria", @@ -165,4 +166,4 @@ } } } -} +} \ No newline at end of file From 3c20e1afbf9f15b136532d51813905db454afe64 Mon Sep 17 00:00:00 2001 From: rogelio-o Date: Thu, 27 Oct 2022 20:18:39 +0200 Subject: [PATCH 2/3] Change approach to use select instead of two booleans --- docs/cards/alarm-control-panel.md | 31 +++++----- .../alarm-control-panel-card-config.ts | 9 ++- .../alarm-control-panel-card-editor.ts | 57 ++++++++++++------- .../alarm-control-panel-card.ts | 2 +- src/cards/alarm-control-panel-card/const.ts | 2 + src/translations/en.json | 4 +- src/translations/es.json | 4 +- 7 files changed, 66 insertions(+), 43 deletions(-) diff --git a/docs/cards/alarm-control-panel.md b/docs/cards/alarm-control-panel.md index f54c83318..25e4b4f56 100644 --- a/docs/cards/alarm-control-panel.md +++ b/docs/cards/alarm-control-panel.md @@ -11,19 +11,18 @@ An alarm control panel card allows you to control a alarm panel entity. All the options are available in the lovelace editor but you can use `yaml` if you want. -| Name | Type | Default | Description | -| :------------------- | :-------------------------------------------------- | :----------------------------- | :---------------------------------------------------------------------------------- | -| `entity` | string | Required | Alarm control panel entity | -| `icon` | string | Optional | Custom icon | -| `name` | string | Optional | Custom name | -| `layout` | string | Optional | Layout of the card. Vertical, horizontal and default layout are supported | -| `fill_container` | boolean | `false` | Fill container or not. Useful when card is in a grid, vertical or horizontal layout | -| `primary_info` | `name` `state` `last-changed` `last-updated` `none` | `name` | Info to show as primary info | -| `secondary_info` | `name` `state` `last-changed` `last-updated` `none` | `state` | Info to show as secondary info | -| `icon_type` | `icon` `entity-picture` `none` | `icon` | Type of icon to display | -| `states` | list | `["armed_home", "armed_away"]` | List of arm states to display | -| `show_keypad` | boolean | `false` | Show the keypad | -| `show_keypad_disarm` | boolean | `false` | Show the keypad for disarming | -| `tap_action` | action | `more-info` | Home assistant action to perform on tap | -| `hold_action` | action | `more-info` | Home assistant action to perform on hold | -| `double_tap_action` | action | `more-info` | Home assistant action to perform on double_tap | +| Name | Type | Default | Description | +| :------------------ | :-------------------------------------------------- | :----------------------------- | :---------------------------------------------------------------------------------- | +| `entity` | string | Required | Alarm control panel entity | +| `icon` | string | Optional | Custom icon | +| `name` | string | Optional | Custom name | +| `layout` | string | Optional | Layout of the card. Vertical, horizontal and default layout are supported | +| `fill_container` | boolean | `false` | Fill container or not. Useful when card is in a grid, vertical or horizontal layout | +| `primary_info` | `name` `state` `last-changed` `last-updated` `none` | `name` | Info to show as primary info | +| `secondary_info` | `name` `state` `last-changed` `last-updated` `none` | `state` | Info to show as secondary info | +| `icon_type` | `icon` `entity-picture` `none` | `icon` | Type of icon to display | +| `states` | list | `["armed_home", "armed_away"]` | List of arm states to display | +| `show_keypad` | `always`, `never`, `disarm` | `never` | Show the keypad | +| `tap_action` | action | `more-info` | Home assistant action to perform on tap | +| `hold_action` | action | `more-info` | Home assistant action to perform on hold | +| `double_tap_action` | action | `more-info` | Home assistant action to perform on double_tap | diff --git a/src/cards/alarm-control-panel-card/alarm-control-panel-card-config.ts b/src/cards/alarm-control-panel-card/alarm-control-panel-card-config.ts index a0e983300..59352ef07 100644 --- a/src/cards/alarm-control-panel-card/alarm-control-panel-card-config.ts +++ b/src/cards/alarm-control-panel-card/alarm-control-panel-card-config.ts @@ -1,4 +1,4 @@ -import { array, assign, boolean, object, optional } from "superstruct"; +import { array, assign, enums, object, optional } from "superstruct"; import { LovelaceCardConfig } from "../../ha"; import { ActionsSharedConfig, actionsSharedConfigStruct } from "../../shared/config/actions-config"; import { @@ -7,14 +7,14 @@ import { } from "../../shared/config/appearance-config"; import { EntitySharedConfig, entitySharedConfigStruct } from "../../shared/config/entity-config"; import { lovelaceCardConfigStruct } from "../../shared/config/lovelace-card-config"; +import { ALARM_CONTROL_PANEL_CARD_SHOW_KEYPAD_OPTIONS } from "./const"; export type AlarmControlPanelCardConfig = LovelaceCardConfig & EntitySharedConfig & AppearanceSharedConfig & ActionsSharedConfig & { states?: string[]; - show_keypad?: boolean; - show_keypad_disarm?: boolean; + show_keypad?: string; }; export const alarmControlPanelCardCardConfigStruct = assign( @@ -22,7 +22,6 @@ export const alarmControlPanelCardCardConfigStruct = assign( assign(entitySharedConfigStruct, appearanceSharedConfigStruct, actionsSharedConfigStruct), object({ states: optional(array()), - show_keypad: optional(boolean()), - show_keypad_disarm: optional(boolean()), + show_keypad: optional(enums(ALARM_CONTROL_PANEL_CARD_SHOW_KEYPAD_OPTIONS)), }) ); diff --git a/src/cards/alarm-control-panel-card/alarm-control-panel-card-editor.ts b/src/cards/alarm-control-panel-card/alarm-control-panel-card-editor.ts index 780ce9461..a56a1b08f 100644 --- a/src/cards/alarm-control-panel-card/alarm-control-panel-card-editor.ts +++ b/src/cards/alarm-control-panel-card/alarm-control-panel-card-editor.ts @@ -15,31 +15,43 @@ import { AlarmControlPanelCardConfig, alarmControlPanelCardCardConfigStruct, } from "./alarm-control-panel-card-config"; -import { ALARM_CONTROl_PANEL_CARD_EDITOR_NAME, ALARM_CONTROl_PANEL_ENTITY_DOMAINS } from "./const"; +import { + ALARM_CONTROl_PANEL_CARD_EDITOR_NAME, + ALARM_CONTROl_PANEL_ENTITY_DOMAINS, + ALARM_CONTROL_PANEL_CARD_SHOW_KEYPAD_OPTIONS, +} from "./const"; const actions: UiAction[] = ["more-info", "navigate", "url", "call-service", "assist", "none"]; const states = ["armed_home", "armed_away", "armed_night", "armed_vacation", "armed_custom_bypass"]; -const ALARM_CONTROL_PANEL_LABELS = ["show_keypad"]; +const ALARM_CONTROL_PANEL_LABELS = ["show_keypad", "always", "never"]; -const computeSchema = memoizeOne((localize: LocalizeFunc): HaFormSchema[] => [ - { name: "entity", selector: { entity: { domain: ALARM_CONTROl_PANEL_ENTITY_DOMAINS } } }, - { name: "name", selector: { text: {} } }, - { name: "icon", selector: { icon: {} }, context: { icon_entity: "entity" } }, - ...APPEARANCE_FORM_SCHEMA, - { - type: "multi_select", - name: "states", - options: states.map((state) => [ - state, - localize(`ui.card.alarm_control_panel.${state.replace("armed", "arm")}`), - ]) as [string, string][], - }, - { name: "show_keypad", selector: { boolean: {} } }, - { name: "show_keypad_disarm", selector: { boolean: {} } }, - ...computeActionsFormSchema(actions), -]); +const computeSchema = memoizeOne( + (localize: LocalizeFunc, customLocalize: LocalizeFunc, icon?: string): HaFormSchema[] => [ + { name: "entity", selector: { entity: { domain: ALARM_CONTROl_PANEL_ENTITY_DOMAINS } } }, + { name: "name", selector: { text: {} } }, + { name: "icon", selector: { icon: {} }, context: { icon_entity: "entity" } }, + ...APPEARANCE_FORM_SCHEMA, + { + type: "multi_select", + name: "states", + options: states.map((state) => [ + state, + localize(`ui.card.alarm_control_panel.${state.replace("armed", "arm")}`), + ]) as [string, string][], + }, + { + type: "select", + name: "show_keypad", + options: ALARM_CONTROL_PANEL_CARD_SHOW_KEYPAD_OPTIONS.map((state) => [ + state, + customLocalize(`editor.card.alarm_control_panel.${state}`), + ]) as [string, string][], + }, + ...computeActionsFormSchema(actions), + ] +); @customElement(ALARM_CONTROl_PANEL_CARD_EDITOR_NAME) export class SwitchCardEditor extends MushroomBaseElement implements LovelaceCardEditor { @@ -60,7 +72,14 @@ export class SwitchCardEditor extends MushroomBaseElement implements LovelaceCar return nothing; } +<<<<<<< HEAD const schema = computeSchema(this.hass!.localize); +======= + const entityState = this._config.entity ? this.hass.states[this._config.entity] : undefined; + const entityIcon = entityState ? stateIcon(entityState) : undefined; + const icon = this._config.icon || entityIcon; + const schema = computeSchema(this.hass!.localize, setupCustomlocalize(this.hass!), icon); +>>>>>>> af1b875 (Change approach to use select instead of two booleans) return html` Date: Sun, 15 Oct 2023 18:09:12 +0200 Subject: [PATCH 3/3] Fix rebase --- .../alarm-control-panel-card-editor.ts | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/cards/alarm-control-panel-card/alarm-control-panel-card-editor.ts b/src/cards/alarm-control-panel-card/alarm-control-panel-card-editor.ts index a56a1b08f..ab1e9e363 100644 --- a/src/cards/alarm-control-panel-card/alarm-control-panel-card-editor.ts +++ b/src/cards/alarm-control-panel-card/alarm-control-panel-card-editor.ts @@ -28,7 +28,7 @@ const states = ["armed_home", "armed_away", "armed_night", "armed_vacation", "ar const ALARM_CONTROL_PANEL_LABELS = ["show_keypad", "always", "never"]; const computeSchema = memoizeOne( - (localize: LocalizeFunc, customLocalize: LocalizeFunc, icon?: string): HaFormSchema[] => [ + (localize: LocalizeFunc, customLocalize: LocalizeFunc): HaFormSchema[] => [ { name: "entity", selector: { entity: { domain: ALARM_CONTROl_PANEL_ENTITY_DOMAINS } } }, { name: "name", selector: { text: {} } }, { name: "icon", selector: { icon: {} }, context: { icon_entity: "entity" } }, @@ -72,14 +72,7 @@ export class SwitchCardEditor extends MushroomBaseElement implements LovelaceCar return nothing; } -<<<<<<< HEAD - const schema = computeSchema(this.hass!.localize); -======= - const entityState = this._config.entity ? this.hass.states[this._config.entity] : undefined; - const entityIcon = entityState ? stateIcon(entityState) : undefined; - const icon = this._config.icon || entityIcon; - const schema = computeSchema(this.hass!.localize, setupCustomlocalize(this.hass!), icon); ->>>>>>> af1b875 (Change approach to use select instead of two booleans) + const schema = computeSchema(this.hass!.localize, setupCustomlocalize(this.hass!)); return html`