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 hui-script-entity-row to TypeScript/LitElement #2020

Merged
merged 2 commits into from Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
80 changes: 0 additions & 80 deletions src/panels/lovelace/entity-rows/hui-script-entity-row.js

This file was deleted.

93 changes: 93 additions & 0 deletions src/panels/lovelace/entity-rows/hui-script-entity-row.ts
@@ -0,0 +1,93 @@
import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element";
import { TemplateResult } from "lit-html";

import "../components/hui-generic-entity-row";
import "../../../components/entity/ha-entity-toggle";
import "./hui-error-entity-row";

import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin";
import { HomeAssistant } from "../../../types";
import { EntityRow, EntityConfig } from "./types";

class HuiScriptEntityRow extends hassLocalizeLitMixin(LitElement)
implements EntityRow {
public hass?: HomeAssistant;
private _config?: EntityConfig;

static get properties(): PropertyDeclarations {
return {
hass: {},
_config: {},
};
}

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

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

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

if (!stateObj) {
return html`
<hui-error-entity-row
.entity="${this._config.entity}"
></hui-error-entity-row>
`;
}

return html`
${this.renderStyle()}
<hui-generic-entity-row .hass="${this.hass}" .config="${this._config}">
${
stateObj.attributes.can_cancel
? html`
<ha-entity-toggle
.hass="${this.hass}"
.stateObj="${stateObj}"
></ha-entity-toggle>
`
: html`
<paper-button @click="${this._callService}">
${this.localize("ui.card.script.execute")}
</paper-button>
`
}
</hui-generic-entity-row>
`;
}

protected renderStyle(): TemplateResult {
return html`
<style>
paper-button {
color: var(--primary-color);
font-weight: 500;
margin-right: -0.57em;
}
</style>
`;
}

private _callService(ev): void {
ev.stopPropagation();
this.hass!.callService("script", "turn_on", {
entity_id: this._config!.entity,
});
}
}

declare global {
interface HTMLElementTagNameMap {
"hui-script-entity-row": HuiScriptEntityRow;
}
}

customElements.define("hui-script-entity-row", HuiScriptEntityRow);