-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathhui-calendar-card.ts
More file actions
380 lines (327 loc) · 10.3 KB
/
hui-calendar-card.ts
File metadata and controls
380 lines (327 loc) · 10.3 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import type { UnsubscribeFunc } from "home-assistant-js-websocket";
import type { PropertyValues } from "lit";
import { css, html, LitElement, nothing } from "lit";
import { classMap } from "lit/directives/class-map";
import { customElement, property, state } from "lit/decorators";
import {
computeCssColor,
isValidColorString,
} from "../../../common/color/compute-color";
import { getColorByIndex } from "../../../common/color/colors";
import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_element";
import type { HASSDomEvent } from "../../../common/dom/fire_event";
import { debounce } from "../../../common/util/debounce";
import "../../../components/ha-card";
import "../../../components/ha-spinner";
import type { Calendar, CalendarEvent } from "../../../data/calendar";
import { fetchCalendarEvents } from "../../../data/calendar";
import type { EntityRegistryEntry } from "../../../data/entity/entity_registry";
import { subscribeEntityRegistry } from "../../../data/entity/entity_registry";
import { SubscribeMixin } from "../../../mixins/subscribe-mixin";
import type {
CalendarViewChanged,
FullCalendarView,
HomeAssistant,
} from "../../../types";
import "../../calendar/ha-full-calendar";
import { findEntities } from "../common/find-entities";
import "../components/hui-warning";
import type {
LovelaceCard,
LovelaceCardEditor,
LovelaceGridOptions,
} from "../types";
import type { CalendarCardConfig } from "./types";
@customElement("hui-calendar-card")
export class HuiCalendarCard
extends SubscribeMixin(LitElement)
implements LovelaceCard
{
public static async getConfigElement(): Promise<LovelaceCardEditor> {
await import("../editor/config-elements/hui-calendar-card-editor");
return document.createElement("hui-calendar-card-editor");
}
public static getStubConfig(
hass: HomeAssistant,
entities: string[],
entitiesFill: string[]
) {
const includeDomains = ["calendar"];
const maxEntities = 2;
const foundEntities = findEntities(
hass,
maxEntities,
entities,
entitiesFill,
includeDomains
);
return {
entities: foundEntities,
};
}
@property({ attribute: false }) public hass?: HomeAssistant;
@property({ attribute: false }) public layout?: string;
@state() private _events: CalendarEvent[] = [];
@state() private _config?: CalendarCardConfig;
@state() private _calendars: Calendar[] = [];
@state() private _narrow = false;
@state() private _error?: string = undefined;
@state() private _entityRegistry?: EntityRegistryEntry[];
@state() private _eventsLoaded = false;
private _startDate?: Date;
private _endDate?: Date;
private _resizeObserver?: ResizeObserver;
public setConfig(config: CalendarCardConfig): void {
if (!config.entities?.length) {
throw new Error("Entities must be specified");
}
if (!Array.isArray(config.entities)) {
throw new Error("Entities need to be an array");
}
if (this._config?.entities !== config.entities) {
this._fetchCalendarEvents();
}
this._config = { initial_view: "dayGridMonth", ...config };
}
public willUpdate(changedProps: PropertyValues): void {
super.willUpdate(changedProps);
// Don't build calendars until entity registry is loaded
if (!this._entityRegistry) {
return;
}
// Reset loading state when config changes or entity registry updates
if (changedProps.has("_config") || changedProps.has("_entityRegistry")) {
this._eventsLoaded = false;
}
if (
!this.hasUpdated ||
(changedProps.has("_config") && this._config?.entities) ||
changedProps.has("_entityRegistry")
) {
const computedStyles = getComputedStyle(this);
const entityOptionsMap = new Map(
this._entityRegistry?.map((entry) => [
entry.entity_id,
entry.options,
]) ?? []
);
if (this._config?.entities) {
this._calendars = this._config.entities.map((entity, idx) => {
const entityColor = entityOptionsMap.get(entity)?.calendar?.color;
let backgroundColor: string;
// Validate and use the color from entity registry if valid
if (entityColor && isValidColorString(entityColor)) {
backgroundColor = computeCssColor(entityColor);
} else {
// Fall back to default color by index
backgroundColor = getColorByIndex(idx, computedStyles);
}
return {
entity_id: entity,
backgroundColor,
};
});
}
}
}
public getCardSize(): number {
return 12;
}
public getGridOptions(): LovelaceGridOptions {
return {
rows: 6,
columns: 12,
min_columns: 4,
min_rows: 4,
};
}
public hassSubscribe(): UnsubscribeFunc[] {
return [
subscribeEntityRegistry(this.hass!.connection!, (entities) => {
this._entityRegistry = entities;
}),
];
}
public connectedCallback(): void {
super.connectedCallback();
this.updateComplete.then(() => this._attachObserver());
}
public disconnectedCallback(): void {
super.disconnectedCallback();
if (this._resizeObserver) {
this._resizeObserver.disconnect();
}
}
protected render() {
if (!this._config || !this.hass) {
return nothing;
}
const loading = !this._entityRegistry || !this._eventsLoaded;
const dayView: FullCalendarView = this._config.schedule_day_view
? "timeGridDay"
: "dayGridDay";
const views: FullCalendarView[] = ["dayGridMonth", dayView, "listWeek"];
return html`
<ha-card>
${this._config.title
? html`<div class="header">${this._config.title}</div>`
: nothing}
<ha-full-calendar
class=${classMap({
"is-grid": this.layout === "grid",
"is-panel": this.layout === "panel",
"has-title": !!this._config.title,
loading: loading,
})}
.narrow=${this._narrow}
.events=${this._events}
.calendars=${this._calendars}
.hass=${this.hass}
.views=${views}
.initialView=${this._config.initial_view!}
.error=${this._error}
@view-changed=${this._handleViewChanged}
></ha-full-calendar>
${loading
? html`<div class="loading">
<ha-spinner></ha-spinner>
</div>`
: nothing}
</ha-card>
`;
}
protected updated(changedProps: PropertyValues) {
super.updated(changedProps);
if (!this._config || !this.hass) {
return;
}
// Refetch events when entity registry changes (to update colors)
if (changedProps.has("_entityRegistry") && this._entityRegistry) {
this._fetchCalendarEvents();
}
// If no calendars configured, mark events as loaded to hide spinner
if (
this._entityRegistry &&
!this._calendars.length &&
!this._eventsLoaded
) {
this._eventsLoaded = true;
}
const oldHass = changedProps.get("hass") as HomeAssistant | undefined;
const oldConfig = changedProps.get("_config") as
| CalendarCardConfig
| undefined;
if (
!oldHass ||
!oldConfig ||
(changedProps.has("hass") && oldHass.themes !== this.hass.themes) ||
(changedProps.has("_config") && oldConfig.theme !== this._config.theme)
) {
applyThemesOnElement(this, this.hass.themes, this._config!.theme);
}
}
private _handleViewChanged(ev: HASSDomEvent<CalendarViewChanged>): void {
this._startDate = ev.detail.start;
this._endDate = ev.detail.end;
this._eventsLoaded = false;
this._fetchCalendarEvents();
}
private async _fetchCalendarEvents(): Promise<void> {
if (!this._startDate || !this._endDate) {
return;
}
this._error = undefined;
const result = await fetchCalendarEvents(
this.hass!,
this._startDate,
this._endDate,
this._calendars
);
this._events = result.events;
// Wait for component update and one animation frame for FullCalendar to render
this.updateComplete.then(() => {
requestAnimationFrame(() => {
this._eventsLoaded = true;
});
});
if (result.errors.length > 0) {
this._error = `${this.hass!.localize(
"ui.components.calendar.event_retrieval_error"
)}`;
}
}
private _measureCard() {
const card = this.shadowRoot!.querySelector("ha-card");
if (!card) {
return;
}
this._narrow = card.offsetWidth < 870;
}
private async _attachObserver(): Promise<void> {
if (!this._resizeObserver) {
this._resizeObserver = new ResizeObserver(
debounce(() => this._measureCard(), 250, false)
);
}
const card = this.shadowRoot!.querySelector("ha-card");
// If we show an error or warning there is no ha-card
if (!card) {
return;
}
this._resizeObserver.observe(card);
}
static styles = css`
ha-card {
position: relative;
padding: 0 8px 8px;
box-sizing: border-box;
height: 100%;
overflow: hidden;
}
.header {
color: var(--ha-card-header-color, var(--primary-text-color));
font-size: var(--ha-card-header-font-size, var(--ha-font-size-2xl));
line-height: var(--ha-line-height-condensed);
padding-top: 16px;
padding-left: 8px;
padding-inline-start: 8px;
direction: var(--direction);
white-space: nowrap;
text-overflow: ellipsis;
}
ha-full-calendar {
--calendar-height: 400px;
display: block;
width: 100%;
height: var(--calendar-height);
min-height: var(--calendar-height);
}
ha-full-calendar.loading {
visibility: hidden;
}
ha-full-calendar.is-grid,
ha-full-calendar.is-panel {
--calendar-height: calc(100% - 16px);
}
ha-full-calendar.is-grid.has-title,
ha-full-calendar.is-panel.has-title {
--calendar-height: calc(
100% - var(--ha-card-header-font-size, var(--ha-font-size-2xl)) - 22px
);
}
.loading {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
background: var(--card-background-color, var(--ha-card-background));
z-index: 1;
}
`;
}
declare global {
interface HTMLElementTagNameMap {
"hui-calendar-card": HuiCalendarCard;
}
}