forked from custom-cards/boilerplate-card
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boilerplate-card.ts
128 lines (108 loc) · 3.96 KB
/
boilerplate-card.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* eslint-disable @typescript-eslint/no-explicit-any */
import { LitElement, html, TemplateResult, css, PropertyValues, CSSResultGroup } from 'lit';
import { customElement, property, state } from 'lit/decorators';
import {
HomeAssistant,
hasConfigOrEntityChanged,
hasAction,
ActionHandlerEvent,
handleAction,
LovelaceCardEditor,
getLovelace,
} from 'custom-card-helpers'; // This is a community maintained npm module with common helper functions/types. https://github.com/custom-cards/custom-card-helpers
import type { BoilerplateCardConfig } from './types';
import { actionHandler } from './action-handler-directive';
import { CARD_VERSION } from './const';
import { localize } from './localize/localize';
/* eslint no-console: 0 */
console.info(
`%c BOILERPLATE-CARD \n%c ${localize('common.version')} ${CARD_VERSION} `,
'color: orange; font-weight: bold; background: black',
'color: white; font-weight: bold; background: dimgray',
);
// This puts your card into the UI card picker dialog
(window as any).customCards = (window as any).customCards || [];
(window as any).customCards.push({
type: 'boilerplate-card',
name: 'Boilerplate Card',
description: 'A template custom card for you to create something awesome',
});
// TODO Name your custom element
@customElement('boilerplate-card')
export class BoilerplateCard extends LitElement {
public static async getConfigElement(): Promise<LovelaceCardEditor> {
await import('./editor');
return document.createElement('boilerplate-card-editor');
}
public static getStubConfig(): Record<string, unknown> {
return {};
}
// TODO Add any properities that should cause your element to re-render here
// https://lit.dev/docs/components/properties/
@property({ attribute: false }) public hass!: HomeAssistant;
@state() private config!: BoilerplateCardConfig;
// https://lit.dev/docs/components/properties/#accessors-custom
public setConfig(config: BoilerplateCardConfig): void {
// TODO Check for required fields and that they are of the proper format
if (!config) {
throw new Error(localize('common.invalid_configuration'));
}
if (config.test_gui) {
getLovelace().setEditMode(true);
}
this.config = {
name: 'Boilerplate',
...config,
};
}
// https://lit.dev/docs/components/lifecycle/#reactive-update-cycle-performing
protected shouldUpdate(changedProps: PropertyValues): boolean {
if (!this.config) {
return false;
}
return hasConfigOrEntityChanged(this, changedProps, false);
}
// https://lit.dev/docs/components/rendering/
protected render(): TemplateResult | void {
// TODO Check for stateObj or other necessary things and render a warning if missing
if (this.config.show_warning) {
return this._showWarning(localize('common.show_warning'));
}
if (this.config.show_error) {
return this._showError(localize('common.show_error'));
}
return html`
<ha-card
.header=${this.config.name}
@action=${this._handleAction}
.actionHandler=${actionHandler({
hasHold: hasAction(this.config.hold_action),
hasDoubleClick: hasAction(this.config.double_tap_action),
})}
tabindex="0"
.label=${`Boilerplate: ${this.config.entity || 'No Entity Defined'}`}
></ha-card>
`;
}
private _handleAction(ev: ActionHandlerEvent): void {
if (this.hass && this.config && ev.detail.action) {
handleAction(this, this.hass, this.config, ev.detail.action);
}
}
private _showWarning(warning: string): TemplateResult {
return html` <hui-warning>${warning}</hui-warning> `;
}
private _showError(error: string): TemplateResult {
const errorCard = document.createElement('hui-error-card');
errorCard.setConfig({
type: 'error',
error,
origConfig: this.config,
});
return html` ${errorCard} `;
}
// https://lit.dev/docs/components/styles/
static get styles(): CSSResultGroup {
return css``;
}
}