-
-
Notifications
You must be signed in to change notification settings - Fork 523
/
editor.ts
198 lines (179 loc) · 5.63 KB
/
editor.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { LitElement, html, nothing } from 'lit';
import {
HomeAssistant,
LovelaceCardConfig,
LovelaceCardEditor,
fireEvent,
} from 'custom-card-helpers';
import localize from './localize';
import { customElement, property, state } from 'lit/decorators.js';
import { Template, VacuumCardConfig } from './types';
import styles from './editor.css';
type ConfigElement = HTMLInputElement & {
configValue?: keyof VacuumCardConfig;
};
@customElement('vacuum-card-editor')
export class VacuumCardEditor extends LitElement implements LovelaceCardEditor {
@property({ attribute: false }) public hass?: HomeAssistant;
@state() private config!: Partial<VacuumCardConfig>;
@state() private image? = undefined;
@state() private compact_view = false;
@state() private show_name = true;
@state() private show_status = true;
@state() private show_toolbar = true;
setConfig(config: LovelaceCardConfig & VacuumCardConfig): void {
this.config = config;
if (!this.config.entity) {
this.config.entity = this.getEntitiesByType('vacuum')[0] || '';
fireEvent(this, 'config-changed', { config: this.config });
}
}
private getEntitiesByType(type: string): string[] {
if (!this.hass) {
return [];
}
return Object.keys(this.hass.states).filter((id) => id.startsWith(type));
}
protected render(): Template {
if (!this.hass) {
return nothing;
}
const vacuumEntities = this.getEntitiesByType('vacuum');
const cameraEntities = [
...this.getEntitiesByType('camera'),
...this.getEntitiesByType('image'),
];
return html`
<div class="card-config">
<div class="option">
<ha-select
.label=${localize('editor.entity')}
@selected=${this.valueChanged}
.configValue=${'entity'}
.value=${this.config.entity}
@closed=${(e: Event) => e.stopPropagation()}
fixedMenuPosition
naturalMenuWidth
required
validationMessage=${localize('error.missing_entity')}
>
${vacuumEntities.map(
(entity) => html` <mwc-list-item .value=${entity}
>${entity}</mwc-list-item
>`
)}
</ha-select>
</div>
<div class="option">
<ha-select
.label=${localize('editor.map')}
@selected=${this.valueChanged}
.configValue=${'map'}
.value=${this.config.map}
@closed=${(e: Event) => e.stopPropagation()}
fixedMenuPosition
naturalMenuWidth
>
${cameraEntities.map(
(entity) => html` <mwc-list-item .value=${entity}
>${entity}</mwc-list-item
>`
)}
</ha-select>
</div>
<div class="option">
<paper-input
label="${localize('editor.image')}"
.value=${this.image}
.configValue=${'image'}
@value-changed=${this.valueChanged}
></paper-input>
</div>
<div class="option">
<ha-switch
aria-label=${localize(
this.compact_view
? 'editor.compact_view_aria_label_off'
: 'editor.compact_view_aria_label_on'
)}
.checked=${Boolean(this.compact_view)}
.configValue=${'compact_view'}
@change=${this.valueChanged}
>
</ha-switch>
${localize('editor.compact_view')}
</div>
<div class="option">
<ha-switch
aria-label=${localize(
this.show_name
? 'editor.show_name_aria_label_off'
: 'editor.show_name_aria_label_on'
)}
.checked=${Boolean(this.show_name)}
.configValue=${'show_name'}
@change=${this.valueChanged}
>
</ha-switch>
${localize('editor.show_name')}
</div>
<div class="option">
<ha-switch
aria-label=${localize(
this.show_status
? 'editor.show_status_aria_label_off'
: 'editor.show_status_aria_label_on'
)}
.checked=${Boolean(this.show_status)}
.configValue=${'show_status'}
@change=${this.valueChanged}
>
</ha-switch>
${localize('editor.show_status')}
</div>
<div class="option">
<ha-switch
aria-label=${localize(
this.show_toolbar
? 'editor.show_toolbar_aria_label_off'
: 'editor.show_toolbar_aria_label_on'
)}
.checked=${Boolean(this.show_toolbar)}
.configValue=${'show_toolbar'}
@change=${this.valueChanged}
>
</ha-switch>
${localize('editor.show_toolbar')}
</div>
<strong>${localize('editor.code_only_note')}</strong>
</div>
`;
}
private valueChanged(event: Event): void {
if (!this.config || !this.hass || !event.target) {
return;
}
const target = event.target as ConfigElement;
if (
!target.configValue ||
this.config[target.configValue] === target?.value
) {
return;
}
if (target.configValue) {
if (target.value === '') {
delete this.config[target.configValue];
} else {
this.config = {
...this.config,
[target.configValue]:
target.checked !== undefined ? target.checked : target.value,
};
}
}
fireEvent(this, 'config-changed', { config: this.config });
}
static get styles() {
return styles;
}
}