mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Optionally update sensor units when unit system changes (#15287)
Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
parent
c6eee9bf74
commit
d570d063c7
@ -26,6 +26,9 @@ import "../../../layouts/hass-subpage";
|
|||||||
import { haStyle } from "../../../resources/styles";
|
import { haStyle } from "../../../resources/styles";
|
||||||
import type { HomeAssistant } from "../../../types";
|
import type { HomeAssistant } from "../../../types";
|
||||||
import "../../../components/ha-alert";
|
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")
|
@customElement("ha-config-section-general")
|
||||||
class HaConfigSectionGeneral extends LitElement {
|
class HaConfigSectionGeneral extends LitElement {
|
||||||
@ -55,6 +58,8 @@ class HaConfigSectionGeneral extends LitElement {
|
|||||||
|
|
||||||
@state() private _error?: string;
|
@state() private _error?: string;
|
||||||
|
|
||||||
|
@state() private _updateUnits?: boolean;
|
||||||
|
|
||||||
protected render(): TemplateResult {
|
protected render(): TemplateResult {
|
||||||
const canEdit = ["storage", "default"].includes(
|
const canEdit = ["storage", "default"].includes(
|
||||||
this.hass.config.config_source
|
this.hass.config.config_source
|
||||||
@ -174,6 +179,32 @@ class HaConfigSectionGeneral extends LitElement {
|
|||||||
.disabled=${this._submitting}
|
.disabled=${this._submitting}
|
||||||
></ha-radio>
|
></ha-radio>
|
||||||
</ha-formfield>
|
</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>
|
||||||
<div>
|
<div>
|
||||||
<ha-select
|
<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 {
|
protected firstUpdated(): void {
|
||||||
this._unitSystem =
|
this._unitSystem = this._configuredUnitSystem();
|
||||||
this.hass.config.unit_system.temperature === UNIT_C
|
|
||||||
? "metric"
|
|
||||||
: "us_customary";
|
|
||||||
this._currency = this.hass.config.currency;
|
this._currency = this.hass.config.currency;
|
||||||
this._country = this.hass.config.country;
|
this._country = this.hass.config.country;
|
||||||
this._language = this.hass.config.language;
|
this._language = this.hass.config.language;
|
||||||
this._elevation = this.hass.config.elevation;
|
this._elevation = this.hass.config.elevation;
|
||||||
this._timeZone = this.hass.config.time_zone || "Etc/GMT";
|
this._timeZone = this.hass.config.time_zone || "Etc/GMT";
|
||||||
this._name = this.hass.config.location_name;
|
this._name = this.hass.config.location_name;
|
||||||
|
this._updateUnits = true;
|
||||||
this._computeLanguages();
|
this._computeLanguages();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -335,6 +370,10 @@ class HaConfigSectionGeneral extends LitElement {
|
|||||||
| "us_customary";
|
| "us_customary";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _updateUnitsChanged(ev: CustomEvent) {
|
||||||
|
this._updateUnits = (ev.target as HaCheckbox).checked;
|
||||||
|
}
|
||||||
|
|
||||||
private _locationChanged(ev: CustomEvent) {
|
private _locationChanged(ev: CustomEvent) {
|
||||||
this._location = ev.detail.location;
|
this._location = ev.detail.location;
|
||||||
}
|
}
|
||||||
@ -344,6 +383,25 @@ class HaConfigSectionGeneral extends LitElement {
|
|||||||
if (button.progress) {
|
if (button.progress) {
|
||||||
return;
|
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;
|
button.progress = true;
|
||||||
|
|
||||||
let locationConfig;
|
let locationConfig;
|
||||||
@ -362,6 +420,7 @@ class HaConfigSectionGeneral extends LitElement {
|
|||||||
currency: this._currency,
|
currency: this._currency,
|
||||||
elevation: Number(this._elevation),
|
elevation: Number(this._elevation),
|
||||||
unit_system: this._unitSystem,
|
unit_system: this._unitSystem,
|
||||||
|
update_units: this._updateUnits && unitSystemChanged,
|
||||||
time_zone: this._timeZone,
|
time_zone: this._timeZone,
|
||||||
location_name: this._name,
|
location_name: this._name,
|
||||||
language: this._language,
|
language: this._language,
|
||||||
|
@ -1702,7 +1702,14 @@
|
|||||||
"save_button": "Save",
|
"save_button": "Save",
|
||||||
"currency": "Currency",
|
"currency": "Currency",
|
||||||
"edit_location": "Edit location",
|
"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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user