Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stream preview element to generic camera #21463

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
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
1 change: 1 addition & 0 deletions public/static/icons/spinner-48x48.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 9 additions & 1 deletion src/components/ha-hls-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,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[3], 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[3], 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 type { UnsubscribeFunc } from "home-assistant-js-websocket";
import type { HomeAssistant } from "../types";

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

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

@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 @@ -26,9 +26,9 @@ class FlowPreviewGeneric extends LitElement {

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

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

@state() private _error?: string;
@state() protected _error?: string;

private _unsub?: Promise<UnsubscribeFunc>;

Expand Down
39 changes: 39 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,39 @@
import { html } from "lit";
import { customElement } from "lit/decorators";
import { FlowPreviewGeneric } from "./flow-preview-generic";
import "../../../components/ha-hls-player";

@customElement("flow-preview-generic_camera")
class FlowPreviewGenericCamera extends FlowPreviewGeneric {
protected override render() {
if (!this._preview) {
return html`<ha-alert alert-type="error">${this._error}</ha-alert>`;
}

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

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}
.url=${streamUrl}
posterUrl="/static/icons/spinner-48x48.svg"
></ha-hls-player>`
: ""}`;
}
}

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