Skip to content
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

Convert input-number to Lit/TS #2792

Merged
merged 5 commits into from
Feb 22, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/data/input_text.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HomeAssistant } from "../types";

export const setValue = (hass: HomeAssistant, entity: string, value: string) =>
hass.callService("input_text", "set_value", {
hass.callService(entity.split(".", 1)[0], "set_value", {
value,
entity_id: entity,
});
188 changes: 0 additions & 188 deletions src/panels/lovelace/entity-rows/hui-input-number-entity-row.js

This file was deleted.

143 changes: 143 additions & 0 deletions src/panels/lovelace/entity-rows/hui-input-number-entity-row.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import {
html,
LitElement,
TemplateResult,
property,
customElement,
css,
CSSResult,
} from "lit-element";

import "../components/hui-generic-entity-row";
import "../../../components/ha-slider";
import "../components/hui-warning";

import { computeRTLDirection } from "../../../common/util/compute_rtl";
import { EntityRow, EntityConfig } from "./types";
import { HomeAssistant } from "../../../types";
import { setValue } from "../../../data/input_text";

@customElement("hui-input-number-entity-row")
class HuiInputNumberEntityRow extends LitElement implements EntityRow {
@property() public hass?: HomeAssistant;
@property() private _config?: EntityConfig;

public setConfig(config: EntityConfig): void {
if (!config) {
throw new Error("Configuration error");
}
this._config = config;
}

protected firstUpdated(): void {
const element = this.shadowRoot!.querySelector(".state") as HTMLElement;

if (!element) {
return;
}

element.hidden = this.clientWidth <= 350;
}

protected render(): TemplateResult | void {
if (!this._config || !this.hass) {
return html``;
}

const stateObj = this.hass.states[this._config.entity];

if (!stateObj) {
return html`
<hui-warning
>${this.hass.localize(
"ui.panel.lovelace.warning.entity_not_found",
"entity",
this._config.entity
)}</hui-warning
>
`;
}

return html`
<hui-generic-entity-row .hass="${this.hass}" .config="${this._config}">
<div>
${stateObj.attributes.mode === "slider"
? html`
<div class="flex">
<ha-slider
.dir="${this._computeRTLDirection}"
.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">
iantrich marked this conversation as resolved.
Show resolved Hide resolved
${Number(stateObj.state)}
${stateObj.attributes.unit_of_measurement}
</span>
</div>
`
: html`
<paper-input
no-label-float
auto-validate
.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>
`}
</div>
</hui-generic-entity-row>
`;
}

static get styles(): CSSResult {
return css`
.flex {
display: flex;
align-items: center;
}
.state {
min-width: 45px;
text-align: center;
}
paper-input {
text-align: right;
}
`;
}

private get _inputElement(): { value: string } {
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!);
}
}

private _computeRTLDirection(): string {
return computeRTLDirection(this.hass!);
}
}

declare global {
interface HTMLElementTagNameMap {
"hui-input-number-entity-row": HuiInputNumberEntityRow;
}
}