mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-23 17:26:42 +00:00
Add some decorators (#1784)
* Add some decorators * Disable sort keys * Add babel plugins * Update typescript to 7.1
This commit is contained in:
parent
af81ede100
commit
a7684d7206
@ -20,6 +20,9 @@ module.exports.babelLoaderConfig = ({ latestBuild }) => {
|
|||||||
"@babel/plugin-proposal-object-rest-spread",
|
"@babel/plugin-proposal-object-rest-spread",
|
||||||
{ loose: true, useBuiltIns: true },
|
{ loose: true, useBuiltIns: true },
|
||||||
],
|
],
|
||||||
|
// Used for decorators in typescript
|
||||||
|
["@babel/plugin-proposal-decorators", { legacy: true }],
|
||||||
|
"@babel/plugin-proposal-class-properties",
|
||||||
// Only support the syntax, Webpack will handle it.
|
// Only support the syntax, Webpack will handle it.
|
||||||
"@babel/syntax-dynamic-import",
|
"@babel/syntax-dynamic-import",
|
||||||
[
|
[
|
||||||
|
@ -91,7 +91,8 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/core": "^7.1.2",
|
"@babel/core": "^7.1.2",
|
||||||
"@babel/plugin-external-helpers": "^7.0.0",
|
"@babel/plugin-external-helpers": "^7.0.0",
|
||||||
"@babel/plugin-proposal-class-properties": "7.0.0",
|
"@babel/plugin-proposal-class-properties": "^7.1.0",
|
||||||
|
"@babel/plugin-proposal-decorators": "^7.1.2",
|
||||||
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
|
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
|
||||||
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
|
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
|
||||||
"@babel/plugin-transform-react-jsx": "^7.0.0",
|
"@babel/plugin-transform-react-jsx": "^7.0.0",
|
||||||
|
@ -11,13 +11,13 @@ import {
|
|||||||
LocalizeMixin,
|
LocalizeMixin,
|
||||||
} from "./localize-base-mixin";
|
} from "./localize-base-mixin";
|
||||||
|
|
||||||
export const HassLocalizeLitMixin = (
|
export const HassLocalizeLitMixin = <T extends LitElement>(
|
||||||
superClass: Constructor<LitElement>
|
superClass: Constructor<T>
|
||||||
): Constructor<LitElement & LocalizeMixin> =>
|
): Constructor<T & LocalizeMixin> =>
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
class extends LocalizeBaseMixin(superClass) {
|
class extends LocalizeBaseMixin(superClass) {
|
||||||
protected hass?: HomeAssistant;
|
protected hass?: HomeAssistant;
|
||||||
protected localize?: LocalizeFunc;
|
protected localize!: LocalizeFunc;
|
||||||
|
|
||||||
static get properties(): PropertyDeclarations {
|
static get properties(): PropertyDeclarations {
|
||||||
return {
|
return {
|
||||||
|
@ -1,4 +1,10 @@
|
|||||||
import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element";
|
import {
|
||||||
|
html,
|
||||||
|
LitElement,
|
||||||
|
property,
|
||||||
|
customElement,
|
||||||
|
eventOptions,
|
||||||
|
} from "@polymer/lit-element";
|
||||||
import { fireEvent } from "../../../common/dom/fire_event.js";
|
import { fireEvent } from "../../../common/dom/fire_event.js";
|
||||||
|
|
||||||
import "../../../components/ha-card.js";
|
import "../../../components/ha-card.js";
|
||||||
@ -22,15 +28,13 @@ interface Config extends LovelaceConfig {
|
|||||||
service_data?: object;
|
service_data?: object;
|
||||||
}
|
}
|
||||||
|
|
||||||
class HuiEntityButtonCard extends HassLocalizeLitMixin(LitElement)
|
@customElement("hui-entity-button-card" as any)
|
||||||
implements LovelaceCard {
|
export class HuiEntityButtonCard extends HassLocalizeLitMixin(LitElement)
|
||||||
static get properties(): PropertyDeclarations {
|
implements LovelaceCard {
|
||||||
return {
|
@property()
|
||||||
hass: {}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
protected hass?: HomeAssistant;
|
protected hass?: HomeAssistant;
|
||||||
|
|
||||||
|
@property()
|
||||||
protected config?: Config;
|
protected config?: Config;
|
||||||
|
|
||||||
public getCardSize() {
|
public getCardSize() {
|
||||||
@ -38,13 +42,13 @@ implements LovelaceCard {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public setConfig(config: Config) {
|
public setConfig(config: Config) {
|
||||||
if(!isValidEntityId(config.entity)) {
|
if (!isValidEntityId(config.entity)) {
|
||||||
throw new Error("Invalid Entity");
|
throw new Error("Invalid Entity");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.config = config;
|
this.config = config;
|
||||||
|
|
||||||
if(this.hass) {
|
if (this.hass) {
|
||||||
this.requestUpdate();
|
this.requestUpdate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -60,18 +64,26 @@ implements LovelaceCard {
|
|||||||
<ha-card @click="${this.handleClick}">
|
<ha-card @click="${this.handleClick}">
|
||||||
${
|
${
|
||||||
!stateObj
|
!stateObj
|
||||||
? html`<div class="not-found">Entity not available: ${this.config.entity}</div>`
|
? html`<div class="not-found">Entity not available: ${
|
||||||
|
this.config.entity
|
||||||
|
}</div>`
|
||||||
: html`
|
: html`
|
||||||
<paper-button>
|
<paper-button>
|
||||||
<div>
|
<div>
|
||||||
<ha-icon
|
<ha-icon
|
||||||
data-domain="${computeStateDomain(stateObj)}"
|
data-domain="${computeStateDomain(stateObj)}"
|
||||||
data-state="${stateObj.state}"
|
data-state="${stateObj.state}"
|
||||||
.icon="${this.config.icon ? this.config.icon : stateIcon(stateObj)}"
|
.icon="${
|
||||||
style="${styleMap({filter: this._computeBrightness(stateObj), color: this._computeColor(stateObj)})}"
|
this.config.icon ? this.config.icon : stateIcon(stateObj)
|
||||||
|
}"
|
||||||
|
style="${styleMap({
|
||||||
|
filter: this._computeBrightness(stateObj),
|
||||||
|
color: this._computeColor(stateObj),
|
||||||
|
})}"
|
||||||
></ha-icon>
|
></ha-icon>
|
||||||
<span>
|
<span>
|
||||||
${this.config.name
|
${
|
||||||
|
this.config.name
|
||||||
? this.config.name
|
? this.config.name
|
||||||
: computeStateName(stateObj)
|
: computeStateName(stateObj)
|
||||||
}
|
}
|
||||||
@ -134,23 +146,24 @@ implements LovelaceCard {
|
|||||||
|
|
||||||
private _computeColor(stateObj) {
|
private _computeColor(stateObj) {
|
||||||
if (!stateObj.attributes.hs_color) {
|
if (!stateObj.attributes.hs_color) {
|
||||||
return '';
|
return "";
|
||||||
}
|
}
|
||||||
const hue = stateObj.attributes.hs_color[0];
|
const hue = stateObj.attributes.hs_color[0];
|
||||||
const sat = stateObj.attributes.hs_color[1];
|
const sat = stateObj.attributes.hs_color[1];
|
||||||
if (sat <= 10) {
|
if (sat <= 10) {
|
||||||
return '';
|
return "";
|
||||||
}
|
}
|
||||||
return `hsl(${hue}, 100%, ${100 - sat / 2}%)`;
|
return `hsl(${hue}, 100%, ${100 - sat / 2}%)`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@eventOptions({ passive: true })
|
||||||
private handleClick() {
|
private handleClick() {
|
||||||
const config = this.config;
|
const config = this.config;
|
||||||
if (!config) {
|
if (!config) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const stateObj = this.hass!.states[config.entity];
|
const stateObj = this.hass!.states[config.entity];
|
||||||
if(!stateObj) {
|
if (!stateObj) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const entityId = stateObj.entity_id;
|
const entityId = stateObj.entity_id;
|
||||||
@ -159,7 +172,7 @@ implements LovelaceCard {
|
|||||||
toggleEntity(this.hass, entityId);
|
toggleEntity(this.hass, entityId);
|
||||||
break;
|
break;
|
||||||
case "call-service": {
|
case "call-service": {
|
||||||
if(!config.service){
|
if (!config.service) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const [domain, service] = config.service.split(".", 2);
|
const [domain, service] = config.service.split(".", 2);
|
||||||
@ -178,5 +191,3 @@ declare global {
|
|||||||
"hui-entity-button-card": HuiEntityButtonCard;
|
"hui-entity-button-card": HuiEntityButtonCard;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
customElements.define("hui-entity-button-card", HuiEntityButtonCard);
|
|
||||||
|
@ -1,4 +1,10 @@
|
|||||||
import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element";
|
import {
|
||||||
|
html,
|
||||||
|
LitElement,
|
||||||
|
customElement,
|
||||||
|
property,
|
||||||
|
eventOptions,
|
||||||
|
} from "@polymer/lit-element";
|
||||||
import { classMap } from "lit-html/directives/classMap.js";
|
import { classMap } from "lit-html/directives/classMap.js";
|
||||||
import { repeat } from "lit-html/directives/repeat";
|
import { repeat } from "lit-html/directives/repeat";
|
||||||
|
|
||||||
@ -35,15 +41,15 @@ interface Config extends LovelaceConfig {
|
|||||||
entities: EntityConfig[];
|
entities: EntityConfig[];
|
||||||
}
|
}
|
||||||
|
|
||||||
class HuiGlanceCard extends HassLocalizeLitMixin(LitElement)
|
@customElement("hui-glance-card" as any)
|
||||||
|
export class HuiGlanceCard extends HassLocalizeLitMixin(LitElement)
|
||||||
implements LovelaceCard {
|
implements LovelaceCard {
|
||||||
static get properties(): PropertyDeclarations {
|
@property()
|
||||||
return {
|
|
||||||
hass: {},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
protected hass?: HomeAssistant;
|
protected hass?: HomeAssistant;
|
||||||
|
|
||||||
|
@property()
|
||||||
protected config?: Config;
|
protected config?: Config;
|
||||||
|
|
||||||
protected configEntities?: EntityConfig[];
|
protected configEntities?: EntityConfig[];
|
||||||
|
|
||||||
public getCardSize() {
|
public getCardSize() {
|
||||||
@ -179,6 +185,7 @@ class HuiGlanceCard extends HassLocalizeLitMixin(LitElement)
|
|||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@eventOptions({ passive: true })
|
||||||
private handleClick(ev: MouseEvent) {
|
private handleClick(ev: MouseEvent) {
|
||||||
const config = (ev.currentTarget as any).entityConf as EntityConfig;
|
const config = (ev.currentTarget as any).entityConf as EntityConfig;
|
||||||
const entityId = config.entity;
|
const entityId = config.entity;
|
||||||
@ -203,5 +210,3 @@ declare global {
|
|||||||
"hui-glance-card": HuiGlanceCard;
|
"hui-glance-card": HuiGlanceCard;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
customElements.define("hui-glance-card", HuiGlanceCard);
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
"rules": {
|
"rules": {
|
||||||
"interface-name": false,
|
"interface-name": false,
|
||||||
"no-submodule-imports": false,
|
"no-submodule-imports": false,
|
||||||
"ordered-imports": false
|
"ordered-imports": false,
|
||||||
|
"object-literal-sort-keys": false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user