Add unit to temperature tile number button group (#17841)

* Add unit to temperature tile number button group

* Update gallery

* Use blank before unit

* Hide unit if no space

* Fix build

---------

Co-authored-by: Paul Bottein <paul.bottein@gmail.com>
This commit is contained in:
Philip Allgaier 2023-11-10 14:22:10 +01:00 committed by GitHub
parent 21644c70b3
commit d3f6ebd1d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 62 additions and 36 deletions

View File

@ -11,6 +11,7 @@ const buttons: {
min?: number; min?: number;
max?: number; max?: number;
step?: number; step?: number;
unit?: string;
class?: string; class?: string;
}[] = [ }[] = [
{ {
@ -29,6 +30,11 @@ const buttons: {
label: "Custom", label: "Custom",
class: "custom", class: "custom",
}, },
{
id: "unit",
label: "With unit",
unit: "m",
},
]; ];
@customElement("demo-components-ha-control-number-buttons") @customElement("demo-components-ha-control-number-buttons")
@ -50,6 +56,7 @@ export class DemoHarControlNumberButtons extends LitElement {
<pre>Config: ${JSON.stringify(config)}</pre> <pre>Config: ${JSON.stringify(config)}</pre>
<ha-control-number-buttons <ha-control-number-buttons
.value=${this.value} .value=${this.value}
.unit=${config.unit}
.min=${config.min} .min=${config.min}
.max=${config.max} .max=${config.max}
.step=${config.step} .step=${config.step}

View File

@ -256,6 +256,7 @@
"@polymer/polymer": "patch:@polymer/polymer@3.5.1#./.yarn/patches/@polymer/polymer/pr-5569.patch", "@polymer/polymer": "patch:@polymer/polymer@3.5.1#./.yarn/patches/@polymer/polymer/pr-5569.patch",
"@material/mwc-button@^0.25.3": "^0.27.0", "@material/mwc-button@^0.25.3": "^0.27.0",
"lit": "2.8.0", "lit": "2.8.0",
"clean-css": "5.3.2",
"@lit/reactive-element": "1.6.3", "@lit/reactive-element": "1.6.3",
"sortablejs@1.15.0": "patch:sortablejs@npm%3A1.15.0#./.yarn/patches/sortablejs-npm-1.15.0-f3a393abcc.patch", "sortablejs@1.15.0": "patch:sortablejs@npm%3A1.15.0#./.yarn/patches/sortablejs-npm-1.15.0-f3a393abcc.patch",
"leaflet-draw@1.0.4": "patch:leaflet-draw@npm%3A1.0.4#./.yarn/patches/leaflet-draw-npm-1.0.4-0ca0ebcf65.patch" "leaflet-draw@1.0.4": "patch:leaflet-draw@npm%3A1.0.4#./.yarn/patches/leaflet-draw-npm-1.0.4-0ca0ebcf65.patch"

View File

@ -18,6 +18,7 @@ import { blankBeforePercent } from "../translations/blank_before_percent";
import { LocalizeFunc } from "../translations/localize"; import { LocalizeFunc } from "../translations/localize";
import { computeDomain } from "./compute_domain"; import { computeDomain } from "./compute_domain";
import { computeStateDomain } from "./compute_state_domain"; import { computeStateDomain } from "./compute_state_domain";
import { blankBeforeUnit } from "../translations/blank_before_unit";
export const computeAttributeValueDisplay = ( export const computeAttributeValueDisplay = (
localize: LocalizeFunc, localize: LocalizeFunc,
@ -55,20 +56,12 @@ export const computeAttributeValueDisplay = (
unit = getWeatherUnit(config, stateObj as WeatherEntity, attribute); unit = getWeatherUnit(config, stateObj as WeatherEntity, attribute);
} }
if (unit === "%") { if (TEMPERATURE_ATTRIBUTES.has(attribute)) {
return `${formattedValue}${blankBeforePercent(locale)}${unit}`; unit = config.unit_system.temperature;
}
if (unit === "°") {
return `${formattedValue}${unit}`;
} }
if (unit) { if (unit) {
return `${formattedValue} ${unit}`; return `${formattedValue}${blankBeforeUnit(unit, locale)}${unit}`;
}
if (TEMPERATURE_ATTRIBUTES.has(attribute)) {
return `${formattedValue} ${config.unit_system.temperature}`;
} }
return formattedValue; return formattedValue;

View File

@ -3,13 +3,13 @@ import { UNAVAILABLE, UNKNOWN } from "../../data/entity";
import { EntityRegistryDisplayEntry } from "../../data/entity_registry"; import { EntityRegistryDisplayEntry } from "../../data/entity_registry";
import { FrontendLocaleData, TimeZone } from "../../data/translation"; import { FrontendLocaleData, TimeZone } from "../../data/translation";
import { import {
updateIsInstallingFromAttributes,
UPDATE_SUPPORT_PROGRESS, UPDATE_SUPPORT_PROGRESS,
updateIsInstallingFromAttributes,
} from "../../data/update"; } from "../../data/update";
import { HomeAssistant } from "../../types"; import { HomeAssistant } from "../../types";
import { import {
formatDuration,
UNIT_TO_MILLISECOND_CONVERT, UNIT_TO_MILLISECOND_CONVERT,
formatDuration,
} from "../datetime/duration"; } from "../datetime/duration";
import { formatDate } from "../datetime/format_date"; import { formatDate } from "../datetime/format_date";
import { formatDateTime } from "../datetime/format_date_time"; import { formatDateTime } from "../datetime/format_date_time";
@ -19,10 +19,10 @@ import {
getNumberFormatOptions, getNumberFormatOptions,
isNumericFromAttributes, isNumericFromAttributes,
} from "../number/format_number"; } from "../number/format_number";
import { blankBeforePercent } from "../translations/blank_before_percent";
import { LocalizeFunc } from "../translations/localize"; import { LocalizeFunc } from "../translations/localize";
import { computeDomain } from "./compute_domain"; import { computeDomain } from "./compute_domain";
import { supportsFeatureFromAttributes } from "./supports-feature"; import { supportsFeatureFromAttributes } from "./supports-feature";
import { blankBeforeUnit } from "../translations/blank_before_unit";
export const computeStateDisplaySingleEntity = ( export const computeStateDisplaySingleEntity = (
localize: LocalizeFunc, localize: LocalizeFunc,
@ -108,16 +108,20 @@ export const computeStateDisplayFromEntityAttributes = (
// fallback to default // fallback to default
} }
} }
const unit = !attributes.unit_of_measurement
? "" const value = formatNumber(
: attributes.unit_of_measurement === "%"
? blankBeforePercent(locale) + "%"
: ` ${attributes.unit_of_measurement}`;
return `${formatNumber(
state, state,
locale, locale,
getNumberFormatOptions({ state, attributes } as HassEntity, entity) getNumberFormatOptions({ state, attributes } as HassEntity, entity)
)}${unit}`; );
const unit = attributes.unit_of_measurement;
if (unit) {
return `${value}${blankBeforeUnit(unit)}${unit}`;
}
return value;
} }
const domain = computeDomain(entityId); const domain = computeDomain(entityId);

