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 QR code element #19155

Merged
merged 4 commits into from
Dec 27, 2023
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
- name: Install dependencies
run: yarn install --immutable
- name: Build resources
run: ./node_modules/.bin/gulp build-translations build-locale-data
run: ./node_modules/.bin/gulp gen-icons-json build-translations build-locale-data
- name: Run Tests
run: yarn run test
build:
Expand Down
1 change: 0 additions & 1 deletion hassio/src/dialogs/datadisk/dialog-hassio-datadisk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { customElement, property, state } from "lit/decorators";
import memoizeOne from "memoize-one";
import { fireEvent } from "../../../../src/common/dom/fire_event";
import "../../../../src/components/ha-circular-progress";
import "../../../../src/components/ha-markdown";
import "../../../../src/components/ha-select";
import {
extractApiErrorMessage,
Expand Down
9 changes: 9 additions & 0 deletions src/components/ha-markdown-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ class HaMarkdownElement extends ReactiveElement {
}
node.firstElementChild!.replaceWith(alertNote);
}
} else if (
node instanceof HTMLElement &&
["ha-alert", "ha-qr-code", "ha-icon", "ha-svg-icon"].includes(
node.localName
)
) {
import(
/* webpackInclude: /(ha-alert)|(ha-qr-code)|(ha-icon)|(ha-svg-icon)/ */ `./${node.localName}`
);
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/components/ha-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ import { css, CSSResultGroup, html, LitElement, nothing } from "lit";
import { customElement, property } from "lit/decorators";
import "./ha-markdown-element";

// Import components that are allwoed to be defined.
import "./ha-alert";
import "./ha-icon";
import "./ha-svg-icon";

@customElement("ha-markdown")
export class HaMarkdown extends LitElement {
@property() public content?;
Expand Down
114 changes: 114 additions & 0 deletions src/components/ha-qr-code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { LitElement, PropertyValues, css, html, nothing } from "lit";
import { customElement, property, query, state } from "lit/decorators";
import QRCode from "qrcode";

@customElement("ha-qr-code")
export class HaQrCode extends LitElement {
@property() public data?: string;

@property({ attribute: "error-correction-level" })
public errorCorrectionLevel: "low" | "medium" | "quartile" | "high" =
"medium";

@property({ type: Number })
public width = 4;

@property({ type: Number })
public scale = 4;

@property({ type: Number })
public margin = 4;

@property({ type: Number }) public maskPattern?:
| 0
| 1
| 2
| 3
| 4
| 5
| 6
| 7;

@property({ attribute: "center-image" }) public centerImage?: string;

@state() private _error?: string;

@query("canvas") private _canvas?: HTMLCanvasElement;

protected willUpdate(changedProperties: PropertyValues): void {
super.willUpdate(changedProperties);
if (
(changedProperties.has("data") ||
changedProperties.has("scale") ||
changedProperties.has("width") ||
changedProperties.has("margin") ||
changedProperties.has("maskPattern") ||
changedProperties.has("errorCorrectionLevel")) &&
this._error
) {
this._error = undefined;
}
}

updated(changedProperties: PropertyValues) {
const canvas = this._canvas;
if (
canvas &&
this.data &&
(changedProperties.has("data") ||
changedProperties.has("scale") ||
changedProperties.has("width") ||
changedProperties.has("margin") ||
changedProperties.has("maskPattern") ||
changedProperties.has("errorCorrectionLevel") ||
changedProperties.has("centerImage"))
) {
const computedStyles = getComputedStyle(this);

QRCode.toCanvas(canvas, this.data, {
errorCorrectionLevel: this.errorCorrectionLevel,
width: this.width,
scale: this.scale,
margin: this.margin,
maskPattern: this.maskPattern,
color: {
light: computedStyles.getPropertyValue("--card-background-color"),
dark: computedStyles.getPropertyValue("--primary-text-color"),
},
}).catch((err) => {
this._error = err.message;
});

if (this.centerImage) {
const context = this._canvas!.getContext("2d");
const imageObj = new Image();
imageObj.src = this.centerImage;
imageObj.onload = () => {
context?.drawImage(
imageObj,
canvas.width * 0.375,
canvas.height * 0.375,
canvas.width / 4,
canvas.height / 4
);
};
}
}
}

render() {
if (!this.data) {
return nothing;
}
if (this._error) {
return html`<ha-alert alert-type="error">${this._error}</ha-alert>`;
}
return html`<canvas></canvas>`;
}

static styles = css`
:host {
display: block;
}
`;
}
1 change: 1 addition & 0 deletions src/panels/config/automation/manual-automation-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ensureArray } from "../../../common/array/ensure-array";
import { fireEvent } from "../../../common/dom/fire_event";
import "../../../components/ha-card";
import "../../../components/ha-icon-button";
import "../../../components/ha-markdown";
import {
Condition,
ManualAutomationConfig,
Expand Down
1 change: 0 additions & 1 deletion src/panels/config/storage/dialog-move-datadisk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import memoizeOne from "memoize-one";
import { fireEvent } from "../../../common/dom/fire_event";
import { stopPropagation } from "../../../common/dom/stop_propagation";
import "../../../components/ha-circular-progress";
import "../../../components/ha-markdown";
import "../../../components/ha-select";
import {
extractApiErrorMessage,
Expand Down
69 changes: 15 additions & 54 deletions src/panels/config/tags/dialog-tag-detail.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import "@material/mwc-button";
import {
css,
CSSResultGroup,
html,
LitElement,
TemplateResult,
nothing,
} from "lit";
import { CSSResultGroup, LitElement, css, html, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import { fireEvent } from "../../../common/dom/fire_event";
import "../../../components/ha-alert";
import { createCloseHeading } from "../../../components/ha-dialog";
import "../../../components/ha-formfield";
import "../../../components/ha-qr-code";
import "../../../components/ha-switch";
import "../../../components/ha-textfield";
import { Tag, UpdateTagParams } from "../../../data/tag";
Expand All @@ -20,8 +14,6 @@ import { haStyleDialog } from "../../../resources/styles";
import { HomeAssistant } from "../../../types";
import { TagDetailDialogParams } from "./show-dialog-tag-detail";

const QR_LOGO_URL = "/static/icons/favicon-192x192.png";

@customElement("dialog-tag-detail")
class DialogTagDetail
extends LitElement
Expand All @@ -39,8 +31,6 @@ class DialogTagDetail

@state() private _submitting = false;

@state() private _qrCode?: TemplateResult;

public showDialog(params: TagDetailDialogParams): void {
this._params = params;
this._error = undefined;
Expand All @@ -50,13 +40,10 @@ class DialogTagDetail
this._id = "";
this._name = "";
}

this._generateQR();
}

public closeDialog(): void {
this._params = undefined;
this._qrCode = undefined;
fireEvent(this, "dialog-closed", { dialog: this.localName });
}

Expand Down Expand Up @@ -130,9 +117,15 @@ class DialogTagDetail
})}
</p>
</div>
${this._qrCode
? html` <div id="qr">${this._qrCode}</div> `
: ""}
<div id="qr">
<ha-qr-code
.data=${this._params!.entry!.id}
center-image="/static/icons/favicon-192x192.png"
error-correction-level="quartile"
scale="5"
>
</ha-qr-code>
</div>
`
: ``}
</div>
Expand All @@ -158,7 +151,7 @@ class DialogTagDetail
: this.hass!.localize("ui.panel.config.tag.detail.create")}
</mwc-button>
${this._params.openWrite && !this._params.entry
? html` <mwc-button
? html`<mwc-button
slot="primaryAction"
@click=${this._updateWriteEntry}
.disabled=${this._submitting || !this._name}
Expand Down Expand Up @@ -221,41 +214,6 @@ class DialogTagDetail
}
}

private async _generateQR() {
const qrcode = await import("qrcode");
const canvas = await qrcode.toCanvas(
`https://www.home-assistant.io/tag/${this._params!.entry!.id}`,
{
width: 180,
errorCorrectionLevel: "Q",
color: {
light: "#fff",
},
}
);
const context = canvas.getContext("2d");

const imageObj = new Image();
imageObj.src = QR_LOGO_URL;
await new Promise((resolve) => {
imageObj.onload = resolve;
});
context?.drawImage(
imageObj,
canvas.width / 3,
canvas.height / 3,
canvas.width / 3,
canvas.height / 3
);

this._qrCode = html`<img
alt=${this.hass.localize("ui.panel.config.tag.qr_code_image", {
name: this._name,
})}
src=${canvas.toDataURL()}
></img>`;
}

static get styles(): CSSResultGroup {
return [
haStyleDialog,
Expand All @@ -270,6 +228,9 @@ class DialogTagDetail
display: block;
margin: 8px 0;
}
::slotted(img) {
height: 100%;
}
`,
];
}
Expand Down
8 changes: 8 additions & 0 deletions src/resources/markdown-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ const renderMarkdown = async (
"ha-icon": ["icon"],
"ha-svg-icon": ["path"],
"ha-alert": ["alert-type", "title"],
"ha-qr-code": [
"data",
"scale",
"width",
"margin",
"error-correction-level",
"center-image",
],
};
}

Expand Down
Loading