Skip to content
Open
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
35 changes: 35 additions & 0 deletions src/panels/calendar/ha-panel-calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class PanelCalendar extends LitElement {

private _headerHeight = 56;

private _refreshInterval?: number;

// Refresh interval in milliseconds (5 minutes)
private static readonly REFRESH_INTERVAL_MS = 5 * 60 * 1000;

public connectedCallback() {
super.connectedCallback();
this._mql = window.matchMedia(
Expand All @@ -72,12 +77,14 @@ class PanelCalendar extends LitElement {
this._headerHeight = Number(
computedStyles.getPropertyValue("--header-height").replace("px", "")
);
this._startRefreshInterval();
}

public disconnectedCallback() {
super.disconnectedCallback();
this._mql?.removeListener(this._setIsMobile!);
this._mql = undefined;
this._stopRefreshInterval();
}

private _setIsMobile = (ev: MediaQueryListEvent) => {
Expand All @@ -89,6 +96,20 @@ class PanelCalendar extends LitElement {
if (!this.hasUpdated) {
this._calendars = getCalendars(this.hass);
}

// Refresh calendar events if any of the calendar entities' states have changed
if (changedProps.has("hass") && this.hasUpdated) {
const oldHass = changedProps.get("hass") as HomeAssistant | undefined;
if (oldHass) {
const calendarStateChanged = this._calendars.some(
(cal) =>
oldHass.states[cal.entity_id] !== this.hass.states[cal.entity_id]
);
if (calendarStateChanged) {
this._handleRefresh();
}
}
}
}

protected render(): TemplateResult {
Expand Down Expand Up @@ -290,6 +311,20 @@ class PanelCalendar extends LitElement {
}
}

private _startRefreshInterval(): void {
this._stopRefreshInterval();
this._refreshInterval = window.setInterval(() => {
this._handleRefresh();
}, PanelCalendar.REFRESH_INTERVAL_MS);
}

private _stopRefreshInterval(): void {
if (this._refreshInterval !== undefined) {
clearInterval(this._refreshInterval);
this._refreshInterval = undefined;
}
}

static get styles(): CSSResultGroup {
return [
haStyle,
Expand Down
32 changes: 32 additions & 0 deletions src/panels/lovelace/cards/hui-calendar-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ export class HuiCalendarCard extends LitElement implements LovelaceCard {

private _resizeObserver?: ResizeObserver;

private _refreshInterval?: number;

// Refresh interval in milliseconds (5 minutes)
private static readonly REFRESH_INTERVAL_MS = 5 * 60 * 1000;

public setConfig(config: CalendarCardConfig): void {
if (!config.entities?.length) {
throw new Error("Entities must be specified");
Expand Down Expand Up @@ -110,13 +115,15 @@ export class HuiCalendarCard extends LitElement implements LovelaceCard {
public connectedCallback(): void {
super.connectedCallback();
this.updateComplete.then(() => this._attachObserver());
this._startRefreshInterval();
}

public disconnectedCallback(): void {
super.disconnectedCallback();
if (this._resizeObserver) {
this._resizeObserver.disconnect();
}
this._stopRefreshInterval();
}

protected render() {
Expand Down Expand Up @@ -170,6 +177,17 @@ export class HuiCalendarCard extends LitElement implements LovelaceCard {
) {
applyThemesOnElement(this, this.hass.themes, this._config!.theme);
}

// Refresh calendar events if any of the calendar entities' states have changed
if (changedProps.has("hass") && oldHass) {
const calendarStateChanged = this._calendars.some(
(cal) =>
oldHass.states[cal.entity_id] !== this.hass!.states[cal.entity_id]
);
if (calendarStateChanged) {
this._fetchCalendarEvents();
}
}
}

private _handleViewChanged(ev: HASSDomEvent<CalendarViewChanged>): void {
Expand Down Expand Up @@ -223,6 +241,20 @@ export class HuiCalendarCard extends LitElement implements LovelaceCard {
this._resizeObserver.observe(card);
}

private _startRefreshInterval(): void {
this._stopRefreshInterval();
this._refreshInterval = window.setInterval(() => {
this._fetchCalendarEvents();
}, HuiCalendarCard.REFRESH_INTERVAL_MS);
}

private _stopRefreshInterval(): void {
if (this._refreshInterval !== undefined) {
clearInterval(this._refreshInterval);
this._refreshInterval = undefined;
}
}

static styles = css`
ha-card {
position: relative;
Expand Down
Loading