From e371f5d406a42c5562aaa0a14f21779c427b5553 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Mon, 8 May 2023 22:06:21 +0200 Subject: [PATCH] Lock entity options (#16344) --- src/data/entity_registry.ts | 11 +++- .../entity-registry-settings-editor.ts | 53 +++++++++++++++++++ src/translations/en.json | 2 + 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/data/entity_registry.ts b/src/data/entity_registry.ts index b9f145c6e8..3f7c80de4c 100644 --- a/src/data/entity_registry.ts +++ b/src/data/entity_registry.ts @@ -78,6 +78,10 @@ export interface NumberEntityOptions { unit_of_measurement?: string | null; } +export interface LockEntityOptions { + default_code?: string | null; +} + export interface WeatherEntityOptions { precipitation_unit?: string | null; pressure_unit?: string | null; @@ -89,6 +93,7 @@ export interface WeatherEntityOptions { export interface EntityRegistryOptions { number?: NumberEntityOptions; sensor?: SensorEntityOptions; + lock?: LockEntityOptions; weather?: WeatherEntityOptions; conversation?: Record; "cloud.alexa"?: Record; @@ -104,7 +109,11 @@ export interface EntityRegistryEntryUpdateParams { hidden_by: string | null; new_entity_id?: string; options_domain?: string; - options?: SensorEntityOptions | NumberEntityOptions | WeatherEntityOptions; + options?: + | SensorEntityOptions + | NumberEntityOptions + | LockEntityOptions + | WeatherEntityOptions; aliases?: string[]; } diff --git a/src/panels/config/entities/entity-registry-settings-editor.ts b/src/panels/config/entities/entity-registry-settings-editor.ts index c590ebcda4..c41f1fb68f 100644 --- a/src/panels/config/entities/entity-registry-settings-editor.ts +++ b/src/panels/config/entities/entity-registry-settings-editor.ts @@ -60,6 +60,7 @@ import { fetchEntityRegistry, SensorEntityOptions, updateEntityRegistryEntry, + LockEntityOptions, } from "../../../data/entity_registry"; import { domainToName } from "../../../data/integration"; import { getNumberDeviceClassConvertibleUnits } from "../../../data/number"; @@ -171,6 +172,8 @@ export class EntityRegistrySettingsEditor extends LitElement { @state() private _weatherConvertibleUnits?: WeatherUnits; + @state() private _defaultCode?: string | null; + @state() private _noDeviceArea?: boolean; private _origEntityId!: string; @@ -222,6 +225,10 @@ export class EntityRegistrySettingsEditor extends LitElement { this._precision = this.entry.options?.sensor?.display_precision; } + if (domain === "lock") { + this._defaultCode = this.entry.options?.lock?.default_code; + } + if (domain === "weather") { const stateObj: HassEntity | undefined = 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 { if (changedProps.has("_deviceClass")) { const domain = computeDomain(this.entry.entity_id); @@ -303,6 +320,13 @@ export class EntityRegistrySettingsEditor extends LitElement { const invalidDomainUpdate = computeDomain(this._entityId.trim()) !== domain; + const invalidDefaultCode = + domain === "lock" && + this._isInvalidDefaultCode( + stateObj?.attributes?.code_format, + this._defaultCode + ); + const defaultPrecision = this.entry.options?.sensor?.suggested_display_precision ?? undefined; @@ -409,6 +433,22 @@ export class EntityRegistrySettingsEditor extends LitElement { ` : ""} + ${domain === "lock" + ? html` + + ` + : ""} ${domain === "sensor" && this._deviceClass && stateObj?.attributes.unit_of_measurement && @@ -891,6 +931,14 @@ export class EntityRegistrySettingsEditor extends LitElement { (params.options as SensorEntityOptions).display_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 ( domain === "weather" && (stateObj?.attributes?.precipitation_unit !== this._precipitation_unit || @@ -996,6 +1044,11 @@ export class EntityRegistrySettingsEditor extends LitElement { 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 { fireEvent(this, "change"); this._precipitation_unit = ev.target.value; diff --git a/src/translations/en.json b/src/translations/en.json index 05cc547c4d..54ee1e55c3 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -983,6 +983,8 @@ "name": "Name", "icon": "Icon", "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", "unit_of_measurement": "Unit of Measurement", "precipitation_unit": "Precipitation unit",