Fix overzealous loading of ha-service-control default values (#15741)

This commit is contained in:
karwosts 2023-03-06 08:06:38 -08:00 committed by GitHub
parent f193563649
commit 4d19e3ad63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 6 deletions

View File

@ -136,6 +136,7 @@ export class HaServiceControl extends LitElement {
if (oldValue?.service !== this.value?.service) {
let updatedDefaultValue = false;
if (this._value && serviceData) {
const loadDefaults = this.value && !("data" in this.value);
// Set mandatory bools without a default value to false
if (!this._value.data) {
this._value.data = {};
@ -152,6 +153,7 @@ export class HaServiceControl extends LitElement {
this._value!.data![field.key] = false;
}
if (
loadDefaults &&
field.selector &&
field.default !== undefined &&
this._value!.data![field.key] === undefined
@ -341,10 +343,10 @@ export class HaServiceControl extends LitElement {
.selector=${dataField.selector}
.key=${dataField.key}
@value-changed=${this._serviceDataChanged}
.value=${this._value?.data &&
this._value.data[dataField.key] !== undefined
.value=${this._value?.data
? this._value.data[dataField.key]
: dataField.default}
: undefined}
.placeholder=${dataField.default}
></ha-selector>
</ha-settings-row>`
: "";
@ -362,7 +364,7 @@ export class HaServiceControl extends LitElement {
this._value?.service,
this.hass.services
)?.fields.find((field) => field.key === key)?.default;
if (defaultValue) {
if (defaultValue != null) {
data = {
...this._value?.data,
[key]: defaultValue,

View File

@ -57,7 +57,9 @@ export class HuiActionEditor extends LitElement {
private _serviceAction = memoizeOne(
(config: CallServiceActionConfig): ServiceAction => ({
service: this._service,
data: config.data ?? config.service_data,
...(config.data || config.service_data
? { data: config.data ?? config.service_data }
: null),
target: config.target,
})
);
@ -196,9 +198,12 @@ export class HuiActionEditor extends LitElement {
const value = {
...this.config!,
service: ev.detail.value.service || "",
data: ev.detail.value.data || {},
data: ev.detail.value.data,
target: ev.detail.value.target || {},
};
if (!ev.detail.value.data) {
delete value.data;
}
// "service_data" is allowed for backwards compatibility but replaced with "data" on write
if ("service_data" in value) {
delete value.service_data;