Add basic per-method comments.

This commit is contained in:
Dermot Duffy
2021-08-08 22:06:34 -07:00
parent 7f4dfc43e6
commit 8566573297
+29 -8
View File
@@ -3,8 +3,6 @@
// TODO Does each event contain thumbnail? // TODO Does each event contain thumbnail?
// TODO comments per method.
// TODO Check for HA state presence and validity before using it, otherwise warn. // TODO Check for HA state presence and validity before using it, otherwise warn.
// TODO Add material tooltips // TODO Add material tooltips
@@ -64,8 +62,11 @@ enum FrigateCardView {
SNAPSHOTS, // Show the snapshots gallery. SNAPSHOTS, // Show the snapshots gallery.
} }
// Main FrigateCard class.
@customElement('frigate-card') @customElement('frigate-card')
export class FrigateCard extends LitElement { export class FrigateCard extends LitElement {
// Constructor for FrigateCard.
constructor() { constructor() {
super(); super();
this._viewMode = FrigateCardView.LIVE; this._viewMode = FrigateCardView.LIVE;
@@ -73,10 +74,12 @@ export class FrigateCard extends LitElement {
this._interactionTimerID = null; this._interactionTimerID = null;
} }
// Get the configuration element.
public static async getConfigElement(): Promise<LovelaceCardEditor> { public static async getConfigElement(): Promise<LovelaceCardEditor> {
return document.createElement('frigate-card-editor'); return document.createElement('frigate-card-editor');
} }
// Get a stub basic config.
public static getStubConfig(): Record<string, string> { public static getStubConfig(): Record<string, string> {
return {}; return {};
} }
@@ -95,12 +98,13 @@ export class FrigateCard extends LitElement {
protected _interactionTimerID: number | null; protected _interactionTimerID: number | null;
// Set the object configuration.
public setConfig(inputConfig: FrigateCardConfig): void { public setConfig(inputConfig: FrigateCardConfig): void {
if (!inputConfig) { if (!inputConfig) {
throw new Error(localize('common.invalid_configuration:')); throw new Error(localize('common.invalid_configuration:'));
} }
// inputConfig is not extensible, need to make a copy to allow // inputConfig is not "extensible" (i.e. preventExtensions() has been
// modifications. // called on it), need to make a copy to allow modifications.
const cardConfig = Object.assign({ const cardConfig = Object.assign({
name: 'Frigate' name: 'Frigate'
}, inputConfig); }, inputConfig);
@@ -137,7 +141,8 @@ export class FrigateCard extends LitElement {
this.config = cardConfig; this.config = cardConfig;
this._setViewModeToDefault(); this._setViewModeToDefault();
} }
// Set the view mode to the configured default.
protected _setViewModeToDefault(): void { protected _setViewModeToDefault(): void {
if (this.config.view_default == "live") { if (this.config.view_default == "live") {
this._viewMode = FrigateCardView.LIVE; this._viewMode = FrigateCardView.LIVE;
@@ -164,6 +169,7 @@ export class FrigateCard extends LitElement {
// this.renderRoot.appendChild(div); // this.renderRoot.appendChild(div);
// == // ==
// Determine whether the card should be updated.
protected shouldUpdate(changedProps: PropertyValues): boolean { protected shouldUpdate(changedProps: PropertyValues): boolean {
if (!this.config || !this.hass) { if (!this.config || !this.hass) {
return false; return false;
@@ -194,6 +200,7 @@ export class FrigateCard extends LitElement {
return true; return true;
} }
// Get FrigateEvents from the Frigate server API.
protected async _getEvents({ protected async _getEvents({
has_clip = false, has_clip = false,
has_snapshot = false, has_snapshot = false,
@@ -223,6 +230,7 @@ export class FrigateCard extends LitElement {
} }
} }
// Render Frigate events into a card gallery.
protected async _renderEvents() : Promise<TemplateResult> { protected async _renderEvents() : Promise<TemplateResult> {
const want_clips = this._viewMode == FrigateCardView.CLIPS; const want_clips = this._viewMode == FrigateCardView.CLIPS;
@@ -259,6 +267,7 @@ export class FrigateCard extends LitElement {
</ul>`; </ul>`;
} }
// Render a progress spinner while content loads.
protected _renderProgressIndicator(): TemplateResult { protected _renderProgressIndicator(): TemplateResult {
return html` return html`
<div class="frigate-card-exception"> <div class="frigate-card-exception">
@@ -269,6 +278,7 @@ export class FrigateCard extends LitElement {
</div>` </div>`
} }
// Stop/Play video controls.
protected _controlVideos({ protected _controlVideos({
stop, stop,
control_live = false, control_live = false,
@@ -314,6 +324,7 @@ export class FrigateCard extends LitElement {
} }
} }
// Render the main navbar (live, clips, snapshots).
protected _renderNavigationBar(): TemplateResult { protected _renderNavigationBar(): TemplateResult {
return html` return html`
<div class="frigate-card-navbar" > <div class="frigate-card-navbar" >
@@ -345,6 +356,7 @@ export class FrigateCard extends LitElement {
</div>` </div>`
} }
// Render the player for a saved clip.
protected async _renderClipPlayer(): Promise<TemplateResult> { protected async _renderClipPlayer(): Promise<TemplateResult> {
let event: FrigateEvent; let event: FrigateEvent;
if (this._viewEvent) { if (this._viewEvent) {
@@ -371,6 +383,7 @@ export class FrigateCard extends LitElement {
</video>` </video>`
} }
// Render a snapshot.
protected async _renderSnapshotViewer(): Promise<TemplateResult> { protected async _renderSnapshotViewer(): Promise<TemplateResult> {
let event: FrigateEvent; let event: FrigateEvent;
if (this._viewEvent) { if (this._viewEvent) {
@@ -393,6 +406,7 @@ export class FrigateCard extends LitElement {
return html`<img class="frigate-card-viewer" src="${url}">` return html`<img class="frigate-card-viewer" src="${url}">`
} }
// Render the status bar (motion icon).
protected _renderStatusBar(): TemplateResult { protected _renderStatusBar(): TemplateResult {
if (!this.config.motion_entity || !(this.config.motion_entity in this.hass.states)) { if (!this.config.motion_entity || !(this.config.motion_entity in this.hass.states)) {
return html``; return html``;
@@ -410,6 +424,9 @@ export class FrigateCard extends LitElement {
</div>` </div>`
} }
// Render the live viewer.
// Note: The live viewer is the main element used to size the overall card. It
// is always rendered (but sometimes hidden).
protected _renderLiveViewer(): TemplateResult { protected _renderLiveViewer(): TemplateResult {
return html` return html`
<ha-camera-stream <ha-camera-stream
@@ -423,6 +440,7 @@ export class FrigateCard extends LitElement {
</ha-camera-stream>`; </ha-camera-stream>`;
} }
// Record interactions with the card.
protected _interactionHandler(): void { protected _interactionHandler(): void {
if (!this.config.view_timeout) { if (!this.config.view_timeout) {
return; return;
@@ -436,6 +454,7 @@ export class FrigateCard extends LitElement {
}, this.config.view_timeout * 1000); }, this.config.view_timeout * 1000);
} }
// Render the call (master render method).
protected render(): TemplateResult | void { protected render(): TemplateResult | void {
if (this.config.show_warning) { if (this.config.show_warning) {
return this._showWarning(localize('common.show_warning')); return this._showWarning(localize('common.show_warning'));
@@ -443,8 +462,6 @@ export class FrigateCard extends LitElement {
if (this.config.show_error) { if (this.config.show_error) {
return this._showError(localize('common.show_error')); return this._showError(localize('common.show_error'));
} }
// TODO: Add latest snapshot fetch functionality.
return html` return html`
<div <div
class="frigate-card-container" class="frigate-card-container"
@@ -482,12 +499,14 @@ export class FrigateCard extends LitElement {
// } // }
// } // }
// Show a warning card.
private _showWarning(warning: string): TemplateResult { private _showWarning(warning: string): TemplateResult {
return html` return html`
<hui-warning> ${warning} </hui-warning> <hui-warning> ${warning} </hui-warning>
`; `;
} }
// Show an error card.
private _showError(error: string): TemplateResult { private _showError(error: string): TemplateResult {
const errorCard = document.createElement('hui-error-card'); const errorCard = document.createElement('hui-error-card');
errorCard.setConfig({ errorCard.setConfig({
@@ -501,11 +520,13 @@ export class FrigateCard extends LitElement {
`; `;
} }
// Get the CSS styles. CSS is compiled from frigate-card.scss, so this is
// safe to be piped through `unsafeCSS`.
static get styles(): CSSResult { static get styles(): CSSResult {
// CSS is compiled from frigate-card.scss, so this is safe.
return unsafeCSS(style); return unsafeCSS(style);
} }
// Get the Lovelace card size.
static getCardSize(): number { static getCardSize(): number {
return 5; return 5;
} }