Energy device settings fixes (#24801)

This commit is contained in:
karwosts 2025-03-27 08:30:17 -07:00 committed by Bram Kragten
parent b009d71e8f
commit 94ee99160b
3 changed files with 37 additions and 23 deletions

View File

@ -152,12 +152,14 @@ export class EnergyDeviceSettings extends LitElement {
device_consumptions: this.preferences
.device_consumption as DeviceConsumptionEnergyPreference[],
saveCallback: async (newDevice) => {
await this._savePreferences({
const newPrefs = {
...this.preferences,
device_consumption: this.preferences.device_consumption.map((d) =>
d === origDevice ? newDevice : d
),
});
};
this._sanitizeParents(newPrefs);
await this._savePreferences(newPrefs);
},
});
}
@ -177,6 +179,15 @@ export class EnergyDeviceSettings extends LitElement {
});
}
private _sanitizeParents(prefs: EnergyPreferences) {
const statIds = prefs.device_consumption.map((d) => d.stat_consumption);
prefs.device_consumption.forEach((d) => {
if (d.included_in_stat && !statIds.includes(d.included_in_stat)) {
delete d.included_in_stat;
}
});
}
private async _deleteDevice(ev) {
const deviceToDelete: DeviceConsumptionEnergyPreference =
ev.currentTarget.device;
@ -196,14 +207,7 @@ export class EnergyDeviceSettings extends LitElement {
(device) => device !== deviceToDelete
),
};
newPrefs.device_consumption.forEach((d, idx) => {
if (d.included_in_stat === deviceToDelete.stat_consumption) {
newPrefs.device_consumption[idx] = {
...newPrefs.device_consumption[idx],
};
delete newPrefs.device_consumption[idx].included_in_stat;
}
});
this._sanitizeParents(newPrefs);
await this._savePreferences(newPrefs);
} catch (err: any) {
showAlertDialog(this, { title: `Failed to save config: ${err.message}` });

View File

@ -74,6 +74,7 @@ export class DialogEnergyDeviceSettings
this._possibleParents = this._params.device_consumptions.filter(
(d) =>
d.stat_consumption !== this._device!.stat_consumption &&
d.stat_consumption !== this._params?.device?.stat_consumption &&
!children.includes(d.stat_consumption)
);
}
@ -160,18 +161,26 @@ export class DialogEnergyDeviceSettings
naturalMenuWidth
clearable
>
${this._possibleParents.map(
(stat) => html`
<mwc-list-item .value=${stat.stat_consumption}
>${stat.name ||
getStatisticLabel(
this.hass,
stat.stat_consumption,
this._params?.statsMetadata?.[stat.stat_consumption]
)}</mwc-list-item
>
`
)}
${!this._possibleParents.length
? html`
<mwc-list-item disabled value="-"
>${this.hass.localize(
"ui.panel.config.energy.device_consumption.dialog.no_upstream_devices"
)}</mwc-list-item
>
`
: this._possibleParents.map(
(stat) => html`
<mwc-list-item .value=${stat.stat_consumption}
>${stat.name ||
getStatisticLabel(
this.hass,
stat.stat_consumption,
this._params?.statsMetadata?.[stat.stat_consumption]
)}</mwc-list-item
>
`
)}
</ha-select>
<mwc-button @click=${this.closeDialog} slot="secondaryAction">

View File

@ -2902,7 +2902,8 @@
"device_consumption_energy": "Device energy consumption",
"selected_stat_intro": "Select the energy sensor that measures the device's energy usage in either of {unit}.",
"included_in_device": "Upstream device",
"included_in_device_helper": "If this device is already counted by another device (such as a smart switch measured by a smart breaker), selecting the upstream device prevents duplicate energy tracking."
"included_in_device_helper": "If this device is already counted by another device (such as a smart switch measured by a smart breaker), selecting the upstream device prevents duplicate energy tracking.",
"no_upstream_devices": "No eligible upstream devices"
}
}
},