Skip to content

Commit

Permalink
Add unit to temperature tile number button group (#17841)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
spacegaier and piitaya authored Nov 10, 2023
1 parent 21644c7 commit d3f6ebd
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 36 deletions.
7 changes: 7 additions & 0 deletions gallery/src/pages/components/ha-control-number-buttons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const buttons: {
min?: number;
max?: number;
step?: number;
unit?: string;
class?: string;
}[] = [
{
Expand All @@ -29,6 +30,11 @@ const buttons: {
label: "Custom",
class: "custom",
},
{
id: "unit",
label: "With unit",
unit: "m",
},
];

@customElement("demo-components-ha-control-number-buttons")
Expand All @@ -50,6 +56,7 @@ export class DemoHarControlNumberButtons extends LitElement {
<pre>Config: ${JSON.stringify(config)}</pre>
<ha-control-number-buttons
.value=${this.value}
.unit=${config.unit}
.min=${config.min}
.max=${config.max}
.step=${config.step}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@
"@polymer/polymer": "patch:@polymer/[email protected]#./.yarn/patches/@polymer/polymer/pr-5569.patch",
"@material/mwc-button@^0.25.3": "^0.27.0",
"lit": "2.8.0",
"clean-css": "5.3.2",
"@lit/reactive-element": "1.6.3",
"[email protected]": "patch:sortablejs@npm%3A1.15.0#./.yarn/patches/sortablejs-npm-1.15.0-f3a393abcc.patch",
"[email protected]": "patch:leaflet-draw@npm%3A1.0.4#./.yarn/patches/leaflet-draw-npm-1.0.4-0ca0ebcf65.patch"
Expand Down
15 changes: 4 additions & 11 deletions src/common/entity/compute_attribute_display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { blankBeforePercent } from "../translations/blank_before_percent";
import { LocalizeFunc } from "../translations/localize";
import { computeDomain } from "./compute_domain";
import { computeStateDomain } from "./compute_state_domain";
import { blankBeforeUnit } from "../translations/blank_before_unit";

export const computeAttributeValueDisplay = (
localize: LocalizeFunc,
Expand Down Expand Up @@ -55,20 +56,12 @@ export const computeAttributeValueDisplay = (
unit = getWeatherUnit(config, stateObj as WeatherEntity, attribute);
}

if (unit === "%") {
return `${formattedValue}${blankBeforePercent(locale)}${unit}`;
}

if (unit === "°") {
return `${formattedValue}${unit}`;
if (TEMPERATURE_ATTRIBUTES.has(attribute)) {
unit = config.unit_system.temperature;
}

if (unit) {
return `${formattedValue} ${unit}`;
}

if (TEMPERATURE_ATTRIBUTES.has(attribute)) {
return `${formattedValue} ${config.unit_system.temperature}`;
return `${formattedValue}${blankBeforeUnit(unit, locale)}${unit}`;
}

return formattedValue;
Expand Down
24 changes: 14 additions & 10 deletions src/common/entity/compute_state_display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { UNAVAILABLE, UNKNOWN } from "../../data/entity";
import { EntityRegistryDisplayEntry } from "../../data/entity_registry";
import { FrontendLocaleData, TimeZone } from "../../data/translation";
import {
updateIsInstallingFromAttributes,
UPDATE_SUPPORT_PROGRESS,
updateIsInstallingFromAttributes,
} from "../../data/update";
import { HomeAssistant } from "../../types";
import {
formatDuration,
UNIT_TO_MILLISECOND_CONVERT,
formatDuration,
} from "../datetime/duration";
import { formatDate } from "../datetime/format_date";
import { formatDateTime } from "../datetime/format_date_time";
Expand All @@ -19,10 +19,10 @@ import {
getNumberFormatOptions,
isNumericFromAttributes,
} from "../number/format_number";
import { blankBeforePercent } from "../translations/blank_before_percent";
import { LocalizeFunc } from "../translations/localize";
import { computeDomain } from "./compute_domain";
import { supportsFeatureFromAttributes } from "./supports-feature";
import { blankBeforeUnit } from "../translations/blank_before_unit";

export const computeStateDisplaySingleEntity = (
localize: LocalizeFunc,
Expand Down Expand Up @@ -108,16 +108,20 @@ export const computeStateDisplayFromEntityAttributes = (
// fallback to default
}
}
const unit = !attributes.unit_of_measurement
? ""
: attributes.unit_of_measurement === "%"
? blankBeforePercent(locale) + "%"
: ` ${attributes.unit_of_measurement}`;
return `${formatNumber(

const value = formatNumber(
state,
locale,
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);
Expand Down
32 changes: 27 additions & 5 deletions src/components/ha-control-number-buttons.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
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 { ifDefined } from "lit/directives/if-defined";
import { fireEvent } from "../common/dom/fire_event";
import { conditionalClamp } from "../common/number/clamp";
import { formatNumber } from "../common/number/format_number";
import { blankBeforeUnit } from "../common/translations/blank_before_unit";
import { FrontendLocaleData } from "../data/translation";
import { fireEvent } from "../common/dom/fire_event";

const A11Y_KEY_CODES = new Set([
"ArrowRight",
Expand Down Expand Up @@ -34,6 +42,8 @@ export class HaControlNumberButton extends LitElement {

@property({ type: Number }) public max?: number;

@property() public unit?: string;

@property({ attribute: "false" })
public formatOptions: Intl.NumberFormatOptions = {};

Expand Down Expand Up @@ -114,26 +124,28 @@ export class HaControlNumberButton extends LitElement {
}

protected render(): TemplateResult {
const displayedValue =
const value =
this.value != null
? formatNumber(this.value, this.locale, this.formatOptions)
: "";
const unit = this.unit ? `${blankBeforeUnit(this.unit)}${this.unit}` : "";

return html`
<div class="container">
<div
id="input"
class="value"
role="number-button"
role="spinbutton"
.tabIndex=${this.disabled ? "-1" : "0"}
aria-valuenow=${this.value}
aria-valuetext=${`${value}${unit}`}
aria-valuemin=${this.min}
aria-valuemax=${this.max}
aria-label=${ifDefined(this.label)}
?disabled=${this.disabled}
@keydown=${this._handleKeyDown}
>
${displayedValue}
${value} ${unit ? html`<span class="unit">${unit}</span>` : nothing}
</div>
<button
class="button minus"
Expand Down Expand Up @@ -185,6 +197,8 @@ export class HaControlNumberButton extends LitElement {
position: relative;
width: 100%;
height: 100%;
container-type: inline-size;
container-name: container;
}
.value {
display: flex;
Expand Down Expand Up @@ -249,6 +263,14 @@ export class HaControlNumberButton extends LitElement {
.button.plus {
right: 0;
}
.unit {
white-space: pre;
}
@container container (max-width: 100px) {
.unit {
display: none;
}
}
`;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ class HuiTargetTemperatureTileFeature
.formatOptions=${options}
.target="value"
.value=${this.stateObj.attributes.temperature}
.unit=${this.hass.config.unit_system.temperature}
.min=${this._min}
.max=${this._max}
.step=${this._step}
Expand All @@ -197,6 +198,7 @@ class HuiTargetTemperatureTileFeature
"--control-number-buttons-focus-color": stateColor,
})}
.disabled=${this.stateObj!.state === UNAVAILABLE}
.locale=${this.hass.locale}
>
</ha-control-number-buttons>
</ha-control-button-group>
Expand All @@ -215,6 +217,7 @@ class HuiTargetTemperatureTileFeature
.formatOptions=${options}
.target=${"low"}
.value=${this._targetTemperature.low}
.unit=${this.hass.config.unit_system.temperature}
.min=${this._min}
.max=${Math.min(
this._max,
Expand All @@ -230,12 +233,14 @@ class HuiTargetTemperatureTileFeature
"--control-number-buttons-focus-color": stateColor,
})}
.disabled=${this.stateObj!.state === UNAVAILABLE}
.locale=${this.hass.locale}
>
</ha-control-number-buttons>
<ha-control-number-buttons
.formatOptions=${options}
.target=${"high"}
.value=${this._targetTemperature.high}
.unit=${this.hass.config.unit_system.temperature}
.min=${Math.max(
this._min,
this._targetTemperature.low ?? this._min
Expand All @@ -251,6 +256,7 @@ class HuiTargetTemperatureTileFeature
"--control-number-buttons-focus-color": stateColor,
})}
.disabled=${this.stateObj!.state === UNAVAILABLE}
.locale=${this.hass.locale}
>
</ha-control-number-buttons>
</ha-control-button-group>
Expand All @@ -261,13 +267,15 @@ class HuiTargetTemperatureTileFeature
<ha-control-button-group>
<ha-control-number-buttons
.disabled=${this.stateObj!.state === UNAVAILABLE}
.unit=${this.hass.config.unit_system.temperature}
.label=${this.hass.formatEntityAttributeName(
this.stateObj,
"temperature"
)}
style=${styleMap({
"--control-number-buttons-focus-color": stateColor,
})}
.locale=${this.hass.locale}
>
</ha-control-number-buttons>
</ha-control-button-group>
Expand Down
11 changes: 1 addition & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6616,16 +6616,7 @@ __metadata:
languageName: node
linkType: hard

"clean-css@npm:^4.2.1, clean-css@npm:^4.2.3":
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":
"clean-css@npm:5.3.2":
version: 5.3.2
resolution: "clean-css@npm:5.3.2"
dependencies:
Expand Down

0 comments on commit d3f6ebd

Please sign in to comment.