mirror of
https://github.com/home-assistant/frontend.git
synced 2025-11-08 18:39:40 +00:00
108 lines
3.0 KiB
TypeScript
108 lines
3.0 KiB
TypeScript
import "@material/mwc-button/mwc-button";
|
|
import {
|
|
css,
|
|
CSSResultGroup,
|
|
html,
|
|
LitElement,
|
|
PropertyValues,
|
|
nothing,
|
|
} from "lit";
|
|
import { customElement, property, state } from "lit/decorators";
|
|
import { isUnavailableState } from "../../../data/entity";
|
|
import { canRun, ScriptEntity } from "../../../data/script";
|
|
import { HomeAssistant } from "../../../types";
|
|
import { hasConfigOrEntityChanged } from "../common/has-changed";
|
|
import "../components/hui-generic-entity-row";
|
|
import { createEntityNotFoundWarning } from "../components/hui-warning";
|
|
import { ActionRowConfig, LovelaceRow } from "./types";
|
|
|
|
@customElement("hui-script-entity-row")
|
|
class HuiScriptEntityRow extends LitElement implements LovelaceRow {
|
|
@property({ attribute: false }) public hass?: HomeAssistant;
|
|
|
|
@state() private _config?: ActionRowConfig;
|
|
|
|
public setConfig(config: ActionRowConfig): void {
|
|
if (!config) {
|
|
throw new Error("Invalid configuration");
|
|
}
|
|
this._config = config;
|
|
}
|
|
|
|
protected shouldUpdate(changedProps: PropertyValues): boolean {
|
|
return hasConfigOrEntityChanged(this, changedProps);
|
|
}
|
|
|
|
protected render() {
|
|
if (!this._config || !this.hass) {
|
|
return nothing;
|
|
}
|
|
|
|
const stateObj = this.hass.states[this._config.entity] as ScriptEntity;
|
|
|
|
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.state === "on"
|
|
? html`<mwc-button @click=${this._cancelScript}>
|
|
${stateObj.attributes.mode !== "single" &&
|
|
stateObj.attributes.current &&
|
|
stateObj.attributes.current > 0
|
|
? this.hass.localize("ui.card.script.cancel_multiple", {
|
|
number: stateObj.attributes.current,
|
|
})
|
|
: this.hass.localize("ui.card.script.cancel")}
|
|
</mwc-button>`
|
|
: ""}
|
|
${stateObj.state === "off" || stateObj.attributes.max
|
|
? html`<mwc-button
|
|
@click=${this._runScript}
|
|
.disabled=${isUnavailableState(stateObj.state) ||
|
|
!canRun(stateObj)}
|
|
>
|
|
${this._config.action_name ||
|
|
this.hass!.localize("ui.card.script.run")}
|
|
</mwc-button>`
|
|
: ""}
|
|
</hui-generic-entity-row>
|
|
`;
|
|
}
|
|
|
|
static get styles(): CSSResultGroup {
|
|
return css`
|
|
mwc-button:last-child {
|
|
margin-right: -0.57em;
|
|
}
|
|
`;
|
|
}
|
|
|
|
private _cancelScript(ev): void {
|
|
ev.stopPropagation();
|
|
this._callService("turn_off");
|
|
}
|
|
|
|
private _runScript(ev): void {
|
|
ev.stopPropagation();
|
|
this._callService("turn_on");
|
|
}
|
|
|
|
private _callService(service: string): void {
|
|
this.hass!.callService("script", service, {
|
|
entity_id: this._config!.entity,
|
|
});
|
|
}
|
|
}
|
|
|
|
declare global {
|
|
interface HTMLElementTagNameMap {
|
|
"hui-script-entity-row": HuiScriptEntityRow;
|
|
}
|
|
}
|