Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions src/panels/lovelace/common/call-service.ts

This file was deleted.

2 changes: 2 additions & 0 deletions src/panels/lovelace/create-element/create-row-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "../entity-rows/hui-script-entity-row";
import "../entity-rows/hui-sensor-entity-row";
import "../entity-rows/hui-text-entity-row";
import "../entity-rows/hui-toggle-entity-row";
import "../special-rows/hui-button-row";
import "../special-rows/hui-attribute-row";
import "../special-rows/hui-call-service-row";
import { EntityConfig } from "../entity-rows/types";
Expand All @@ -16,6 +17,7 @@ const ALWAYS_LOADED_TYPES = new Set([
"sensor-entity",
"text-entity",
"toggle-entity",
"button",
"call-service",
]);
const LAZY_LOAD_TYPES = {
Expand Down
11 changes: 10 additions & 1 deletion src/panels/lovelace/entity-rows/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HomeAssistant } from "../../../types";
import { Condition } from "../common/validate-condition";
import { ActionConfig } from "../../../data/lovelace";

export interface EntityConfig {
entity: string;
Expand Down Expand Up @@ -30,9 +31,16 @@ export interface WeblinkConfig {
}
export interface CallServiceConfig extends EntityConfig {
type: "call-service";
action_name?: string;
service: string;
Comment thread
iantrich marked this conversation as resolved.
service_data?: { [key: string]: any };
Comment thread
iantrich marked this conversation as resolved.
action_name?: string;
}
export interface ButtonRowConfig extends EntityConfig {
type: "button";
action_name?: string;
tap_action?: ActionConfig;
hold_action?: ActionConfig;
double_tap_action?: ActionConfig;
}
export interface CastConfig {
type: "cast";
Expand All @@ -54,6 +62,7 @@ export type LovelaceRowConfig =
| WeblinkConfig
| CallServiceConfig
| CastConfig
| ButtonRowConfig
| ButtonsRowConfig
| ConditionalRowConfig
| AttributeRowConfig;
Expand Down
103 changes: 103 additions & 0 deletions src/panels/lovelace/special-rows/hui-button-row.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import {
html,
LitElement,
TemplateResult,
customElement,
property,
css,
CSSResult,
} from "lit-element";
import "@material/mwc-button";

import "../../../components/ha-icon";

import { LovelaceRow, ButtonRowConfig } from "../entity-rows/types";
import { HomeAssistant } from "../../../types";
import { actionHandler } from "../common/directives/action-handler-directive";
import { hasAction } from "../common/has-action";
import { ActionHandlerEvent } from "../../../data/lovelace";
import { handleAction } from "../common/handle-action";

@customElement("hui-button-row")
export class HuiButtonRow extends LitElement implements LovelaceRow {
public hass?: HomeAssistant;
@property() private _config?: ButtonRowConfig;

public setConfig(config: ButtonRowConfig): void {
if (!config) {
throw new Error("Error in card configuration.");
}

if (!config.name) {
throw new Error("Error in card configuration. No name specified.");
}

if (!config.tap_action) {
throw new Error("Error in card configuration. No action specified.");
}

this._config = config;
}

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

return html`
<ha-icon .icon=${this._config.icon || "hass:remote"}></ha-icon>
<div class="flex">
<div>${this._config.name}</div>
<mwc-button
@action=${this._handleAction}
.actionHandler=${actionHandler({
hasHold: hasAction(this._config!.hold_action),
hasDoubleClick: hasAction(this._config!.double_tap_action),
})}
>${this._config.action_name
? this._config.action_name
: this.hass!.localize("ui.card.service.run")}</mwc-button
>
</div>
`;
}

static get styles(): CSSResult {
return css`
:host {
display: flex;
align-items: center;
}
ha-icon {
padding: 8px;
color: var(--paper-item-icon-color);
}
.flex {
flex: 1;
overflow: hidden;
margin-left: 16px;
display: flex;
justify-content: space-between;
align-items: center;
}
.flex div {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
mwc-button {
margin-right: -0.57em;
}
`;
}

private _handleAction(ev: ActionHandlerEvent) {
handleAction(this, this.hass!, this._config!, ev.detail.action!);
}
}

declare global {
interface HTMLElementTagNameMap {
"hui-button-row": HuiButtonRow;
}
}
91 changes: 21 additions & 70 deletions src/panels/lovelace/special-rows/hui-call-service-row.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,34 @@
import {
html,
LitElement,
TemplateResult,
customElement,
property,
css,
CSSResult,
} from "lit-element";
import "@material/mwc-button";
import { customElement } from "lit-element";

import "../../../components/ha-icon";

import { callService } from "../common/call-service";
import { LovelaceRow, CallServiceConfig } from "../entity-rows/types";
import { HomeAssistant } from "../../../types";
import { CallServiceConfig } from "../entity-rows/types";
import { HuiButtonRow } from "./hui-button-row";

@customElement("hui-call-service-row")
class HuiCallServiceRow extends LitElement implements LovelaceRow {
public hass?: HomeAssistant;

@property() private _config?: CallServiceConfig;
export class HuiCallServiceRow extends HuiButtonRow {
public setConfig(config: any): void {
const callServiceConfig: CallServiceConfig = config;

public setConfig(config: CallServiceConfig): void {
if (!config || !config.name || !config.service) {
if (!callServiceConfig) {
throw new Error("Error in card configuration.");
}

this._config = { icon: "hass:remote", ...config };
}

protected render(): TemplateResult {
if (!this._config) {
return html``;
if (!callServiceConfig.name) {
throw new Error("Error in card configuration. No name specified.");
}

return html`
<ha-icon .icon="${this._config.icon}"></ha-icon>
<div class="flex">
<div>${this._config.name}</div>
<mwc-button @click="${this._callService}"
>${this._config.action_name
? this._config.action_name
: this.hass!.localize("ui.card.service.run")}</mwc-button
>
</div>
`;
}

static get styles(): CSSResult {
return css`
:host {
display: flex;
align-items: center;
}
ha-icon {
padding: 8px;
color: var(--paper-item-icon-color);
}
.flex {
flex: 1;
overflow: hidden;
margin-left: 16px;
display: flex;
justify-content: space-between;
align-items: center;
}
.flex div {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
mwc-button {
margin-right: -0.57em;
}
`;
}
if (!callServiceConfig.service) {
throw new Error("Error in card configuration. No service specified.");
}

private _callService() {
callService(this._config!, this.hass!);
super.setConfig({
tap_action: {
action: "call-service",
service: callServiceConfig.service,
service_data: callServiceConfig.service_data,
},
...callServiceConfig,
type: "button",
});
}
}

Expand Down