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

Add default code to alarm_control_panel #20062

Merged
6 changes: 6 additions & 0 deletions src/data/entity_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -112,6 +116,7 @@ export interface SwitchAsXEntityOptions {
export interface EntityRegistryOptions {
number?: NumberEntityOptions;
sensor?: SensorEntityOptions;
alarm_control_panel?: AlarmControlPanelEntityOptions;
lock?: LockEntityOptions;
weather?: WeatherEntityOptions;
light?: LightEntityOptions;
Expand All @@ -134,6 +139,7 @@ export interface EntityRegistryEntryUpdateParams {
| SensorEntityOptions
| NumberEntityOptions
| LockEntityOptions
| AlarmControlPanelEntityOptions
| WeatherEntityOptions
| LightEntityOptions;
aliases?: string[];
Expand Down
27 changes: 27 additions & 0 deletions src/panels/config/entities/entity-registry-settings-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import {
updateDeviceRegistryEntry,
} from "../../../data/device_registry";
import {
AlarmControlPanelEntityOptions,
gjohansson-ST marked this conversation as resolved.
Show resolved Hide resolved
EntityRegistryEntry,
EntityRegistryEntryUpdateParams,
ExtEntityRegistryEntry,
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -583,6 +588,19 @@ export class EntityRegistrySettingsEditor extends LitElement {
></ha-textfield>
`
: ""}
${domain === "alarm_control_panel"
? html`
<ha-textfield
.value=${this._defaultCode == null ? "" : this._defaultCode}
.label=${this.hass.localize(
"ui.dialogs.entity_registry.editor.default_code"
)}
type="password"
.disabled=${this.disabled}
@input=${this._defaultcodeChanged}
></ha-textfield>
`
: ""}
${domain === "sensor" &&
this._deviceClass &&
stateObj?.attributes.unit_of_measurement &&
Expand Down Expand Up @@ -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 ||
Expand Down
40 changes: 37 additions & 3 deletions src/panels/lovelace/cards/hui-alarm-panel-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down Expand Up @@ -99,6 +103,8 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard {

@state() private _config?: AlarmPanelCardConfig;

@state() private _entry?: ExtEntityRegistryEntry | null;
gjohansson-ST marked this conversation as resolved.
Show resolved Hide resolved

@query("#alarmCode") private _input?: HaTextField;

public async getCardSize(): Promise<number> {
Expand Down Expand Up @@ -134,7 +140,14 @@ 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 ||
Expand Down Expand Up @@ -165,6 +178,25 @@ 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;
}
}
gjohansson-ST marked this conversation as resolved.
Show resolved Hide resolved

public async firstUpdated(changedProps: PropertyValues) {
super.firstUpdated(changedProps);
await this._loadEntityRegistryEntry();
}

protected render() {
if (!this._config || !this.hass) {
return nothing;
Expand All @@ -184,6 +216,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`
<ha-card>
<h1 class="card-header">
Expand Down Expand Up @@ -222,7 +256,7 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard {
`
)}
</div>
${!stateObj.attributes.code_format
${!stateObj.attributes.code_format || defaultCode
? nothing
: html`
<ha-textfield
Expand All @@ -234,7 +268,7 @@ class HuiAlarmPanelCard extends LitElement implements LovelaceCard {
: "text"}
></ha-textfield>
`}
${stateObj.attributes.code_format !== FORMAT_NUMBER
${stateObj.attributes.code_format !== FORMAT_NUMBER || defaultCode
? nothing
: html`
<div id="keypad">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;

gjohansson-ST marked this conversation as resolved.
Show resolved Hide resolved
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;
}
code = response;
}

await this.hass!.callService("alarm_control_panel", service, {
Expand Down
Loading