View File

@ -1,11 +1,19 @@
import { mdiMinus, mdiPlus } from "@mdi/js"; import { mdiMinus, mdiPlus } from "@mdi/js";
import { CSSResultGroup, LitElement, TemplateResult, css, html } from "lit"; import {
CSSResultGroup,
LitElement,
TemplateResult,
css,
html,
nothing,
} from "lit";
import { customElement, property, query } from "lit/decorators"; import { customElement, property, query } from "lit/decorators";
import { ifDefined } from "lit/directives/if-defined"; import { ifDefined } from "lit/directives/if-defined";
import { fireEvent } from "../common/dom/fire_event";
import { conditionalClamp } from "../common/number/clamp"; import { conditionalClamp } from "../common/number/clamp";
import { formatNumber } from "../common/number/format_number"; import { formatNumber } from "../common/number/format_number";
import { blankBeforeUnit } from "../common/translations/blank_before_unit";
import { FrontendLocaleData } from "../data/translation"; import { FrontendLocaleData } from "../data/translation";
import { fireEvent } from "../common/dom/fire_event";
const A11Y_KEY_CODES = new Set([ const A11Y_KEY_CODES = new Set([
"ArrowRight", "ArrowRight",
@ -34,6 +42,8 @@ export class HaControlNumberButton extends LitElement {
@property({ type: Number }) public max?: number; @property({ type: Number }) public max?: number;
@property() public unit?: string;
@property({ attribute: "false" }) @property({ attribute: "false" })
public formatOptions: Intl.NumberFormatOptions = {}; public formatOptions: Intl.NumberFormatOptions = {};
@ -114,26 +124,28 @@ export class HaControlNumberButton extends LitElement {
} }
protected render(): TemplateResult { protected render(): TemplateResult {
const displayedValue = const value =
this.value != null this.value != null
? formatNumber(this.value, this.locale, this.formatOptions) ? formatNumber(this.value, this.locale, this.formatOptions)
: ""; : "";
const unit = this.unit ? `${blankBeforeUnit(this.unit)}${this.unit}` : "";
return html` return html`
<div class="container"> <div class="container">
<div <div
id="input" id="input"
class="value" class="value"
role="number-button" role="spinbutton"
.tabIndex=${this.disabled ? "-1" : "0"} .tabIndex=${this.disabled ? "-1" : "0"}
aria-valuenow=${this.value} aria-valuenow=${this.value}
aria-valuetext=${`${value}${unit}`}
aria-valuemin=${this.min} aria-valuemin=${this.min}
aria-valuemax=${this.max} aria-valuemax=${this.max}
aria-label=${ifDefined(this.label)} aria-label=${ifDefined(this.label)}
?disabled=${this.disabled} ?disabled=${this.disabled}
@keydown=${this._handleKeyDown} @keydown=${this._handleKeyDown}
> >
${displayedValue} ${value} ${unit ? html`<span class="unit">${unit}</span>` : nothing}
</div> </div>
<button <button
class="button minus" class="button minus"
@ -185,6 +197,8 @@ export class HaControlNumberButton extends LitElement {
position: relative; position: relative;
width: 100%; width: 100%;
height: 100%; height: 100%;
container-type: inline-size;
container-name: container;
} }
.value { .value {
display: flex; display: flex;
@ -249,6 +263,14 @@ export class HaControlNumberButton extends LitElement {
.button.plus { .button.plus {
right: 0; right: 0;
} }
.unit {
white-space: pre;
}
@container container (max-width: 100px) {
.unit {
display: none;
}
}
`; `;
} }
} }

View File

@ -185,6 +185,7 @@ class HuiTargetTemperatureTileFeature
.formatOptions=${options} .formatOptions=${options}
.target="value" .target="value"
.value=${this.stateObj.attributes.temperature} .value=${this.stateObj.attributes.temperature}
.unit=${this.hass.config.unit_system.temperature}
.min=${this._min} .min=${this._min}
.max=${this._max} .max=${this._max}
.step=${this._step} .step=${this._step}
@ -197,6 +198,7 @@ class HuiTargetTemperatureTileFeature
"--control-number-buttons-focus-color": stateColor, "--control-number-buttons-focus-color": stateColor,
})} })}
.disabled=${this.stateObj!.state === UNAVAILABLE} .disabled=${this.stateObj!.state === UNAVAILABLE}
.locale=${this.hass.locale}
> >
</ha-control-number-buttons> </ha-control-number-buttons>
</ha-control-button-group> </ha-control-button-group>
@ -215,6 +217,7 @@ class HuiTargetTemperatureTileFeature
.formatOptions=${options} .formatOptions=${options}
.target=${"low"} .target=${"low"}
.value=${this._targetTemperature.low} .value=${this._targetTemperature.low}
.unit=${this.hass.config.unit_system.temperature}
.min=${this._min} .min=${this._min}
.max=${Math.min( .max=${Math.min(
this._max, this._max,
@ -230,12 +233,14 @@ class HuiTargetTemperatureTileFeature
"--control-number-buttons-focus-color": stateColor, "--control-number-buttons-focus-color": stateColor,
})} })}
.disabled=${this.stateObj!.state === UNAVAILABLE} .disabled=${this.stateObj!.state === UNAVAILABLE}
.locale=${this.hass.locale}
> >
</ha-control-number-buttons> </ha-control-number-buttons>
<ha-control-number-buttons <ha-control-number-buttons
.formatOptions=${options} .formatOptions=${options}
.target=${"high"} .target=${"high"}
.value=${this._targetTemperature.high} .value=${this._targetTemperature.high}
.unit=${this.hass.config.unit_system.temperature}
.min=${Math.max( .min=${Math.max(
this._min, this._min,
this._targetTemperature.low ?? this._min this._targetTemperature.low ?? this._min
@ -251,6 +256,7 @@ class HuiTargetTemperatureTileFeature
"--control-number-buttons-focus-color": stateColor, "--control-number-buttons-focus-color": stateColor,
})} })}
.disabled=${this.stateObj!.state === UNAVAILABLE} .disabled=${this.stateObj!.state === UNAVAILABLE}
.locale=${this.hass.locale}
> >
</ha-control-number-buttons> </ha-control-number-buttons>
</ha-control-button-group> </ha-control-button-group>
@ -261,6 +267,7 @@ class HuiTargetTemperatureTileFeature
<ha-control-button-group> <ha-control-button-group>
<ha-control-number-buttons <ha-control-number-buttons
.disabled=${this.stateObj!.state === UNAVAILABLE} .disabled=${this.stateObj!.state === UNAVAILABLE}
.unit=${this.hass.config.unit_system.temperature}
.label=${this.hass.formatEntityAttributeName( .label=${this.hass.formatEntityAttributeName(
this.stateObj, this.stateObj,
"temperature" "temperature"
@ -268,6 +275,7 @@ class HuiTargetTemperatureTileFeature
style=${styleMap({ style=${styleMap({
"--control-number-buttons-focus-color": stateColor, "--control-number-buttons-focus-color": stateColor,
})} })}
.locale=${this.hass.locale}
> >
</ha-control-number-buttons> </ha-control-number-buttons>
</ha-control-button-group> </ha-control-button-group>

View File

@ -6616,16 +6616,7 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"clean-css@npm:^4.2.1, clean-css@npm:^4.2.3": "clean-css@npm:5.3.2":
version: 4.2.4
resolution: "clean-css@npm:4.2.4"
dependencies:
source-map: "npm:~0.6.0"
checksum: 4f64dbebfa29feb79be25d6f91239239179adc805c6d7442e2c728970ca23a75b5f238118477b4b78553b89e50f14a64fe35145ecc86b6badf971883c4ad2ffe
languageName: node
linkType: hard
"clean-css@npm:~5.3.2":
version: 5.3.2 version: 5.3.2
resolution: "clean-css@npm:5.3.2" resolution: "clean-css@npm:5.3.2"
dependencies: dependencies: