import { HassEntity } from "home-assistant-js-websocket"; import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit"; import { customElement, property } from "lit/decorators"; import { classMap } from "lit/directives/class-map"; import { styleMap } from "lit/directives/style-map"; import { stateColorCss } from "../../common/entity/state_color"; import "../../components/ha-control-button"; import "../../components/ha-control-switch"; import "../../components/ha-state-icon"; import { UNAVAILABLE, UNKNOWN } from "../../data/entity"; import { forwardHaptic } from "../../data/haptics"; import { HomeAssistant } from "../../types"; @customElement("ha-state-control-cover-toggle") export class HaStateControlCoverToggle extends LitElement { @property({ attribute: false }) public hass!: HomeAssistant; @property({ attribute: false }) public stateObj!: HassEntity; private _valueChanged(ev) { const checked = ev.target.checked as boolean; if (checked) { this._turnOn(); } else { this._turnOff(); } } private _turnOn() { this._callService(true); } private _turnOff() { this._callService(false); } private async _callService(turnOn): Promise { if (!this.hass || !this.stateObj) { return; } forwardHaptic("light"); await this.hass.callService( "cover", turnOn ? "open_cover" : "close_cover", { entity_id: this.stateObj.entity_id, } ); } protected render(): TemplateResult { const onColor = stateColorCss(this.stateObj, "open"); const offColor = stateColorCss(this.stateObj, "closed"); const isOn = this.stateObj.state === "open" || this.stateObj.state === "closing" || this.stateObj.state === "opening"; const isOff = this.stateObj.state === "closed"; if ( this.stateObj.attributes.assumed_state || this.stateObj.state === UNKNOWN ) { return html`
`; } return html` `; } static get styles(): CSSResultGroup { return css` ha-control-switch { height: 45vh; max-height: 320px; min-height: 200px; --control-switch-thickness: 130px; --control-switch-border-radius: 36px; --control-switch-padding: 6px; --mdc-icon-size: 24px; } .buttons { display: flex; flex-direction: column; width: 130px; height: 45vh; max-height: 320px; min-height: 200px; padding: 6px; box-sizing: border-box; } ha-control-button { flex: 1; width: 100%; --control-button-border-radius: 36px; --mdc-icon-size: 24px; } ha-control-button.active { --control-button-icon-color: white; --control-button-background-color: var(--color); --control-button-background-opacity: 1; } ha-control-button:not(:last-child) { margin-bottom: 6px; } `; } } declare global { interface HTMLElementTagNameMap { "ha-state-control-cover-toggle": HaStateControlCoverToggle; } }