Skip to content
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
3 changes: 3 additions & 0 deletions build-scripts/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ const createWebpackConfig = ({
},
],
},
externals: {
esprima: "esprima",
},
optimization: {
minimizer: [
new TerserPlugin({
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
"@polymer/paper-tooltip": "^3.0.1",
"@polymer/polymer": "3.1.0",
"@thomasloven/round-slider": "0.3.7",
"@types/resize-observer-browser": "^0.1.3",
"@vaadin/vaadin-combo-box": "^5.0.10",
"@vaadin/vaadin-date-picker": "^4.0.7",
"@webcomponents/shadycss": "^1.9.0",
Expand Down Expand Up @@ -136,6 +135,7 @@
"@types/leaflet-draw": "^1.0.1",
"@types/memoize-one": "4.1.0",
"@types/mocha": "^5.2.6",
"@types/resize-observer-browser": "^0.1.3",
"@types/webspeechapi": "^0.0.29",
"@typescript-eslint/eslint-plugin": "^2.28.0",
"@typescript-eslint/parser": "^2.28.0",
Expand Down
2 changes: 1 addition & 1 deletion script/size_stats
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ set -e

cd "$(dirname "$0")/.."

STATS=1 NODE_ENV=production webpack --profile --json > compilation-stats.json
STATS=1 NODE_ENV=production ./node_modules/.bin/webpack --profile --json > compilation-stats.json
npx webpack-bundle-analyzer compilation-stats.json hass_frontend/frontend_latest
rm compilation-stats.json
16 changes: 2 additions & 14 deletions src/panels/lovelace/cards/hui-error-card.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { safeDump } from "js-yaml";
import {
css,
CSSResult,
Expand All @@ -11,18 +10,7 @@ import {
import { HomeAssistant } from "../../../types";
import { LovelaceCard } from "../types";
import { ErrorCardConfig } from "./types";

export const createErrorCardElement = (config: ErrorCardConfig) => {
const el = document.createElement("hui-error-card");
el.setConfig(config);
return el;
};

export const createErrorCardConfig = (error, origConfig) => ({
type: "error",
error,
origConfig,
});
import { safeDump } from "js-yaml";

@customElement("hui-error-card")
export class HuiErrorCard extends LitElement implements LovelaceCard {
Expand All @@ -46,7 +34,7 @@ export class HuiErrorCard extends LitElement implements LovelaceCard {
return html`
${this._config.error}
${this._config.origConfig
? html` <pre>${safeDump(this._config.origConfig)}</pre> `
? html`<pre>${safeDump(this._config.origConfig)}</pre>`
: ""}
`;
}
Expand Down
34 changes: 17 additions & 17 deletions src/panels/lovelace/cards/hui-picture-elements-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { PictureElementsCardConfig } from "./types";

@customElement("hui-picture-elements-card")
class HuiPictureElementsCard extends LitElement implements LovelaceCard {
@property() public hass?: HomeAssistant;

public static getStubConfig(
hass: HomeAssistant,
entities: string[],
Expand Down Expand Up @@ -51,18 +53,6 @@ class HuiPictureElementsCard extends LitElement implements LovelaceCard {

@property() private _config?: PictureElementsCardConfig;

private _hass?: HomeAssistant;

set hass(hass: HomeAssistant) {
this._hass = hass;
for (const el of Array.from(
this.shadowRoot!.querySelectorAll("#root > *")
)) {
const element = el as LovelaceElement;
element.hass = this._hass;
}
}

public getCardSize(): number {
return 4;
}
Expand All @@ -84,9 +74,19 @@ class HuiPictureElementsCard extends LitElement implements LovelaceCard {

protected updated(changedProps: PropertyValues): void {
super.updated(changedProps);
if (!this._config || !this._hass) {
if (!this._config || !this.hass) {
return;
}

if (changedProps.has("hass")) {
for (const el of Array.from(
this.shadowRoot!.querySelectorAll("#root > *")
)) {
const element = el as LovelaceElement;
element.hass = this.hass;
}
}

const oldHass = changedProps.get("hass") as HomeAssistant | undefined;
const oldConfig = changedProps.get("_config") as
| PictureElementsCardConfig
Expand All @@ -98,20 +98,20 @@ class HuiPictureElementsCard extends LitElement implements LovelaceCard {
oldHass.themes !== this.hass.themes ||
oldConfig.theme !== this._config.theme
) {
applyThemesOnElement(this, this._hass.themes, this._config.theme);
applyThemesOnElement(this, this.hass.themes, this._config.theme);
}
}

protected render(): TemplateResult {
if (!this._config) {
if (!this.hass || !this._config) {
return html``;
}

return html`
<ha-card .header=${this._config.title}>
<div id="root">
<hui-image
.hass=${this._hass}
.hass=${this.hass}
.image=${this._config.image}
.stateImage=${this._config.state_image}
.stateFilter=${this._config.state_filter}
Expand All @@ -123,7 +123,7 @@ class HuiPictureElementsCard extends LitElement implements LovelaceCard {
${this._config.elements.map(
(elementConfig: LovelaceElementConfig) => {
const element = createStyledHuiElement(elementConfig);
element.hass = this._hass;
element.hass = this.hass;

return element;
}
Expand Down
11 changes: 6 additions & 5 deletions src/panels/lovelace/components/hui-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ export class HuiImage extends LitElement {
}

protected render(): TemplateResult {
if (!this.hass) {
return html``;
}
const ratio = this.aspectRatio ? parseAspectRatio(this.aspectRatio) : null;
const stateObj =
this.hass && this.entity ? this.hass.states[this.entity] : undefined;
const stateObj = this.entity ? this.hass.states[this.entity] : undefined;
const state = stateObj ? stateObj.state : "unavailable";

// Figure out image source to use
Expand All @@ -84,8 +86,7 @@ export class HuiImage extends LitElement {

if (this.cameraImage) {
if (this.cameraView === "live") {
cameraObj =
this.hass && (this.hass.states[this.cameraImage] as CameraEntity);
cameraObj = this.hass.states[this.cameraImage] as CameraEntity;
} else {
imageSrc = this._cameraImageSrc;
}
Expand All @@ -103,7 +104,7 @@ export class HuiImage extends LitElement {
}

if (imageSrc) {
imageSrc = this.hass!.hassUrl(imageSrc);
imageSrc = this.hass.hassUrl(imageSrc);
}

// Figure out filter to use
Expand Down
2 changes: 1 addition & 1 deletion src/panels/lovelace/create-element/create-card-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const ALWAYS_LOADED_TYPES = new Set([
"entities",
"button",
"entity-button",
"error",
"glance",
"history-graph",
"horizontal-stack",
Expand All @@ -34,6 +33,7 @@ const ALWAYS_LOADED_TYPES = new Set([

const LAZY_LOAD_TYPES = {
"alarm-panel": () => import("../cards/hui-alarm-panel-card"),
error: () => import("../cards/hui-error-card"),
"empty-state": () => import("../cards/hui-empty-state-card"),
"entity-filter": () => import("../cards/hui-entity-filter-card"),
"media-control": () => import("../cards/hui-media-control-card"),
Expand Down
54 changes: 46 additions & 8 deletions src/panels/lovelace/create-element/create-element-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import {
LovelaceCardConfig,
} from "../../../data/lovelace";
import { CUSTOM_TYPE_PREFIX } from "../../../data/lovelace_custom_cards";
import {
createErrorCardConfig,
createErrorCardElement,
HuiErrorCard,
} from "../cards/hui-error-card";
import type { HuiErrorCard } from "../cards/hui-error-card";
import { LovelaceElement, LovelaceElementConfig } from "../elements/types";
import { LovelaceRow, LovelaceRowConfig } from "../entity-rows/types";
import { LovelaceHeaderFooterConfig } from "../header-footer/types";
Expand All @@ -18,6 +14,7 @@ import {
LovelaceCardConstructor,
LovelaceHeaderFooter,
} from "../types";
import type { ErrorCardConfig } from "../cards/types";

const TIMEOUT = 2000;

Expand Down Expand Up @@ -49,6 +46,25 @@ interface CreateElementConfigTypes {
};
}

export const createErrorCardElement = (config: ErrorCardConfig) => {
const el = document.createElement("hui-error-card");
if (customElements.get("hui-error-card")) {
el.setConfig(config);
} else {
import("../cards/hui-error-card");
customElements
.whenDefined("hui-error-card")
.then(() => el.setConfig(config));
}
return el;
};

export const createErrorCardConfig = (error, origConfig) => ({
type: "error",
error,
origConfig,
});

const _createElement = <T extends keyof CreateElementConfigTypes>(
tag: string,
config: CreateElementConfigTypes[T]["config"]
Expand All @@ -73,7 +89,7 @@ const _createErrorElement = <T extends keyof CreateElementConfigTypes>(
config: CreateElementConfigTypes[T]["config"]
): HuiErrorCard => createErrorCardElement(createErrorCardConfig(error, config));

const _maybeCreate = <T extends keyof CreateElementConfigTypes>(
const _customCreate = <T extends keyof CreateElementConfigTypes>(
tag: string,
config: CreateElementConfigTypes[T]["config"]
) => {
Expand All @@ -98,6 +114,28 @@ const _maybeCreate = <T extends keyof CreateElementConfigTypes>(
return element;
};

const _lazyCreate = <T extends keyof CreateElementConfigTypes>(
tag: string,
config: CreateElementConfigTypes[T]["config"]
) => {
if (customElements.get(tag)) {
return _createElement(tag, config);
}
const element = document.createElement(
tag
) as CreateElementConfigTypes[T]["element"];
customElements.whenDefined(tag).then(() => {
try {
// @ts-ignore
element.setConfig(config);
} catch (err) {
// We let it rebuild and the error wil be handled by _createElement
fireEvent(element, "ll-rebuild");
}
});
return element;
};

const _getCustomTag = (type: string) =>
type.startsWith(CUSTOM_TYPE_PREFIX)
? type.substr(CUSTOM_TYPE_PREFIX.length)
Expand Down Expand Up @@ -129,7 +167,7 @@ export const createLovelaceElement = <T extends keyof CreateElementConfigTypes>(
const customTag = config.type ? _getCustomTag(config.type) : undefined;

if (customTag) {
return _maybeCreate(customTag, config);
return _customCreate(customTag, config);
}

let type: string | undefined;
Expand All @@ -152,7 +190,7 @@ export const createLovelaceElement = <T extends keyof CreateElementConfigTypes>(

if (lazyLoadTypes && type in lazyLoadTypes) {
lazyLoadTypes[type]();
return _maybeCreate(tag, config);
return _lazyCreate(tag, config);
}

if (alwaysLoadTypes && alwaysLoadTypes.has(type)) {
Expand Down
2 changes: 1 addition & 1 deletion src/panels/lovelace/editor/card-editor/hui-card-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import "@polymer/paper-input/paper-textarea";
import { computeRTL } from "../../../../common/util/compute_rtl";
import { LovelaceCardConfig } from "../../../../data/lovelace";
import { HomeAssistant } from "../../../../types";
import { createErrorCardConfig } from "../../cards/hui-error-card";
import { createCardElement } from "../../create-element/create-card-element";
import { LovelaceCard } from "../../types";
import { ConfigError } from "../types";
import { createErrorCardConfig } from "../../create-element/create-element-base";

export class HuiCardPreview extends HTMLElement {
private _hass?: HomeAssistant;
Expand Down