forked from nielsfaber/scheduler-card
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler-item-row.ts
More file actions
executable file
·211 lines (196 loc) · 6.78 KB
/
scheduler-item-row.ts
File metadata and controls
executable file
·211 lines (196 loc) · 6.78 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
import { CSSResultGroup, LitElement, css, html } from "lit";
import { customElement, property } from "lit/decorators";
import { CardConfig, Schedule } from "../types";
import { computeActionIcon } from "../data/format/compute_action_icon";
import { HomeAssistant } from "../lib/types";
import { computeScheduleDisplay } from "../data/format/compute_schedule_display";
import { unsafeHTML } from 'lit/directives/unsafe-html';
import { computeEntityIcon } from "../data/format/compute_entity_icon";
import { computeDomain } from "../lib/entity";
import './scheduler-relative-time';
import { DEFAULT_PRIMARY_INFO_DISPLAY, DEFAULT_SECONDARY_INFO_DISPLAY } from "../const";
@customElement("scheduler-item-row")
export class SchedulerItemRow extends LitElement {
@property() hass!: HomeAssistant;
@property() schedule_id!: string;
@property() schedule!: Schedule;
@property() config!: CardConfig;
render() {
try {
const stateObj = this.hass.states[this.schedule.entity_id!];
if (!stateObj) return html``;
const disabled = ['off', 'completed'].includes(stateObj.state);
const nextAction = this.schedule.entries[0].slots[this.schedule.next_entries[0] || 0].actions[0];
let icon = computeActionIcon(nextAction, this.config.customize);
if (this.config.display_options?.icon == 'entity') {
let entityId = [nextAction.target?.entity_id || []].flat().shift();
if (['script', 'notify'].includes(computeDomain(nextAction.service))) entityId = nextAction.service;
if (entityId) icon = computeEntityIcon(entityId, this.config.customize, this.hass);
}
const hasRemovedEntity = !([nextAction.target?.entity_id || []].flat()).every(entity_id => Object.keys(this.hass.states).includes(entity_id));
if (hasRemovedEntity) icon = 'mdi:help';
return html`
<ha-icon
icon="${icon}"
@click=${this._handleIconClick}
class="${disabled ? 'disabled' : ''}"
></ha-icon>
<div
class="info ${disabled ? 'disabled' : ''} ${hasRemovedEntity ? 'defective' : ''}"
@click=${this._handleItemClick}
>
${this.renderDisplayItem(this.config.display_options?.primary_info || DEFAULT_PRIMARY_INFO_DISPLAY)}
<div class="secondary">
${this.renderDisplayItem(this.config.display_options?.secondary_info || DEFAULT_SECONDARY_INFO_DISPLAY)}
</div>
</div>
<div class="state">
${this.config.show_toggle_switches !== false
? html`<ha-switch
?checked=${['on', 'triggered'].includes(stateObj.state || '')}
?disabled=${stateObj.state == 'completed'}
@click=${this._toggleEnableDisable}
></ha-switch>`
: ''}
</div>
`;
} catch (e) {
return html`
<hui-warning .hass=${this.hass} @click=${this._handleItemClick}>
<span style="white-space: normal">
Failed to display schedule ${this.schedule.entity_id}.
Reason: ${e}
</span>
</hui-warning>
`;
}
}
private renderDisplayItem(displayItem: string | string[]) {
const replacePreservedTags = (input: string) => {
const parts = input.split('<relative-time></relative-time>');
if (parts.length > 1) {
const ts = this.schedule.timestamps![this.schedule.next_entries[0] || 0];
return html`
${parts[0] ? unsafeHTML(parts[0]) : ''}
<scheduler-relative-time
.hass=${this.hass}
.datetime=${new Date(ts)}
>
</scheduler-relative-time>
${parts[1] ? unsafeHTML(parts[1]) : ''}
`;
}
const res = input.match(/^(<tag>[^<]*<\/tag>)+$/);
if (res !== null) {
let tags = input.split(/<tag>([^<]*)<\/tag>/).filter(e => e);
return html`
<div class="tags">
${tags?.map(e => html`<span class="tag">${e}</span>`)}
</div>`;
}
return unsafeHTML(input);
};
return computeScheduleDisplay(this.schedule, displayItem, this.hass, this.config.customize)
.filter(e => e.length)
.map(e =>
html`${replacePreservedTags(e)}<br/>`
);
}
private _handleItemClick(_ev: Event) {
const myEvent = new CustomEvent('editClick', { detail: { schedule_id: this.schedule_id } });
this.dispatchEvent(myEvent);
}
private _handleIconClick(_ev: Event) {
const myEvent = new CustomEvent('editClick', { detail: { schedule_id: this.schedule_id } });
this.dispatchEvent(myEvent);
}
private _toggleEnableDisable(ev: Event) {
ev.stopPropagation();
const checked = !(ev.target as HTMLInputElement).checked;
this.hass.callService('switch', checked ? 'turn_on' : 'turn_off', { entity_id: this.schedule.entity_id });
}
static get styles(): CSSResultGroup {
return css`
:host {
display: flex;
align-items: center;
flex-direction: row;
}
.info {
margin-left: 16px;
margin-right: 8px;
margin-inline-start: 16px;
margin-inline-end: 8px;
flex: 1 1 30%;
transition: color 0.2s ease-in-out;
cursor: pointer;
}
.info,
.info > * {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.flex ::slotted(*) {
margin-left: 8px;
margin-inline-start: 8px;
margin-inline-end: initial;
min-width: 0;
}
.flex ::slotted([slot="secondary"]) {
margin-left: 0;
margin-inline-start: 0;
margin-inline-end: initial;
}
.secondary,
ha-relative-time {
color: var(--secondary-text-color);
transition: color 0.2s ease-in-out;
}
.state {
text-align: var(--float-end);
}
.value {
direction: ltr;
}
ha-icon {
display: flex;
flex: 0 0 40px;
color: var(--state-icon-color);
transition: color 0.2s ease-in-out;
cursor: pointer;
align-items: center;
justify-content: center;
}
ha-icon.disabled {
color: var(--disabled-text-color);
}
div.disabled {
--primary-text-color: var(--disabled-text-color);
--secondary-text-color: var(--disabled-text-color);
--state-icon-color: var(--disabled-text-color);
color: var(--disabled-text-color);
}
div.tags {
display: flex;
gap: 5px;
flex-wrap: wrap;
}
span.tag {
height: 28px;
border-radius: 14px;
background: rgba(var(--rgb-primary-color), 0.40);
color: var(--primary-text-color);
line-height: 1.25rem;
font-size: 0.875rem;
padding: 0px 12px;
display: flex;
align-items: center;
box-sizing: border-box;
}
.defective {
text-decoration: line-through;
}
`;
}
}