import type { PropertyValues } from "lit"; import { css, html, LitElement, nothing } from "lit"; import { customElement, property } from "lit/decorators"; import { classMap } from "lit/directives/class-map"; import { fireEvent } from "../../common/dom/fire_event"; import type { NumberSelector } from "../../data/selector"; import type { HomeAssistant } from "../../types"; import "../ha-input-helper-text"; import "../ha-slider"; import "../ha-textfield"; @customElement("ha-selector-number") export class HaNumberSelector extends LitElement { @property({ attribute: false }) public hass!: HomeAssistant; @property({ attribute: false }) public selector!: NumberSelector; @property({ type: Number }) public value?: number; @property({ type: Number }) public placeholder?: number; @property() public label?: string; @property() public helper?: string; @property({ attribute: false }) public localizeValue?: (key: string) => string; @property({ type: Boolean }) public required = true; @property({ type: Boolean }) public disabled = false; private _valueStr = ""; protected willUpdate(changedProps: PropertyValues) { if (changedProps.has("value")) { if (this._valueStr === "" || this.value !== Number(this._valueStr)) { this._valueStr = this.value == null || isNaN(this.value) ? "" : this.value.toString(); } } } protected render() { const isBox = this.selector.number?.mode === "box" || this.selector.number?.min === undefined || this.selector.number?.max === undefined; let sliderStep; if (!isBox) { sliderStep = this.selector.number!.step ?? 1; if (sliderStep === "any") { sliderStep = 1; // divide the range of the slider by 100 steps const step = (this.selector.number!.max! - this.selector.number!.min!) / 100; // biggest step size is 1, round the step size to a division of 1 while (sliderStep > step) { sliderStep /= 10; } } } const translationKey = this.selector.number?.translation_key; let unit = this.selector.number?.unit_of_measurement; if (isBox && unit && this.localizeValue && translationKey) { unit = this.localizeValue(`${translationKey}.unit_of_measurement.${unit}`) || unit; } return html` ${this.label && !isBox ? html`${this.label}${this.required ? "*" : ""}` : nothing}