From 956afe1c3c71005b46e8dd72c15998ecc34952b5 Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Tue, 30 Oct 2018 21:22:52 -0500 Subject: [PATCH 1/6] Convert cover-row to TypeScript/LitElement --- .../lovelace/common/create-row-element.js | 23 +++-- .../entity-rows/hui-cover-entity-row.js | 74 ---------------- .../entity-rows/hui-cover-entity-row.ts | 84 +++++++++++++++++++ 3 files changed, 95 insertions(+), 86 deletions(-) delete mode 100644 src/panels/lovelace/entity-rows/hui-cover-entity-row.js create mode 100644 src/panels/lovelace/entity-rows/hui-cover-entity-row.ts diff --git a/src/panels/lovelace/common/create-row-element.js b/src/panels/lovelace/common/create-row-element.js index 2d775def5210..4d075782f7b9 100644 --- a/src/panels/lovelace/common/create-row-element.js +++ b/src/panels/lovelace/common/create-row-element.js @@ -2,18 +2,17 @@ import { fireEvent } from "../../../common/dom/fire_event"; import "../entity-rows/hui-climate-entity-row"; import "../entity-rows/hui-cover-entity-row"; -import "../entity-rows/hui-group-entity-row"; -import "../entity-rows/hui-input-number-entity-row"; -import "../entity-rows/hui-input-select-entity-row"; -import "../entity-rows/hui-input-text-entity-row"; -import "../entity-rows/hui-lock-entity-row"; -import "../entity-rows/hui-media-player-entity-row"; -import "../entity-rows/hui-scene-entity-row"; -import "../entity-rows/hui-script-entity-row"; -import "../entity-rows/hui-text-entity-row"; -import "../entity-rows/hui-timer-entity-row"; -import "../entity-rows/hui-toggle-entity-row"; - +import "../entity-rows/hui-group-entity-row.js"; +import "../entity-rows/hui-input-number-entity-row.js"; +import "../entity-rows/hui-input-select-entity-row.js"; +import "../entity-rows/hui-input-text-entity-row.js"; +import "../entity-rows/hui-lock-entity-row.js"; +import "../entity-rows/hui-media-player-entity-row.js"; +import "../entity-rows/hui-scene-entity-row.js"; +import "../entity-rows/hui-script-entity-row.js"; +import "../entity-rows/hui-text-entity-row.js"; +import "../entity-rows/hui-timer-entity-row.js"; +import "../entity-rows/hui-toggle-entity-row.js"; import "../special-rows/hui-call-service-row"; import "../special-rows/hui-divider-row"; import "../special-rows/hui-section-row"; diff --git a/src/panels/lovelace/entity-rows/hui-cover-entity-row.js b/src/panels/lovelace/entity-rows/hui-cover-entity-row.js deleted file mode 100644 index 4cdc25416673..000000000000 --- a/src/panels/lovelace/entity-rows/hui-cover-entity-row.js +++ /dev/null @@ -1,74 +0,0 @@ -import { html } from "@polymer/polymer/lib/utils/html-tag"; -import { PolymerElement } from "@polymer/polymer/polymer-element"; - -import "../components/hui-generic-entity-row"; -import "../../../components/ha-cover-controls"; -import "../../../components/ha-cover-tilt-controls"; -import CoverEntity from "../../../util/cover-model"; - -class HuiCoverEntityRow extends PolymerElement { - static get template() { - return html` - ${this.styleTemplate} - - ${this.coverControlTemplate} - - `; - } - - static get styleTemplate() { - return html` - - `; - } - - static get coverControlTemplate() { - return html` - - - `; - } - - static get properties() { - return { - hass: Object, - _config: Object, - _stateObj: { - type: Object, - computed: "_computeStateObj(hass.states, _config.entity)", - }, - _entityObj: { - type: Object, - computed: "_computeEntityObj(hass, _stateObj)", - }, - }; - } - - _computeStateObj(states, entityId) { - return states && entityId in states ? states[entityId] : null; - } - - _computeEntityObj(hass, stateObj) { - return stateObj ? new CoverEntity(hass, stateObj) : null; - } - - setConfig(config) { - if (!config || !config.entity) { - throw new Error("Entity not configured."); - } - this._config = config; - } -} -customElements.define("hui-cover-entity-row", HuiCoverEntityRow); diff --git a/src/panels/lovelace/entity-rows/hui-cover-entity-row.ts b/src/panels/lovelace/entity-rows/hui-cover-entity-row.ts new file mode 100644 index 000000000000..9bf6b8840d65 --- /dev/null +++ b/src/panels/lovelace/entity-rows/hui-cover-entity-row.ts @@ -0,0 +1,84 @@ +import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element"; +import { TemplateResult } from "lit-html"; + +import "../components/hui-generic-entity-row.js"; +import "../../../components/ha-cover-controls.js"; +import "../../../components/ha-cover-tilt-controls.js"; + +import CoverEntity from "../../../util/cover-model.js"; +import { HomeAssistant } from "../../../types.js"; +import { EntityRow, EntityConfig } from "./types.js"; + +class HuiCoverEntityRow extends LitElement implements EntityRow { + public hass?: HomeAssistant; + private _config?: EntityConfig; + private _cover?: CoverEntity; + + static get properties(): PropertyDeclarations { + return { + hass: {}, + _config: {}, + }; + } + + public setConfig(config: EntityConfig): void { + if (!config || !config.entity) { + throw new Error("Entity not configured."); + } + this._config = config; + } + + protected render(): TemplateResult { + if (!this.hass || !this._config) { + return html``; + } + + if (!this._cover) { + this._cover = new CoverEntity( + this.hass, + this.hass.states[this._config.entity] + ); + } + + return html` + ${this.renderStyle()} + + ${ + this._cover.isTiltOnly + ? html` + ` + : html` + ` + } + + `; + } + + private renderStyle(): TemplateResult { + return html` + + `; + } +} + +declare global { + interface HTMLElementTagNameMap { + "hui-cover-entity-row": HuiCoverEntityRow; + } +} + +customElements.define("hui-cover-entity-row", HuiCoverEntityRow); From e51ef000c3e2bd363ea2e2bbefbfd65d2a38ed05 Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Wed, 31 Oct 2018 12:46:27 -0500 Subject: [PATCH 2/6] Extract `supports` methods from cover model --- .../entity-rows/hui-cover-entity-row.ts | 160 +++++++++--------- src/util/cover-model.js | 42 +++++ 2 files changed, 118 insertions(+), 84 deletions(-) diff --git a/src/panels/lovelace/entity-rows/hui-cover-entity-row.ts b/src/panels/lovelace/entity-rows/hui-cover-entity-row.ts index 9bf6b8840d65..6083df8791da 100644 --- a/src/panels/lovelace/entity-rows/hui-cover-entity-row.ts +++ b/src/panels/lovelace/entity-rows/hui-cover-entity-row.ts @@ -1,84 +1,76 @@ -import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element"; -import { TemplateResult } from "lit-html"; - -import "../components/hui-generic-entity-row.js"; -import "../../../components/ha-cover-controls.js"; -import "../../../components/ha-cover-tilt-controls.js"; - -import CoverEntity from "../../../util/cover-model.js"; -import { HomeAssistant } from "../../../types.js"; -import { EntityRow, EntityConfig } from "./types.js"; - -class HuiCoverEntityRow extends LitElement implements EntityRow { - public hass?: HomeAssistant; - private _config?: EntityConfig; - private _cover?: CoverEntity; - - static get properties(): PropertyDeclarations { - return { - hass: {}, - _config: {}, - }; - } - - public setConfig(config: EntityConfig): void { - if (!config || !config.entity) { - throw new Error("Entity not configured."); - } - this._config = config; - } - - protected render(): TemplateResult { - if (!this.hass || !this._config) { - return html``; - } - - if (!this._cover) { - this._cover = new CoverEntity( - this.hass, - this.hass.states[this._config.entity] - ); - } - - return html` - ${this.renderStyle()} - - ${ - this._cover.isTiltOnly - ? html` - ` - : html` - ` - } - - `; - } - - private renderStyle(): TemplateResult { - return html` - - `; - } -} - -declare global { - interface HTMLElementTagNameMap { - "hui-cover-entity-row": HuiCoverEntityRow; - } -} - -customElements.define("hui-cover-entity-row", HuiCoverEntityRow); +import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element"; +import { TemplateResult } from "lit-html"; + +import "../components/hui-generic-entity-row.js"; +import "../../../components/ha-cover-controls.js"; +import "../../../components/ha-cover-tilt-controls.js"; + +import { isTiltOnly } from "../../../util/cover-model.js"; +import { HomeAssistant } from "../../../types.js"; +import { EntityRow, EntityConfig } from "./types.js"; + +class HuiCoverEntityRow extends LitElement implements EntityRow { + public hass?: HomeAssistant; + private _config?: EntityConfig; + + static get properties(): PropertyDeclarations { + return { + hass: {}, + _config: {}, + }; + } + + public setConfig(config: EntityConfig): void { + if (!config || !config.entity) { + throw new Error("Entity not configured."); + } + this._config = config; + } + + protected render(): TemplateResult { + if (!this.hass || !this._config) { + return html``; + } + + return html` + ${this.renderStyle()} + + ${ + isTiltOnly(this.hass.states[this._config.entity]) + ? html` + ` + : html` + ` + } + + `; + } + + private renderStyle(): TemplateResult { + return html` + + `; + } +} + +declare global { + interface HTMLElementTagNameMap { + "hui-cover-entity-row": HuiCoverEntityRow; + } +} + +customElements.define("hui-cover-entity-row", HuiCoverEntityRow); diff --git a/src/util/cover-model.js b/src/util/cover-model.js index 09666718a7b9..8f9a207b9c1c 100644 --- a/src/util/cover-model.js +++ b/src/util/cover-model.js @@ -120,3 +120,45 @@ export default class CoverEntity { this.hass.callService("cover", service, data); } } + +export function supportsOpen(stateObj) { + return (stateObj.attributes.supported_features & 1) !== 0; +} + +export function supportsClose(stateObj) { + return (stateObj.attributes.supported_features & 2) !== 0; +} + +export function supportsSetPosition(stateObj) { + return (stateObj.attributes.supported_features & 4) !== 0; +} + +export function supportsStop(stateObj) { + return (stateObj.attributes.supported_features & 8) !== 0; +} + +export function supportsOpenTilt(stateObj) { + return (stateObj.attributes.supported_features & 16) !== 0; +} + +export function supportsCloseTilt(stateObj) { + return (stateObj.attributes.supported_features & 32) !== 0; +} + +export function supportsStopTilt(stateObj) { + return (stateObj.attributes.supported_features & 64) !== 0; +} + +export function supportsSetTiltPosition(stateObj) { + return (stateObj.attributes.supported_features & 128) !== 0; +} + +export function isTiltOnly(stateObj) { + var supportsCover = + supportsOpen(stateObj) || supportsClose(stateObj) || supportsStop(stateObj); + var supportsTilt = + supportsOpenTilt(stateObj) || + supportsCloseTilt(stateObj) || + supportsStopTilt(stateObj); + return supportsTilt && !supportsCover; +} From 3566d92c7c75af06fdac3556dedbc62e2de9260a Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Wed, 31 Oct 2018 20:14:46 -0500 Subject: [PATCH 3/6] Address review comments --- .../entity-rows/hui-cover-entity-row.ts | 2 +- src/util/cover-model.js | 315 +++++++++--------- 2 files changed, 152 insertions(+), 165 deletions(-) diff --git a/src/panels/lovelace/entity-rows/hui-cover-entity-row.ts b/src/panels/lovelace/entity-rows/hui-cover-entity-row.ts index 6083df8791da..c2babaa2ac9a 100644 --- a/src/panels/lovelace/entity-rows/hui-cover-entity-row.ts +++ b/src/panels/lovelace/entity-rows/hui-cover-entity-row.ts @@ -28,7 +28,7 @@ class HuiCoverEntityRow extends LitElement implements EntityRow { } protected render(): TemplateResult { - if (!this.hass || !this._config) { + if (!this._config || !this.hass || !this.hass.states[this._config.entity]) { return html``; } diff --git a/src/util/cover-model.js b/src/util/cover-model.js index 8f9a207b9c1c..ba4e91be08e1 100644 --- a/src/util/cover-model.js +++ b/src/util/cover-model.js @@ -1,164 +1,151 @@ -/* eslint-enable no-bitwise */ -export default class CoverEntity { - constructor(hass, stateObj) { - this.hass = hass; - this.stateObj = stateObj; - this._attr = stateObj.attributes; - this._feat = this._attr.supported_features; - } - - get isFullyOpen() { - if (this._attr.current_position !== undefined) { - return this._attr.current_position === 100; - } - return this.stateObj.state === "open"; - } - - get isFullyClosed() { - if (this._attr.current_position !== undefined) { - return this._attr.current_position === 0; - } - return this.stateObj.state === "closed"; - } - - get isFullyOpenTilt() { - return this._attr.current_tilt_position === 100; - } - - get isFullyClosedTilt() { - return this._attr.current_tilt_position === 0; - } - - get isOpening() { - return this.stateObj.state === "opening"; - } - - get isClosing() { - return this.stateObj.state === "closing"; - } - - /* eslint-disable no-bitwise */ - - get supportsOpen() { - return (this._feat & 1) !== 0; - } - - get supportsClose() { - return (this._feat & 2) !== 0; - } - - get supportsSetPosition() { - return (this._feat & 4) !== 0; - } - - get supportsStop() { - return (this._feat & 8) !== 0; - } - - get supportsOpenTilt() { - return (this._feat & 16) !== 0; - } - - get supportsCloseTilt() { - return (this._feat & 32) !== 0; - } - - get supportsStopTilt() { - return (this._feat & 64) !== 0; - } - - get supportsSetTiltPosition() { - return (this._feat & 128) !== 0; - } - - get isTiltOnly() { - var supportsCover = - this.supportsOpen || this.supportsClose || this.supportsStop; - var supportsTilt = - this.supportsOpenTilt || this.supportsCloseTilt || this.supportsStopTilt; - return supportsTilt && !supportsCover; - } - - openCover() { - this.callService("open_cover"); - } - - closeCover() { - this.callService("close_cover"); - } - - stopCover() { - this.callService("stop_cover"); - } - - openCoverTilt() { - this.callService("open_cover_tilt"); - } - - closeCoverTilt() { - this.callService("close_cover_tilt"); - } - - stopCoverTilt() { - this.callService("stop_cover_tilt"); - } - - setCoverPosition(position) { - this.callService("set_cover_position", { position }); - } - - setCoverTiltPosition(tiltPosition) { - this.callService("set_cover_tilt_position", { - tilt_position: tiltPosition, - }); - } - - // helper method - - callService(service, data = {}) { - data.entity_id = this.stateObj.entity_id; - this.hass.callService("cover", service, data); - } -} - -export function supportsOpen(stateObj) { - return (stateObj.attributes.supported_features & 1) !== 0; -} - -export function supportsClose(stateObj) { - return (stateObj.attributes.supported_features & 2) !== 0; -} - -export function supportsSetPosition(stateObj) { - return (stateObj.attributes.supported_features & 4) !== 0; -} - -export function supportsStop(stateObj) { - return (stateObj.attributes.supported_features & 8) !== 0; -} - -export function supportsOpenTilt(stateObj) { - return (stateObj.attributes.supported_features & 16) !== 0; -} - -export function supportsCloseTilt(stateObj) { - return (stateObj.attributes.supported_features & 32) !== 0; -} - -export function supportsStopTilt(stateObj) { - return (stateObj.attributes.supported_features & 64) !== 0; -} - -export function supportsSetTiltPosition(stateObj) { - return (stateObj.attributes.supported_features & 128) !== 0; -} - -export function isTiltOnly(stateObj) { - var supportsCover = - supportsOpen(stateObj) || supportsClose(stateObj) || supportsStop(stateObj); - var supportsTilt = - supportsOpenTilt(stateObj) || - supportsCloseTilt(stateObj) || - supportsStopTilt(stateObj); - return supportsTilt && !supportsCover; -} +/* eslint-enable no-bitwise */ +export default class CoverEntity { + constructor(hass, stateObj) { + this.hass = hass; + this.stateObj = stateObj; + this._attr = stateObj.attributes; + this._feat = this._attr.supported_features; + } + + get isFullyOpen() { + if (this._attr.current_position !== undefined) { + return this._attr.current_position === 100; + } + return this.stateObj.state === "open"; + } + + get isFullyClosed() { + if (this._attr.current_position !== undefined) { + return this._attr.current_position === 0; + } + return this.stateObj.state === "closed"; + } + + get isFullyOpenTilt() { + return this._attr.current_tilt_position === 100; + } + + get isFullyClosedTilt() { + return this._attr.current_tilt_position === 0; + } + + get isOpening() { + return this.stateObj.state === "opening"; + } + + get isClosing() { + return this.stateObj.state === "closing"; + } + + /* eslint-disable no-bitwise */ + + get supportsOpen() { + return (this._feat & 1) !== 0; + } + + get supportsClose() { + return (this._feat & 2) !== 0; + } + + get supportsSetPosition() { + return (this._feat & 4) !== 0; + } + + get supportsStop() { + return (this._feat & 8) !== 0; + } + + get supportsOpenTilt() { + return (this._feat & 16) !== 0; + } + + get supportsCloseTilt() { + return (this._feat & 32) !== 0; + } + + get supportsStopTilt() { + return (this._feat & 64) !== 0; + } + + get supportsSetTiltPosition() { + return (this._feat & 128) !== 0; + } + + get isTiltOnly() { + var supportsCover = + this.supportsOpen || this.supportsClose || this.supportsStop; + var supportsTilt = + this.supportsOpenTilt || this.supportsCloseTilt || this.supportsStopTilt; + return supportsTilt && !supportsCover; + } + + openCover() { + this.callService("open_cover"); + } + + closeCover() { + this.callService("close_cover"); + } + + stopCover() { + this.callService("stop_cover"); + } + + openCoverTilt() { + this.callService("open_cover_tilt"); + } + + closeCoverTilt() { + this.callService("close_cover_tilt"); + } + + stopCoverTilt() { + this.callService("stop_cover_tilt"); + } + + setCoverPosition(position) { + this.callService("set_cover_position", { position }); + } + + setCoverTiltPosition(tiltPosition) { + this.callService("set_cover_tilt_position", { + tilt_position: tiltPosition, + }); + } + + // helper method + + callService(service, data = {}) { + data.entity_id = this.stateObj.entity_id; + this.hass.callService("cover", service, data); + } +} + +const support = (stateObj, feature) => + (stateObj.attributes.supported_features & feature) !== 0; + +export const supportsOpen = (stateObj) => support(stateObj, 1); + +export const supportsClose = (stateObj) => support(stateObj, 2); + +export const supportsSetPosition = (stateObj) => support(stateObj, 4); + +export const supportsStop = (stateObj) => support(stateObj, 8); + +export const supportsOpenTilt = (stateObj) => support(stateObj, 16); + +export const supportsCloseTilt = (stateObj) => support(stateObj, 32); + +export const supportsStopTilt = (stateObj) => support(stateObj, 64); + +export const supportsSetTiltPosition = (stateObj) => support(stateObj, 128); + +export function isTiltOnly(stateObj) { + var supportsCover = + supportsOpen(stateObj) || supportsClose(stateObj) || supportsStop(stateObj); + var supportsTilt = + supportsOpenTilt(stateObj) || + supportsCloseTilt(stateObj) || + supportsStopTilt(stateObj); + return supportsTilt && !supportsCover; +} From 4dc3c052d5dd607718aaaadc8c1b65ffc10feda5 Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Thu, 1 Nov 2018 15:40:25 -0500 Subject: [PATCH 4/6] Revert line endings mixup I suck at vs code apparently... --- src/util/cover-model.js | 302 ++++++++++++++++++++-------------------- 1 file changed, 151 insertions(+), 151 deletions(-) diff --git a/src/util/cover-model.js b/src/util/cover-model.js index ba4e91be08e1..ad8ba30b79eb 100644 --- a/src/util/cover-model.js +++ b/src/util/cover-model.js @@ -1,151 +1,151 @@ -/* eslint-enable no-bitwise */ -export default class CoverEntity { - constructor(hass, stateObj) { - this.hass = hass; - this.stateObj = stateObj; - this._attr = stateObj.attributes; - this._feat = this._attr.supported_features; - } - - get isFullyOpen() { - if (this._attr.current_position !== undefined) { - return this._attr.current_position === 100; - } - return this.stateObj.state === "open"; - } - - get isFullyClosed() { - if (this._attr.current_position !== undefined) { - return this._attr.current_position === 0; - } - return this.stateObj.state === "closed"; - } - - get isFullyOpenTilt() { - return this._attr.current_tilt_position === 100; - } - - get isFullyClosedTilt() { - return this._attr.current_tilt_position === 0; - } - - get isOpening() { - return this.stateObj.state === "opening"; - } - - get isClosing() { - return this.stateObj.state === "closing"; - } - - /* eslint-disable no-bitwise */ - - get supportsOpen() { - return (this._feat & 1) !== 0; - } - - get supportsClose() { - return (this._feat & 2) !== 0; - } - - get supportsSetPosition() { - return (this._feat & 4) !== 0; - } - - get supportsStop() { - return (this._feat & 8) !== 0; - } - - get supportsOpenTilt() { - return (this._feat & 16) !== 0; - } - - get supportsCloseTilt() { - return (this._feat & 32) !== 0; - } - - get supportsStopTilt() { - return (this._feat & 64) !== 0; - } - - get supportsSetTiltPosition() { - return (this._feat & 128) !== 0; - } - - get isTiltOnly() { - var supportsCover = - this.supportsOpen || this.supportsClose || this.supportsStop; - var supportsTilt = - this.supportsOpenTilt || this.supportsCloseTilt || this.supportsStopTilt; - return supportsTilt && !supportsCover; - } - - openCover() { - this.callService("open_cover"); - } - - closeCover() { - this.callService("close_cover"); - } - - stopCover() { - this.callService("stop_cover"); - } - - openCoverTilt() { - this.callService("open_cover_tilt"); - } - - closeCoverTilt() { - this.callService("close_cover_tilt"); - } - - stopCoverTilt() { - this.callService("stop_cover_tilt"); - } - - setCoverPosition(position) { - this.callService("set_cover_position", { position }); - } - - setCoverTiltPosition(tiltPosition) { - this.callService("set_cover_tilt_position", { - tilt_position: tiltPosition, - }); - } - - // helper method - - callService(service, data = {}) { - data.entity_id = this.stateObj.entity_id; - this.hass.callService("cover", service, data); - } -} - -const support = (stateObj, feature) => - (stateObj.attributes.supported_features & feature) !== 0; - -export const supportsOpen = (stateObj) => support(stateObj, 1); - -export const supportsClose = (stateObj) => support(stateObj, 2); - -export const supportsSetPosition = (stateObj) => support(stateObj, 4); - -export const supportsStop = (stateObj) => support(stateObj, 8); - -export const supportsOpenTilt = (stateObj) => support(stateObj, 16); - -export const supportsCloseTilt = (stateObj) => support(stateObj, 32); - -export const supportsStopTilt = (stateObj) => support(stateObj, 64); - -export const supportsSetTiltPosition = (stateObj) => support(stateObj, 128); - -export function isTiltOnly(stateObj) { - var supportsCover = - supportsOpen(stateObj) || supportsClose(stateObj) || supportsStop(stateObj); - var supportsTilt = - supportsOpenTilt(stateObj) || - supportsCloseTilt(stateObj) || - supportsStopTilt(stateObj); - return supportsTilt && !supportsCover; -} +/* eslint-enable no-bitwise */ +export default class CoverEntity { + constructor(hass, stateObj) { + this.hass = hass; + this.stateObj = stateObj; + this._attr = stateObj.attributes; + this._feat = this._attr.supported_features; + } + + get isFullyOpen() { + if (this._attr.current_position !== undefined) { + return this._attr.current_position === 100; + } + return this.stateObj.state === "open"; + } + + get isFullyClosed() { + if (this._attr.current_position !== undefined) { + return this._attr.current_position === 0; + } + return this.stateObj.state === "closed"; + } + + get isFullyOpenTilt() { + return this._attr.current_tilt_position === 100; + } + + get isFullyClosedTilt() { + return this._attr.current_tilt_position === 0; + } + + get isOpening() { + return this.stateObj.state === "opening"; + } + + get isClosing() { + return this.stateObj.state === "closing"; + } + + /* eslint-disable no-bitwise */ + + get supportsOpen() { + return (this._feat & 1) !== 0; + } + + get supportsClose() { + return (this._feat & 2) !== 0; + } + + get supportsSetPosition() { + return (this._feat & 4) !== 0; + } + + get supportsStop() { + return (this._feat & 8) !== 0; + } + + get supportsOpenTilt() { + return (this._feat & 16) !== 0; + } + + get supportsCloseTilt() { + return (this._feat & 32) !== 0; + } + + get supportsStopTilt() { + return (this._feat & 64) !== 0; + } + + get supportsSetTiltPosition() { + return (this._feat & 128) !== 0; + } + + get isTiltOnly() { + var supportsCover = + this.supportsOpen || this.supportsClose || this.supportsStop; + var supportsTilt = + this.supportsOpenTilt || this.supportsCloseTilt || this.supportsStopTilt; + return supportsTilt && !supportsCover; + } + + openCover() { + this.callService("open_cover"); + } + + closeCover() { + this.callService("close_cover"); + } + + stopCover() { + this.callService("stop_cover"); + } + + openCoverTilt() { + this.callService("open_cover_tilt"); + } + + closeCoverTilt() { + this.callService("close_cover_tilt"); + } + + stopCoverTilt() { + this.callService("stop_cover_tilt"); + } + + setCoverPosition(position) { + this.callService("set_cover_position", { position }); + } + + setCoverTiltPosition(tiltPosition) { + this.callService("set_cover_tilt_position", { + tilt_position: tiltPosition, + }); + } + + // helper method + + callService(service, data = {}) { + data.entity_id = this.stateObj.entity_id; + this.hass.callService("cover", service, data); + } +} + +const support = (stateObj, feature) => + (stateObj.attributes.supported_features & feature) !== 0; + +export const supportsOpen = (stateObj) => support(stateObj, 1); + +export const supportsClose = (stateObj) => support(stateObj, 2); + +export const supportsSetPosition = (stateObj) => support(stateObj, 4); + +export const supportsStop = (stateObj) => support(stateObj, 8); + +export const supportsOpenTilt = (stateObj) => support(stateObj, 16); + +export const supportsCloseTilt = (stateObj) => support(stateObj, 32); + +export const supportsStopTilt = (stateObj) => support(stateObj, 64); + +export const supportsSetTiltPosition = (stateObj) => support(stateObj, 128); + +export function isTiltOnly(stateObj) { + var supportsCover = + supportsOpen(stateObj) || supportsClose(stateObj) || supportsStop(stateObj); + var supportsTilt = + supportsOpenTilt(stateObj) || + supportsCloseTilt(stateObj) || + supportsStopTilt(stateObj); + return supportsTilt && !supportsCover; +} From bf4851877f5ed358554b73c3680115c146f70e38 Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Fri, 2 Nov 2018 22:08:22 -0500 Subject: [PATCH 5/6] Address review comments --- .../lovelace/common/create-row-element.js | 22 +-- .../entity-rows/hui-cover-entity-row.ts | 158 +++++++++--------- src/util/cover-model.js | 8 +- 3 files changed, 97 insertions(+), 91 deletions(-) diff --git a/src/panels/lovelace/common/create-row-element.js b/src/panels/lovelace/common/create-row-element.js index 4d075782f7b9..00d9d36d17c3 100644 --- a/src/panels/lovelace/common/create-row-element.js +++ b/src/panels/lovelace/common/create-row-element.js @@ -2,17 +2,17 @@ import { fireEvent } from "../../../common/dom/fire_event"; import "../entity-rows/hui-climate-entity-row"; import "../entity-rows/hui-cover-entity-row"; -import "../entity-rows/hui-group-entity-row.js"; -import "../entity-rows/hui-input-number-entity-row.js"; -import "../entity-rows/hui-input-select-entity-row.js"; -import "../entity-rows/hui-input-text-entity-row.js"; -import "../entity-rows/hui-lock-entity-row.js"; -import "../entity-rows/hui-media-player-entity-row.js"; -import "../entity-rows/hui-scene-entity-row.js"; -import "../entity-rows/hui-script-entity-row.js"; -import "../entity-rows/hui-text-entity-row.js"; -import "../entity-rows/hui-timer-entity-row.js"; -import "../entity-rows/hui-toggle-entity-row.js"; +import "../entity-rows/hui-group-entity-row"; +import "../entity-rows/hui-input-number-entity-row"; +import "../entity-rows/hui-input-select-entity-row"; +import "../entity-rows/hui-input-text-entity-row"; +import "../entity-rows/hui-lock-entity-row"; +import "../entity-rows/hui-media-player-entity-row"; +import "../entity-rows/hui-scene-entity-row"; +import "../entity-rows/hui-script-entity-row"; +import "../entity-rows/hui-text-entity-row"; +import "../entity-rows/hui-timer-entity-row"; +import "../entity-rows/hui-toggle-entity-row"; import "../special-rows/hui-call-service-row"; import "../special-rows/hui-divider-row"; import "../special-rows/hui-section-row"; diff --git a/src/panels/lovelace/entity-rows/hui-cover-entity-row.ts b/src/panels/lovelace/entity-rows/hui-cover-entity-row.ts index c2babaa2ac9a..6cade58f8569 100644 --- a/src/panels/lovelace/entity-rows/hui-cover-entity-row.ts +++ b/src/panels/lovelace/entity-rows/hui-cover-entity-row.ts @@ -1,76 +1,82 @@ -import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element"; -import { TemplateResult } from "lit-html"; - -import "../components/hui-generic-entity-row.js"; -import "../../../components/ha-cover-controls.js"; -import "../../../components/ha-cover-tilt-controls.js"; - -import { isTiltOnly } from "../../../util/cover-model.js"; -import { HomeAssistant } from "../../../types.js"; -import { EntityRow, EntityConfig } from "./types.js"; - -class HuiCoverEntityRow extends LitElement implements EntityRow { - public hass?: HomeAssistant; - private _config?: EntityConfig; - - static get properties(): PropertyDeclarations { - return { - hass: {}, - _config: {}, - }; - } - - public setConfig(config: EntityConfig): void { - if (!config || !config.entity) { - throw new Error("Entity not configured."); - } - this._config = config; - } - - protected render(): TemplateResult { - if (!this._config || !this.hass || !this.hass.states[this._config.entity]) { - return html``; - } - - return html` - ${this.renderStyle()} - - ${ - isTiltOnly(this.hass.states[this._config.entity]) - ? html` - ` - : html` - ` - } - - `; - } - - private renderStyle(): TemplateResult { - return html` - - `; - } -} - -declare global { - interface HTMLElementTagNameMap { - "hui-cover-entity-row": HuiCoverEntityRow; - } -} - -customElements.define("hui-cover-entity-row", HuiCoverEntityRow); +import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element"; +import { TemplateResult } from "lit-html"; + +import "../components/hui-generic-entity-row.js"; +import "../../../components/ha-cover-controls.js"; +import "../../../components/ha-cover-tilt-controls.js"; + +import { isTiltOnly } from "../../../util/cover-model.js"; +import { HomeAssistant } from "../../../types.js"; +import { EntityRow, EntityConfig } from "./types.js"; + +class HuiCoverEntityRow extends LitElement implements EntityRow { + public hass?: HomeAssistant; + private _config?: EntityConfig; + + static get properties(): PropertyDeclarations { + return { + hass: {}, + _config: {}, + }; + } + + public setConfig(config: EntityConfig): void { + if (!config) { + throw new Error("Configuration error"); + } + this._config = config; + } + + protected render(): TemplateResult { + if (!this._config || !this.hass) { + return html``; + } + + return html` + ${this.renderStyle()} + + ${ + this.hass.states[this._config.entity] + ? html` + ${ + isTiltOnly(this.hass.states[this._config.entity]) + ? html` + ` + : html` + ` + } + ` + : html`` + } + + `; + } + + private renderStyle(): TemplateResult { + return html` + + `; + } +} + +declare global { + interface HTMLElementTagNameMap { + "hui-cover-entity-row": HuiCoverEntityRow; + } +} + +customElements.define("hui-cover-entity-row", HuiCoverEntityRow); diff --git a/src/util/cover-model.js b/src/util/cover-model.js index ad8ba30b79eb..45de4ad9113d 100644 --- a/src/util/cover-model.js +++ b/src/util/cover-model.js @@ -72,9 +72,9 @@ export default class CoverEntity { } get isTiltOnly() { - var supportsCover = + const supportsCover = this.supportsOpen || this.supportsClose || this.supportsStop; - var supportsTilt = + const supportsTilt = this.supportsOpenTilt || this.supportsCloseTilt || this.supportsStopTilt; return supportsTilt && !supportsCover; } @@ -141,9 +141,9 @@ export const supportsStopTilt = (stateObj) => support(stateObj, 64); export const supportsSetTiltPosition = (stateObj) => support(stateObj, 128); export function isTiltOnly(stateObj) { - var supportsCover = + const supportsCover = supportsOpen(stateObj) || supportsClose(stateObj) || supportsStop(stateObj); - var supportsTilt = + const supportsTilt = supportsOpenTilt(stateObj) || supportsCloseTilt(stateObj) || supportsStopTilt(stateObj); From 4722c23e8275a6f2416b7c6cfc7d1ad64330ab5f Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Mon, 5 Nov 2018 21:04:12 -0600 Subject: [PATCH 6/6] Address review comments: error-row not working --- .../entity-rows/hui-cover-entity-row.ts | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/panels/lovelace/entity-rows/hui-cover-entity-row.ts b/src/panels/lovelace/entity-rows/hui-cover-entity-row.ts index 6cade58f8569..0def319cc125 100644 --- a/src/panels/lovelace/entity-rows/hui-cover-entity-row.ts +++ b/src/panels/lovelace/entity-rows/hui-cover-entity-row.ts @@ -32,6 +32,15 @@ class HuiCoverEntityRow extends LitElement implements EntityRow { return html``; } + const stateObj = this.hass.states[this._config.entity]; + + if (!stateObj) { + return html` + `; + } + return html` ${this.renderStyle()} ${ - this.hass.states[this._config.entity] + isTiltOnly(stateObj) ? html` - ${ - isTiltOnly(this.hass.states[this._config.entity]) - ? html` - ` - : html` - ` - } - ` - : html`` + ` + : html` + ` } `;