Optionally update sensor units when unit system changes (#15287)

Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
Erik Montnemery 2023-02-06 22:37:05 +01:00 committed by GitHub
parent c6eee9bf74
commit d570d063c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 5 deletions

View File

@ -26,6 +26,9 @@ import "../../../layouts/hass-subpage";
import { haStyle } from "../../../resources/styles";
import type { HomeAssistant } from "../../../types";
import "../../../components/ha-alert";
import { showConfirmationDialog } from "../../../dialogs/generic/show-dialog-box";
import type { HaCheckbox } from "../../../components/ha-checkbox";
import "../../../components/ha-checkbox";
@customElement("ha-config-section-general")
class HaConfigSectionGeneral extends LitElement {
@ -55,6 +58,8 @@ class HaConfigSectionGeneral extends LitElement {
@state() private _error?: string;
@state() private _updateUnits?: boolean;
protected render(): TemplateResult {
const canEdit = ["storage", "default"].includes(
this.hass.config.config_source
@ -174,6 +179,32 @@ class HaConfigSectionGeneral extends LitElement {
.disabled=${this._submitting}
></ha-radio>
</ha-formfield>
${this._unitSystem !== this._configuredUnitSystem()
? html`
<ha-formfield
.label=${this.hass.localize(
"ui.panel.config.core.section.core.core_config.update_units_label"
)}
>
<ha-checkbox
.checked=${this._updateUnits}
.disabled=${this._submitting}
@change=${this._updateUnitsChanged}
></ha-checkbox>
</ha-formfield>
<div class="secondary">
${this.hass.localize(
"ui.panel.config.core.section.core.core_config.update_units_text_1"
)}
${this.hass.localize(
"ui.panel.config.core.section.core.core_config.update_units_text_2"
)} <br /><br />
${this.hass.localize(
"ui.panel.config.core.section.core.core_config.update_units_text_3"
)}
</div>
`
: ""}
</div>
<div>
<ha-select
@ -284,17 +315,21 @@ class HaConfigSectionGeneral extends LitElement {
`;
}
private _configuredUnitSystem() {
return this.hass.config.unit_system.temperature === UNIT_C
? "metric"
: "us_customary";
}
protected firstUpdated(): void {
this._unitSystem =
this.hass.config.unit_system.temperature === UNIT_C
? "metric"
: "us_customary";
this._unitSystem = this._configuredUnitSystem();
this._currency = this.hass.config.currency;
this._country = this.hass.config.country;
this._language = this.hass.config.language;
this._elevation = this.hass.config.elevation;
this._timeZone = this.hass.config.time_zone || "Etc/GMT";
this._name = this.hass.config.location_name;
this._updateUnits = true;
this._computeLanguages();
}
@ -335,6 +370,10 @@ class HaConfigSectionGeneral extends LitElement {
| "us_customary";
}
private _updateUnitsChanged(ev: CustomEvent) {
this._updateUnits = (ev.target as HaCheckbox).checked;
}
private _locationChanged(ev: CustomEvent) {
this._location = ev.detail.location;
}
@ -344,6 +383,25 @@ class HaConfigSectionGeneral extends LitElement {
if (button.progress) {
return;
}
const unitSystemChanged = this._unitSystem !== this._configuredUnitSystem();
if (unitSystemChanged && this._updateUnits) {
if (
!(await showConfirmationDialog(this, {
title: this.hass.localize(
"ui.panel.config.core.section.core.core_config.update_units_confirm_title"
),
text: this.hass.localize(
"ui.panel.config.core.section.core.core_config.update_units_confirm_text"
),
confirmText: this.hass!.localize(
"ui.panel.config.core.section.core.core_config.update_units_confirm_update"
),
dismissText: this.hass!.localize("ui.common.cancel"),
}))
) {
return;
}
}
button.progress = true;
let locationConfig;
@ -362,6 +420,7 @@ class HaConfigSectionGeneral extends LitElement {
currency: this._currency,
elevation: Number(this._elevation),
unit_system: this._unitSystem,
update_units: this._updateUnits && unitSystemChanged,
time_zone: this._timeZone,
location_name: this._name,
language: this._language,

View File

@ -1702,7 +1702,14 @@
"save_button": "Save",
"currency": "Currency",
"edit_location": "Edit location",
"edit_location_description": "Location can be changed in zone settings"
"edit_location_description": "Location can be changed in zone settings",
"update_units_label": "Update the unit of all sensors",
"update_units_text_1": "The unit system has been changed, and the unit of some sensors like distance and speed can be updated.",
"update_units_text_2": "The unit of sensors where the unit has been edited from the UI or which can't be edited from the UI will not be updated.",
"update_units_text_3": "Note: The unit of temperature sensors is always updated.",
"update_units_confirm_title": "The unit of sensors will be updated",
"update_units_confirm_text": "The unit system has been changed, and the unit of some sensors like distance and speed will be updated.",
"update_units_confirm_update": "Update"
}
}
}