Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(alarm): show keypad only for disarming #786

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/cards/alarm-control-panel.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ All the options are available in the lovelace editor but you can use `yaml` if y
| `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` | `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 |
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -7,20 +7,21 @@ 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?: string;
};

export const alarmControlPanelCardCardConfigStruct = assign(
lovelaceCardConfigStruct,
assign(entitySharedConfigStruct, appearanceSharedConfigStruct, actionsSharedConfigStruct),
object({
states: optional(array()),
show_keypad: optional(boolean()),
show_keypad: optional(enums(ALARM_CONTROL_PANEL_CARD_SHOW_KEYPAD_OPTIONS)),
})
);
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +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: {} } },
...computeActionsFormSchema(actions),
]);
const computeSchema = memoizeOne(
(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" } },
...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 {
Expand All @@ -59,7 +72,7 @@ export class SwitchCardEditor extends MushroomBaseElement implements LovelaceCar
return nothing;
}

const schema = computeSchema(this.hass!.localize);
const schema = computeSchema(this.hass!.localize, setupCustomlocalize(this.hass!));

return html`
<ha-form
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) && (this._config?.show_keypad === "always" || (this._config?.show_keypad_disarm === "disarm" && !isDisarmed(entity)));
}

protected render() {
Expand Down
2 changes: 2 additions & 0 deletions src/cards/alarm-control-panel-card/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export const ALARM_CONTROL_PANEL_CARD_STATE_COLOR = {

export const ALARM_CONTROL_PANEL_CARD_DEFAULT_STATE_COLOR = "var(--rgb-grey)";

export const ALARM_CONTROL_PANEL_CARD_SHOW_KEYPAD_OPTIONS = ["always", "never", "disarm"];

export const ALARM_CONTROL_PANEL_CARD_STATE_SERVICE = {
disarmed: "alarm_disarm",
armed_away: "alarm_arm_away",
Expand Down
7 changes: 5 additions & 2 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@
"show_tilt_position_control": "Tilt control?"
},
"alarm_control_panel": {
"show_keypad": "Show keypad"
"show_keypad": "Show keypad",
"always": "Always",
"never": "Never",
"disarm": "Disarmed"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"disarm": "Disarmed"
"disarm": "Disarm only"

},
"template": {
"primary": "Primary information",
Expand Down Expand Up @@ -177,4 +180,4 @@
"card": {
"not_found": "Entity not found"
}
}
}
7 changes: 5 additions & 2 deletions src/translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@
"show_tilt_position_control": "¿Control de inclinación?"
},
"alarm_control_panel": {
"show_keypad": "Mostrar teclado"
"show_keypad": "Mostrar teclado",
"always": "Siempre",
"never": "Nunca",
"disarm": "Desarmado"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be "disarm only", not "disarmed"

},
"template": {
"primary": "Información primaria",
Expand Down Expand Up @@ -165,4 +168,4 @@
}
}
}
}
}
Loading