-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add number entity support #7876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
src/panels/lovelace/entity-rows/hui-number-entity-row.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| import "@polymer/paper-input/paper-input"; | ||
| import { | ||
| css, | ||
| CSSResult, | ||
| customElement, | ||
| html, | ||
| LitElement, | ||
| property, | ||
| internalProperty, | ||
| PropertyValues, | ||
| TemplateResult, | ||
| } from "lit-element"; | ||
| import { computeRTLDirection } from "../../../common/util/compute_rtl"; | ||
| import "../../../components/ha-slider"; | ||
| import { UNAVAILABLE_STATES } from "../../../data/entity"; | ||
| import { setValue } from "../../../data/input_text"; | ||
| import { HomeAssistant } from "../../../types"; | ||
| import { hasConfigOrEntityChanged } from "../common/has-changed"; | ||
| import "../components/hui-generic-entity-row"; | ||
| import { EntityConfig, LovelaceRow } from "./types"; | ||
| import { createEntityNotFoundWarning } from "../components/hui-warning"; | ||
|
|
||
| @customElement("hui-number-entity-row") | ||
| class HuiNumberEntityRow extends LitElement implements LovelaceRow { | ||
| @property({ attribute: false }) public hass?: HomeAssistant; | ||
|
|
||
| @internalProperty() private _config?: EntityConfig; | ||
|
|
||
| private _loaded?: boolean; | ||
|
|
||
| private _updated?: boolean; | ||
|
|
||
| public setConfig(config: EntityConfig): void { | ||
| if (!config) { | ||
| throw new Error("Invalid configuration"); | ||
| } | ||
| this._config = config; | ||
| } | ||
|
|
||
| public connectedCallback(): void { | ||
| super.connectedCallback(); | ||
| if (this._updated && !this._loaded) { | ||
| this._initialLoad(); | ||
| } | ||
| } | ||
|
|
||
| protected firstUpdated(): void { | ||
| this._updated = true; | ||
| if (this.isConnected && !this._loaded) { | ||
| this._initialLoad(); | ||
| } | ||
| } | ||
|
|
||
| protected shouldUpdate(changedProps: PropertyValues): boolean { | ||
| return hasConfigOrEntityChanged(this, changedProps); | ||
| } | ||
|
|
||
| protected render(): TemplateResult { | ||
| if (!this._config || !this.hass) { | ||
| return html``; | ||
| } | ||
|
|
||
| const stateObj = this.hass.states[this._config.entity]; | ||
|
|
||
| if (!stateObj) { | ||
| return html` | ||
| <hui-warning> | ||
| ${createEntityNotFoundWarning(this.hass, this._config.entity)} | ||
| </hui-warning> | ||
| `; | ||
| } | ||
|
|
||
| return html` | ||
| <hui-generic-entity-row .hass=${this.hass} .config=${this._config}> | ||
| ${stateObj.attributes.mode === "slider" | ||
| ? html` | ||
| <div class="flex"> | ||
| <ha-slider | ||
| .disabled=${UNAVAILABLE_STATES.includes(stateObj.state)} | ||
| .dir=${computeRTLDirection(this.hass)} | ||
| .step="${Number(stateObj.attributes.step)}" | ||
| .min="${Number(stateObj.attributes.min)}" | ||
| .max="${Number(stateObj.attributes.max)}" | ||
| .value="${Number(stateObj.state)}" | ||
| pin | ||
| @change="${this._selectedValueChanged}" | ||
| ignore-bar-touch | ||
| id="input" | ||
| ></ha-slider> | ||
| <span class="state"> | ||
| ${Number(stateObj.state)} | ||
| ${stateObj.attributes.unit_of_measurement} | ||
| </span> | ||
| </div> | ||
| ` | ||
| : html` | ||
| <div class="flex state"> | ||
| <paper-input | ||
| no-label-float | ||
| auto-validate | ||
| .disabled=${UNAVAILABLE_STATES.includes(stateObj.state)} | ||
| pattern="[0-9]+([\\.][0-9]+)?" | ||
| .step="${Number(stateObj.attributes.step)}" | ||
| .min="${Number(stateObj.attributes.min)}" | ||
| .max="${Number(stateObj.attributes.max)}" | ||
| .value="${Number(stateObj.state)}" | ||
| type="number" | ||
| @change="${this._selectedValueChanged}" | ||
| id="input" | ||
| ></paper-input> | ||
| ${stateObj.attributes.unit_of_measurement} | ||
| </div> | ||
| `} | ||
| </hui-generic-entity-row> | ||
| `; | ||
| } | ||
|
|
||
| static get styles(): CSSResult { | ||
| return css` | ||
| .flex { | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: flex-end; | ||
| flex-grow: 2; | ||
| } | ||
| .state { | ||
| min-width: 45px; | ||
| text-align: end; | ||
| } | ||
| paper-input { | ||
| text-align: end; | ||
| } | ||
| ha-slider { | ||
| width: 100%; | ||
| max-width: 200px; | ||
| } | ||
| :host { | ||
| cursor: pointer; | ||
| } | ||
| `; | ||
| } | ||
|
|
||
| private async _initialLoad(): Promise<void> { | ||
| this._loaded = true; | ||
| await this.updateComplete; | ||
| const element = this.shadowRoot!.querySelector(".state") as HTMLElement; | ||
|
|
||
| if (!element || !this.parentElement) { | ||
| return; | ||
| } | ||
|
|
||
| element.hidden = this.parentElement.clientWidth <= 350; | ||
| } | ||
|
|
||
| private get _inputElement(): { value: string } { | ||
| // linter recommended the following syntax | ||
| return (this.shadowRoot!.getElementById("input") as unknown) as { | ||
| value: string; | ||
| }; | ||
| } | ||
|
|
||
| private _selectedValueChanged(): void { | ||
| const element = this._inputElement; | ||
| const stateObj = this.hass!.states[this._config!.entity]; | ||
|
|
||
| if (element.value !== stateObj.state) { | ||
| setValue(this.hass!, stateObj.entity_id, element.value!); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| declare global { | ||
| interface HTMLElementTagNameMap { | ||
| "hui-number-entity-row": HuiNumberEntityRow; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This means you would need to add a specific state card for this domain, like the
input_numberalso has:https://github.com/home-assistant/frontend/blob/dev/src/state-summary/state-card-input_number.js#L26