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

Update Types for various cards #1920

Merged
merged 6 commits into from
Oct 31, 2018
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
14 changes: 7 additions & 7 deletions src/panels/lovelace/cards/hui-error-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ interface Config extends LovelaceConfig {
}

class HuiErrorCard extends LitElement implements LovelaceCard {
protected config?: Config;
private _config?: Config;

static get properties() {
return {
config: {},
_config: {},
};
}

public getCardSize(): number {
return 4;
}

public setConfig(config): void {
this.config = config;
public setConfig(config: Config): void {
this._config = config;
}

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

return html`
${this.renderStyle()}
${this.config.error}
<pre>${this._toStr(this.config.origConfig)}</pre>
${this._config.error}
<pre>${this._toStr(this._config.origConfig)}</pre>
`;
}

Expand Down
31 changes: 19 additions & 12 deletions src/panels/lovelace/cards/hui-gauge-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
} from "@polymer/lit-element";
import { LovelaceCard, LovelaceConfig } from "../types.js";
import { HomeAssistant } from "../../../types.js";
import isValidEntityId from "../../../common/entity/valid_entity_id.js";
import { fireEvent } from "../../../common/dom/fire_event.js";
import { TemplateResult } from "lit-html";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you moved it a couple of lines up, however it's still not part of the absolute imports. Absolute imports are the ones where the path does not start with a . and so we import from node_modules

import isValidEntityId from "../../../common/entity/valid_entity_id.js";

import "../../../components/ha-card.js";

Expand Down Expand Up @@ -38,11 +39,11 @@ class HuiGaugeCard extends LitElement implements LovelaceCard {
};
}

public getCardSize() {
public getCardSize(): number {
return 2;
}

public setConfig(config) {
public setConfig(config: Config): void {
if (!config || !config.entity) {
throw new Error("Invalid card configuration");
}
Expand All @@ -52,7 +53,7 @@ class HuiGaugeCard extends LitElement implements LovelaceCard {
this._config = { min: 0, max: 100, ...config };
}

protected render() {
protected render(): TemplateResult {
if (!this._config || !this.hass) {
return html``;
}
Expand Down Expand Up @@ -92,7 +93,7 @@ class HuiGaugeCard extends LitElement implements LovelaceCard {
`;
}

protected shouldUpdate(changedProps: PropertyValues) {
protected shouldUpdate(changedProps: PropertyValues): boolean {
if (changedProps.get("hass")) {
return (
(changedProps.get("hass") as any).states[this._config!.entity] !==
Expand All @@ -102,10 +103,10 @@ class HuiGaugeCard extends LitElement implements LovelaceCard {
if (changedProps.get("_config")) {
return changedProps.get("_config") !== this._config;
}
return (changedProps as unknown) as boolean;
return true;
}

protected updated() {
protected updated(): void {
if (
!this._config ||
!this.hass ||
Expand Down Expand Up @@ -135,7 +136,7 @@ class HuiGaugeCard extends LitElement implements LovelaceCard {
);
}

private renderStyle() {
private renderStyle(): TemplateResult {
return html`
<style>
ha-card {
Expand Down Expand Up @@ -215,7 +216,7 @@ class HuiGaugeCard extends LitElement implements LovelaceCard {
`;
}

private _computeSeverity(stateValue: string, sections: object) {
private _computeSeverity(stateValue: string, sections: object): string {
const numberValue = Number(stateValue);

if (!sections) {
Expand Down Expand Up @@ -247,20 +248,26 @@ class HuiGaugeCard extends LitElement implements LovelaceCard {
return severityMap.normal;
}

private _translateTurn(value: number, config: Config) {
private _translateTurn(value: number, config: Config): number {
const maxTurnValue = Math.min(Math.max(value, config.min!), config.max!);
return (
(5 * (maxTurnValue - config.min!)) / (config.max! - config.min!) / 10
);
}

private _computeBaseUnit() {
private _computeBaseUnit(): string {
return this.clientWidth < 200 ? this.clientWidth / 5 + "px" : "50px";
}

private _handleClick() {
private _handleClick(): void {
fireEvent(this, "hass-more-info", { entityId: this._config!.entity });
}
}
zsarnett marked this conversation as resolved.
Show resolved Hide resolved

declare global {
interface HTMLElementTagNameMap {
"hui-gauge-card": HuiGaugeCard;
}
}

customElements.define("hui-gauge-card", HuiGaugeCard);
27 changes: 14 additions & 13 deletions src/panels/lovelace/cards/hui-iframe-card.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { html, LitElement } from "@polymer/lit-element";
import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element";

import "../../../components/ha-card.js";

import { LovelaceCard, LovelaceConfig } from "../types.js";
import { TemplateResult } from "lit-html";

interface Config extends LovelaceConfig {
aspect_ratio?: string;
Expand All @@ -11,42 +12,42 @@ interface Config extends LovelaceConfig {
}

export class HuiIframeCard extends LitElement implements LovelaceCard {
protected config?: Config;
protected _config?: Config;

static get properties() {
static get properties(): PropertyDeclarations {
return {
config: {},
_config: {},
};
}

public getCardSize() {
public getCardSize(): number {
return 1 + this.offsetHeight / 50;
}

public setConfig(config: Config) {
public setConfig(config: Config): void {
if (!config.url) {
throw new Error("URL required");
}

this.config = config;
this._config = config;
}

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

return html`
${this.renderStyle()}
<ha-card .header="${this.config.title}">
<ha-card .header="${this._config.title}">
<div id="root">
<iframe src="${this.config.url}"></iframe>
<iframe src="${this._config.url}"></iframe>
</div>
</ha-card>
`;
}

private renderStyle() {
private renderStyle(): TemplateResult {
return html`
<style>
ha-card {
Expand All @@ -55,7 +56,7 @@ export class HuiIframeCard extends LitElement implements LovelaceCard {
#root {
width: 100%;
position: relative;
padding-top: ${this.config!.aspect_ratio || "50%"};
padding-top: ${this._config!.aspect_ratio || "50%"};
}
iframe {
position: absolute;
Expand Down
29 changes: 15 additions & 14 deletions src/panels/lovelace/cards/hui-markdown-card.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,57 @@
import { html, LitElement } from "@polymer/lit-element";
import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element";
import { classMap } from "lit-html/directives/classMap.js";

import "../../../components/ha-card.js";
import "../../../components/ha-markdown.js";

import { LovelaceCard, LovelaceConfig } from "../types.js";
import { TemplateResult } from "lit-html";

interface Config extends LovelaceConfig {
content: string;
title?: string;
}

export class HuiMarkdownCard extends LitElement implements LovelaceCard {
protected config?: Config;
private _config?: Config;

static get properties() {
static get properties(): PropertyDeclarations {
return {
config: {},
_config: {},
};
}

public getCardSize() {
return this.config!.content.split("\n").length;
public getCardSize(): number {
return this._config!.content.split("\n").length;
}

public setConfig(config: Config) {
public setConfig(config: Config): void {
if (!config.content) {
throw new Error("Invalid Configuration: Content Required");
}

this.config = config;
this._config = config;
}

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

return html`
${this.renderStyle()}
<ha-card .header="${this.config.title}">
<ha-card .header="${this._config.title}">
<ha-markdown
class="markdown ${classMap({
"no-header": !this.config.title,
"no-header": !this._config.title,
})}"
.content="${this.config.content}"
.content="${this._config.content}"
></ha-markdown>
</ha-card>
`;
}

private renderStyle() {
private renderStyle(): TemplateResult {
return html`
<style>
:host {
Expand Down
Loading