Skip to content

Commit

Permalink
Change approach to use select instead of two booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
rogelio-o committed Oct 27, 2022
1 parent 5a0f502 commit 0181308
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 46 deletions.
29 changes: 14 additions & 15 deletions docs/cards/alarm.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@ A alarm control panel card allow 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 |
| :------------------ | :-------------------------------------------------- | :----------------------------- | :---------------------------------------------------------------------------------- |
| `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 |
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,22 +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_disarm?: boolean;
show_keypad?: string;
};

export const alarmControlPanelCardCardConfigStruct = assign(
lovelaceCardConfigStruct,
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)),
})
);
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,43 @@ import {
alarmControlPanelCardCardConfigStruct,
AlarmControlPanelCardConfig,
} 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: Action[] = ["more-info", "navigate", "url", "call-service", "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, icon?: string): HaFormSchema[] => [
{ name: "entity", selector: { entity: { domain: ALARM_CONTROl_PANEL_ENTITY_DOMAINS } } },
{ name: "name", selector: { text: {} } },
{ name: "icon", selector: { icon: { placeholder: icon } } },
...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: { placeholder: icon } } },
...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 @@ -64,7 +76,7 @@ export class SwitchCardEditor extends MushroomBaseElement implements LovelaceCar
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, icon);
const schema = computeSchema(this.hass!.localize, setupCustomlocalize(this.hass!), icon);

return html`
<ha-form
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,10 @@ export class AlarmControlPanelCard extends MushroomBaseCard implements LovelaceC
const entity_id = this._config?.entity;
if (entity_id) {
const entity = this.hass.states[entity_id];
console.log(`Is disarmed: ${isDisarmed(entity)}`);
console.log(`Show keypad disarm: ${this._config?.show_keypad_disarm ?? false}`);
return (
hasCode(entity) &&
((this._config?.show_keypad ?? false) ||
((this._config?.show_keypad_disarm ?? false) && !isDisarmed(entity)))
(this._config?.show_keypad === "always" ||
(this._config?.show_keypad === "disarm" && !isDisarmed(entity)))
);
}
return false;
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
4 changes: 3 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@
},
"alarm_control_panel": {
"show_keypad": "Show keypad",
"show_keypad_disarm": "Show keypad for disarming"
"always": "Always",
"never": "Never",
"disarm": "Disarmed"
},
"template": {
"primary": "Primary information",
Expand Down
4 changes: 3 additions & 1 deletion src/translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
},
"alarm_control_panel": {
"show_keypad": "Mostrar teclado",
"show_keypad_disarm": "Mostrar teclado para desarmar"
"always": "Siempre",
"never": "Nunca",
"disarm": "Desarmado"
},
"template": {
"primary": "Información primaria",
Expand Down

0 comments on commit 0181308

Please sign in to comment.