diff --git a/src/panels/config/energy/components/ha-energy-device-settings.ts b/src/panels/config/energy/components/ha-energy-device-settings.ts
index 59d28b8a23..11eb58d42f 100644
--- a/src/panels/config/energy/components/ha-energy-device-settings.ts
+++ b/src/panels/config/energy/components/ha-energy-device-settings.ts
@@ -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}` });
diff --git a/src/panels/config/energy/dialogs/dialog-energy-device-settings.ts b/src/panels/config/energy/dialogs/dialog-energy-device-settings.ts
index cb190faf13..c713b3e35c 100644
--- a/src/panels/config/energy/dialogs/dialog-energy-device-settings.ts
+++ b/src/panels/config/energy/dialogs/dialog-energy-device-settings.ts
@@ -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`
- ${stat.name ||
- getStatisticLabel(
- this.hass,
- stat.stat_consumption,
- this._params?.statsMetadata?.[stat.stat_consumption]
- )}
- `
- )}
+ ${!this._possibleParents.length
+ ? html`
+ ${this.hass.localize(
+ "ui.panel.config.energy.device_consumption.dialog.no_upstream_devices"
+ )}
+ `
+ : this._possibleParents.map(
+ (stat) => html`
+ ${stat.name ||
+ getStatisticLabel(
+ this.hass,
+ stat.stat_consumption,
+ this._params?.statsMetadata?.[stat.stat_consumption]
+ )}
+ `
+ )}
diff --git a/src/translations/en.json b/src/translations/en.json
index 9052d42df1..a030e84897 100644
--- a/src/translations/en.json
+++ b/src/translations/en.json
@@ -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"
}
}
},