Lock entity options (#16344)

This commit is contained in:
G Johansson 2023-05-08 22:06:21 +02:00 committed by GitHub
parent 6d4e3a0de3
commit e371f5d406
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 1 deletions

View File

@ -78,6 +78,10 @@ export interface NumberEntityOptions {
unit_of_measurement?: string | null; unit_of_measurement?: string | null;
} }
export interface LockEntityOptions {
default_code?: string | null;
}
export interface WeatherEntityOptions { export interface WeatherEntityOptions {
precipitation_unit?: string | null; precipitation_unit?: string | null;
pressure_unit?: string | null; pressure_unit?: string | null;
@ -89,6 +93,7 @@ export interface WeatherEntityOptions {
export interface EntityRegistryOptions { export interface EntityRegistryOptions {
number?: NumberEntityOptions; number?: NumberEntityOptions;
sensor?: SensorEntityOptions; sensor?: SensorEntityOptions;
lock?: LockEntityOptions;
weather?: WeatherEntityOptions; weather?: WeatherEntityOptions;
conversation?: Record<string, unknown>; conversation?: Record<string, unknown>;
"cloud.alexa"?: Record<string, unknown>; "cloud.alexa"?: Record<string, unknown>;
@ -104,7 +109,11 @@ export interface EntityRegistryEntryUpdateParams {
hidden_by: string | null; hidden_by: string | null;
new_entity_id?: string; new_entity_id?: string;
options_domain?: string; options_domain?: string;
options?: SensorEntityOptions | NumberEntityOptions | WeatherEntityOptions; options?:
| SensorEntityOptions
| NumberEntityOptions
| LockEntityOptions
| WeatherEntityOptions;
aliases?: string[]; aliases?: string[];
} }

View File

@ -60,6 +60,7 @@ import {
fetchEntityRegistry, fetchEntityRegistry,
SensorEntityOptions, SensorEntityOptions,
updateEntityRegistryEntry, updateEntityRegistryEntry,
LockEntityOptions,
} from "../../../data/entity_registry"; } from "../../../data/entity_registry";
import { domainToName } from "../../../data/integration"; import { domainToName } from "../../../data/integration";
import { getNumberDeviceClassConvertibleUnits } from "../../../data/number"; import { getNumberDeviceClassConvertibleUnits } from "../../../data/number";
@ -171,6 +172,8 @@ export class EntityRegistrySettingsEditor extends LitElement {
@state() private _weatherConvertibleUnits?: WeatherUnits; @state() private _weatherConvertibleUnits?: WeatherUnits;
@state() private _defaultCode?: string | null;
@state() private _noDeviceArea?: boolean; @state() private _noDeviceArea?: boolean;
private _origEntityId!: string; private _origEntityId!: string;
@ -222,6 +225,10 @@ export class EntityRegistrySettingsEditor extends LitElement {
this._precision = this.entry.options?.sensor?.display_precision; this._precision = this.entry.options?.sensor?.display_precision;
} }
if (domain === "lock") {
this._defaultCode = this.entry.options?.lock?.default_code;
}
if (domain === "weather") { if (domain === "weather") {
const stateObj: HassEntity | undefined = const stateObj: HassEntity | undefined =
this.hass.states[this.entry.entity_id]; this.hass.states[this.entry.entity_id];
@ -257,6 +264,16 @@ export class EntityRegistrySettingsEditor extends LitElement {
}); });
} }
private _isInvalidDefaultCode(
codeFormat?: string,
value?: string | null
): boolean {
if (codeFormat && value) {
return !RegExp(codeFormat).test(value);
}
return false;
}
protected async updated(changedProps: PropertyValues): Promise<void> { protected async updated(changedProps: PropertyValues): Promise<void> {
if (changedProps.has("_deviceClass")) { if (changedProps.has("_deviceClass")) {
const domain = computeDomain(this.entry.entity_id); const domain = computeDomain(this.entry.entity_id);
@ -303,6 +320,13 @@ export class EntityRegistrySettingsEditor extends LitElement {
const invalidDomainUpdate = computeDomain(this._entityId.trim()) !== domain; const invalidDomainUpdate = computeDomain(this._entityId.trim()) !== domain;
const invalidDefaultCode =
domain === "lock" &&
this._isInvalidDefaultCode(
stateObj?.attributes?.code_format,
this._defaultCode
);
const defaultPrecision = const defaultPrecision =
this.entry.options?.sensor?.suggested_display_precision ?? undefined; this.entry.options?.sensor?.suggested_display_precision ?? undefined;
@ -409,6 +433,22 @@ export class EntityRegistrySettingsEditor extends LitElement {
</ha-select> </ha-select>
` `
: ""} : ""}
${domain === "lock"
? html`
<ha-textfield
.errorMessage=${this.hass.localize(
"ui.dialogs.entity_registry.editor.default_code_error"
)}
.value=${this._defaultCode == null ? "" : this._defaultCode}
.label=${this.hass.localize(
"ui.dialogs.entity_registry.editor.default_code"
)}
.invalid=${invalidDefaultCode}
.disabled=${this.disabled}
@input=${this._defaultcodeChanged}
></ha-textfield>
`
: ""}
${domain === "sensor" && ${domain === "sensor" &&
this._deviceClass && this._deviceClass &&
stateObj?.attributes.unit_of_measurement && stateObj?.attributes.unit_of_measurement &&
@ -891,6 +931,14 @@ export class EntityRegistrySettingsEditor extends LitElement {
(params.options as SensorEntityOptions).display_precision = (params.options as SensorEntityOptions).display_precision =
this._precision; this._precision;
} }
if (
domain === "lock" &&
this.entry.options?.[domain]?.default_code !== this._defaultCode
) {
params.options_domain = domain;
params.options = this.entry.options?.[domain] || {};
(params.options as LockEntityOptions).default_code = this._defaultCode;
}
if ( if (
domain === "weather" && domain === "weather" &&
(stateObj?.attributes?.precipitation_unit !== this._precipitation_unit || (stateObj?.attributes?.precipitation_unit !== this._precipitation_unit ||
@ -996,6 +1044,11 @@ export class EntityRegistrySettingsEditor extends LitElement {
this._unit_of_measurement = ev.target.value; this._unit_of_measurement = ev.target.value;
} }
private _defaultcodeChanged(ev): void {
fireEvent(this, "change");
this._defaultCode = ev.target.value === "" ? null : ev.target.value;
}
private _precipitationUnitChanged(ev): void { private _precipitationUnitChanged(ev): void {
fireEvent(this, "change"); fireEvent(this, "change");
this._precipitation_unit = ev.target.value; this._precipitation_unit = ev.target.value;

View File

@ -983,6 +983,8 @@
"name": "Name", "name": "Name",
"icon": "Icon", "icon": "Icon",
"icon_error": "Icons should be in the format 'prefix:iconname', e.g. 'mdi:home'", "icon_error": "Icons should be in the format 'prefix:iconname', e.g. 'mdi:home'",
"default_code": "Default code",
"default_code_error": "Code does not match code format",
"entity_id": "Entity ID", "entity_id": "Entity ID",
"unit_of_measurement": "Unit of Measurement", "unit_of_measurement": "Unit of Measurement",
"precipitation_unit": "Precipitation unit", "precipitation_unit": "Precipitation unit",