Skip to content

Commit

Permalink
Optionally update sensor units when unit system changes (home-assista…
Browse files Browse the repository at this point in the history
…nt#15287)

Co-authored-by: Bram Kragten <mail@bramkragten.nl>
  • Loading branch information
emontnemery and bramkragten authored Feb 6, 2023
1 parent c6eee9b commit d570d06
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
67 changes: 63 additions & 4 deletions src/panels/config/core/ha-config-section-general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Expand Down

0 comments on commit d570d06

Please sign in to comment.