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

Convert cover-row to TypeScript/LitElement #1933

Merged
merged 6 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 0 additions & 1 deletion src/panels/lovelace/common/create-row-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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";
Expand Down
74 changes: 0 additions & 74 deletions src/panels/lovelace/entity-rows/hui-cover-entity-row.js

This file was deleted.

82 changes: 82 additions & 0 deletions src/panels/lovelace/entity-rows/hui-cover-entity-row.ts
Original file line number Diff line number Diff line change
@@ -0,0 +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) {
iantrich marked this conversation as resolved.
Show resolved Hide resolved
throw new Error("Configuration error");
}
this._config = config;
}

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

return html`
${this.renderStyle()}
<hui-generic-entity-row
.hass=${this.hass}
.config=${this._config}
>
${
this.hass.states[this._config.entity]
Copy link
Member

Choose a reason for hiding this comment

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

Migrate this to use the new no-entity-row from #1946

const stateObj = this.hass.states[this._config.entity];

if (!stateObj) {
  return html`<hui-error-entity-row .entity="${this._config.entity}"></hui-error-entity-row>`;
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Why exactly are we wanting to use this new error row instead of the generic row? We still need the generic row to render the label, regardless, so just curious what we gain.

Copy link
Member

Choose a reason for hiding this comment

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

Eventually we will remove the error rendering from generic row.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, added this in and is not rendering anything. See it in the inspector but nothing showing. Too tired to look now, but will check it out further tomorrow during lunch, hopefully.

? html`
${
isTiltOnly(this.hass.states[this._config.entity])
? html`
<ha-cover-tilt-controls
.hass=${this.hass}
.stateObj=${this.hass.states[this._config.entity]}
></ha-cover-tilt-controls>`
: html`
<ha-cover-controls
.hass=${this.hass}
.stateObj=${this.hass.states[this._config.entity]}
></ha-cover-controls>`
}
`
: html``
}
</hui-generic-entity-row>
`;
}

private renderStyle(): TemplateResult {
return html`
<style>
ha-cover-controls,
ha-cover-tilt-controls {
margin-right: -.57em;
}
</style>
`;
}
}

declare global {
interface HTMLElementTagNameMap {
"hui-cover-entity-row": HuiCoverEntityRow;
}
}

customElements.define("hui-cover-entity-row", HuiCoverEntityRow);
33 changes: 31 additions & 2 deletions src/util/cover-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -120,3 +120,32 @@ export default class CoverEntity {
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) {
const supportsCover =
supportsOpen(stateObj) || supportsClose(stateObj) || supportsStop(stateObj);
const supportsTilt =
supportsOpenTilt(stateObj) ||
supportsCloseTilt(stateObj) ||
supportsStopTilt(stateObj);
return supportsTilt && !supportsCover;
}