From 32edaee192bd047d3fc4ee887f309e71ae678aab Mon Sep 17 00:00:00 2001 From: Dermot Duffy Date: Sun, 29 Jan 2023 11:57:07 -0800 Subject: [PATCH] Handle media metadata exceptions. --- src/components/media-filter.ts | 81 +++++++++++++++++++--------------- src/view/view.ts | 2 - 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/src/components/media-filter.ts b/src/components/media-filter.ts index a3a49c4e..36266909 100644 --- a/src/components/media-filter.ts +++ b/src/components/media-filter.ts @@ -34,11 +34,11 @@ import { } from './media-filter-core'; import './surround.js'; import './timeline-core.js'; -import { EventQuery, QueryType, RecordingQuery } from '../camera/types'; +import { EventQuery, MediaMetadata, QueryType, RecordingQuery } from '../camera/types'; import { EventMediaQueries, RecordingMediaQueries } from '../view/media-queries'; import { createViewForEvents, createViewForRecordings } from '../utils/media-to-view.js'; import { HomeAssistant } from 'custom-card-helpers'; -import { prettifyTitle } from '../utils/basic'; +import { errorToConsole, prettifyTitle } from '../utils/basic'; import format from 'date-fns/format'; import parse from 'date-fns/parse'; import endOfMonth from 'date-fns/endOfMonth'; @@ -242,41 +242,50 @@ export class MediaMetadataController implements ReactiveController { } async hostConnected() { - const metadata = await this._cameraManager.getMediaMetadata(this._hass); - if (metadata) { - if (metadata.what) { - this.whatOptions = [...metadata.what] - .sort() - .map((what) => ({ value: what, label: prettifyTitle(what) })); - } - if (metadata.where) { - this.whereOptions = [...metadata.where] - .sort() - .map((where) => ({ value: where, label: prettifyTitle(where) })); - } - if (metadata.days) { - const yearMonths: Set = new Set(); - [...metadata.days].forEach((day) => { - // An efficient conversion: "2023-01-26" -> "2023-01" - yearMonths.add(day.substring(0, 7)); - }); - const monthStarts: Date[] = []; - yearMonths.forEach((yearMonth) => { - monthStarts.push(parse(yearMonth, 'yyyy-MM', new Date())); - }); - this.whenOptions = monthStarts - .sort() - .reverse() - .map((monthStart) => ({ - label: format(monthStart, 'MMMM yyyy'), - value: { - selection: MediaFilterCoreWhen.Custom, - custom: { start: monthStart, end: endOfMonth(monthStart) }, - }, - })); - } - this._host.requestUpdate(); + let metadata: MediaMetadata | null; + try { + metadata = await this._cameraManager.getMediaMetadata(this._hass); + } catch (e) { + errorToConsole(e as Error); + return; } + + if (!metadata) { + return; + } + + if (metadata.what) { + this.whatOptions = [...metadata.what] + .sort() + .map((what) => ({ value: what, label: prettifyTitle(what) })); + } + if (metadata.where) { + this.whereOptions = [...metadata.where] + .sort() + .map((where) => ({ value: where, label: prettifyTitle(where) })); + } + if (metadata.days) { + const yearMonths: Set = new Set(); + [...metadata.days].forEach((day) => { + // An efficient conversion: "2023-01-26" -> "2023-01" + yearMonths.add(day.substring(0, 7)); + }); + const monthStarts: Date[] = []; + yearMonths.forEach((yearMonth) => { + monthStarts.push(parse(yearMonth, 'yyyy-MM', new Date())); + }); + this.whenOptions = monthStarts + .sort() + .reverse() + .map((monthStart) => ({ + label: format(monthStart, 'MMMM yyyy'), + value: { + selection: MediaFilterCoreWhen.Custom, + custom: { start: monthStart, end: endOfMonth(monthStart) }, + }, + })); + } + this._host.requestUpdate(); } } diff --git a/src/view/view.ts b/src/view/view.ts index 1018831c..c098fe45 100644 --- a/src/view/view.ts +++ b/src/view/view.ts @@ -11,8 +11,6 @@ // - TODO: Handle all TODOs in media filter and media filter core. // - TODO: Make media filter slot choice configurable (gallery.ts). // - TODO: Filter panel expands from right can occasionally 'stick' open. -// - TODO: Media type selection, i.e. events or recordings. -// - TODO: Cache every summary. // Hard: // - TODO: Implement gallery.