Skip to content

Commit

Permalink
Add generic_camera flow preview
Browse files Browse the repository at this point in the history
  • Loading branch information
davet2001 committed Jul 21, 2024
1 parent 0612e25 commit 8459b61
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/components/ha-hls-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,15 @@ class HaHLSPlayer extends LitElement {
let playlist_url: string;
if (match !== null && matchTwice === null) {
// Only send the regular playlist url if we match exactly once
playlist_url = new URL(match[2], this.url).href;
// In case we arrive here with a relative URL, we need to provide a valid
// base/absolute URL to avoid the URL() constructor throwing an error.
let base_url: string;
try {
base_url = new URL(this.url).href;
} catch (error) {
base_url = new URL(this.url, window.location.href).href;
}
playlist_url = new URL(match[2], base_url).href;
} else {
playlist_url = this.url;
}
Expand Down
2 changes: 1 addition & 1 deletion src/data/preview.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { UnsubscribeFunc } from "home-assistant-js-websocket";
import { HomeAssistant } from "../types";

const HAS_CUSTOM_PREVIEW = ["template"];
const HAS_CUSTOM_PREVIEW = ["generic_camera", "template"];

export interface GenericPreview {
state: string;
Expand Down
4 changes: 2 additions & 2 deletions src/dialogs/config-flow/previews/flow-preview-generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { debounce } from "../../../common/util/debounce";
import { fireEvent } from "../../../common/dom/fire_event";

@customElement("flow-preview-generic")
class FlowPreviewGeneric extends LitElement {
export class FlowPreviewGeneric extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;

@property() public flowType!: FlowType;
Expand All @@ -24,7 +24,7 @@ class FlowPreviewGeneric extends LitElement {

@property() public stepData!: Record<string, any>;

@state() private _preview?: HassEntity;
@state() protected _preview?: HassEntity;

@state() private _error?: string;

Expand Down
38 changes: 38 additions & 0 deletions src/dialogs/config-flow/previews/flow-preview-generic_camera.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { html, nothing } from "lit";
import { customElement } from "lit/decorators";
import { FlowPreviewGeneric } from "./flow-preview-generic";

@customElement("flow-preview-generic_camera")
class FlowPreviewGenericCamera extends FlowPreviewGeneric {
protected override render() {
if (!this._preview) {
return nothing;
}

const stillUrl = this._preview.attributes.stillUrl;
const streamUrl = this._preview.attributes.streamUrl;

return html` ${stillUrl
? html`<p>Still image:</p>
<p>
<img src=${stillUrl} alt="Still preview" />
</p>`
: ""}
${streamUrl
? html`<p>Stream:</p>
<ha-hls-player
autoplay
playsinline
.hass=${this.hass}
.controls=${false}
.url=${streamUrl}
></ha-hls-player>`
: ""}`;
}
}

declare global {
interface HTMLElementTagNameMap {
"flow-preview-generic_camera": FlowPreviewGenericCamera;
}
}

0 comments on commit 8459b61

Please sign in to comment.