Still have manual input if camera is not supported (#10849)

* Still have manual input if camera is not supported

* Adjust & fix
This commit is contained in:
Bram Kragten 2021-12-11 17:12:41 +01:00 committed by GitHub
parent ca6fd6c770
commit bfb84a834f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 13 deletions

View File

@ -1,5 +1,7 @@
import "@material/mwc-list/mwc-list-item"; import "@material/mwc-list/mwc-list-item";
import "@material/mwc-select/mwc-select"; import "@material/mwc-select/mwc-select";
import "@material/mwc-textfield/mwc-textfield";
import type { TextField } from "@material/mwc-textfield/mwc-textfield";
import { mdiCamera } from "@mdi/js"; import { mdiCamera } from "@mdi/js";
import { css, html, LitElement, PropertyValues, TemplateResult } from "lit"; import { css, html, LitElement, PropertyValues, TemplateResult } from "lit";
import { customElement, property, query, state } from "lit/decorators"; import { customElement, property, query, state } from "lit/decorators";
@ -9,6 +11,7 @@ import { stopPropagation } from "../common/dom/stop_propagation";
import { LocalizeFunc } from "../common/translations/localize"; import { LocalizeFunc } from "../common/translations/localize";
import "./ha-alert"; import "./ha-alert";
import "./ha-button-menu"; import "./ha-button-menu";
import "@material/mwc-button/mwc-button";
@customElement("ha-qr-scanner") @customElement("ha-qr-scanner")
class HaQrScanner extends LitElement { class HaQrScanner extends LitElement {
@ -26,6 +29,8 @@ class HaQrScanner extends LitElement {
@query("#canvas-container", true) private _canvasContainer!: HTMLDivElement; @query("#canvas-container", true) private _canvasContainer!: HTMLDivElement;
@query("mwc-textfield") private _manualInput?: TextField;
public disconnectedCallback(): void { public disconnectedCallback(): void {
super.disconnectedCallback(); super.disconnectedCallback();
this._qrNotFoundCount = 0; this._qrNotFoundCount = 0;
@ -74,7 +79,7 @@ class HaQrScanner extends LitElement {
<ha-icon-button <ha-icon-button
slot="trigger" slot="trigger"
.label=${this.localize( .label=${this.localize(
"ui.panel.config.zwave_js.add_node.select_camera" "ui.components.qr-scanner.select_camera"
)} )}
.path=${mdiCamera} .path=${mdiCamera}
></ha-icon-button> ></ha-icon-button>
@ -90,11 +95,22 @@ class HaQrScanner extends LitElement {
</ha-button-menu>` </ha-button-menu>`
: ""} : ""}
</div>` </div>`
: html`<ha-alert alert-type="warning" : html`<ha-alert alert-type="warning">
>${!window.isSecureContext ${!window.isSecureContext
? "You can only use your camera to scan a QR core when using HTTPS." ? this.localize("ui.components.qr-scanner.only_https_supported")
: "Your browser doesn't support QR scanning."}</ha-alert : this.localize("ui.components.qr-scanner.not_supported")}
>`}`; </ha-alert>
<p>${this.localize("ui.components.qr-scanner.manual_input")}</p>
<div class="row">
<mwc-textfield
.label=${this.localize("ui.components.qr-scanner.enter_qr_code")}
@keyup=${this._manualKeyup}
@paste=${this._manualPaste}
></mwc-textfield>
<mwc-button @click=${this._manualSubmit}
>${this.localize("ui.common.submit")}</mwc-button
>
</div>`}`;
} }
private async _loadQrScanner() { private async _loadQrScanner() {
@ -143,6 +159,23 @@ class HaQrScanner extends LitElement {
fireEvent(this, "qr-code-scanned", { value: qrCodeString }); fireEvent(this, "qr-code-scanned", { value: qrCodeString });
}; };
private _manualKeyup(ev: KeyboardEvent) {
if (ev.key === "Enter") {
this._qrCodeScanned((ev.target as TextField).value);
}
}
private _manualPaste(ev: ClipboardEvent) {
this._qrCodeScanned(
// @ts-ignore
(ev.clipboardData || window.clipboardData).getData("text")
);
}
private _manualSubmit() {
this._qrCodeScanned(this._manualInput!.value);
}
private _cameraChanged(ev: CustomEvent): void { private _cameraChanged(ev: CustomEvent): void {
this._qrScanner?.setCamera((ev.target as any).value); this._qrScanner?.setCamera((ev.target as any).value);
} }
@ -162,6 +195,14 @@ class HaQrScanner extends LitElement {
color: white; color: white;
border-radius: 50%; border-radius: 50%;
} }
.row {
display: flex;
align-items: center;
}
mwc-textfield {
flex: 1;
margin-right: 8px;
}
`; `;
} }

View File

@ -178,7 +178,10 @@ class DialogZWaveJSAddNode extends LitElement {
Search device Search device
</mwc-button>` </mwc-button>`
: this._status === "qr_scan" : this._status === "qr_scan"
? html`<ha-qr-scanner ? html`${this._error
? html`<ha-alert alert-type="error">${this._error}</ha-alert>`
: ""}
<ha-qr-scanner
.localize=${this.hass.localize} .localize=${this.hass.localize}
@qr-code-scanned=${this._qrCodeScanned} @qr-code-scanned=${this._qrCodeScanned}
></ha-qr-scanner> ></ha-qr-scanner>
@ -194,9 +197,9 @@ class DialogZWaveJSAddNode extends LitElement {
</p> </p>
${ ${
this._error this._error
? html`<ha-alert alert-type="error" ? html`<ha-alert alert-type="error">
>${this._error}</ha-alert ${this._error}
>` </ha-alert>`
: "" : ""
} }
<div class="flex-container"> <div class="flex-container">
@ -614,7 +617,6 @@ class DialogZWaveJSAddNode extends LitElement {
ZWaveFeature.SmartStart ZWaveFeature.SmartStart
) )
).supported; ).supported;
this._supportsSmartStart = true;
} }
private _startOver(_ev: Event) { private _startOver(_ev: Event) {

View File

@ -291,6 +291,7 @@
"undo": "Undo", "undo": "Undo",
"move": "Move", "move": "Move",
"save": "Save", "save": "Save",
"submit": "Submit",
"rename": "Rename", "rename": "Rename",
"yes": "Yes", "yes": "Yes",
"no": "No", "no": "No",
@ -538,6 +539,13 @@
}, },
"attributes": { "attributes": {
"expansion_header": "Attributes" "expansion_header": "Attributes"
},
"qr-scanner": {
"select_camera": "Select camera",
"only_https_supported": "You can only use your camera to scan a QR code when using HTTPS.",
"not_supported": "Your browser doesn't support QR scanning.",
"manual_input": "You can scan the QR code with another QR scanner and paste the code in the input below",
"enter_qr_code": "Enter QR code value"
} }
}, },
"dialogs": { "dialogs": {
@ -2920,8 +2928,6 @@
"qr_code": "QR Code", "qr_code": "QR Code",
"qr_code_paragraph": "If your device supports SmartStart you can scan the QR code for easy pairing.", "qr_code_paragraph": "If your device supports SmartStart you can scan the QR code for easy pairing.",
"scan_qr_code": "Scan QR code", "scan_qr_code": "Scan QR code",
"enter_qr_code": "Enter QR code value",
"select_camera": "Select camera",
"inclusion_failed": "The device could not be added.", "inclusion_failed": "The device could not be added.",
"check_logs": "Please check the logs for more information.", "check_logs": "Please check the logs for more information.",
"inclusion_finished": "The device has been added.", "inclusion_finished": "The device has been added.",