1
0
mirror of https://github.com/home-assistant/frontend.git synced 2025-12-24 20:55:49 +00:00
Files
frontend/src/panels/lovelace/components/hui-entities-toggle.ts
Paulus Schoutsen 1b9286db76 Fix lit warnings (#9204)
Co-authored-by: Bram Kragten <mail@bramkragten.nl>
2021-05-18 15:01:43 -07:00

80 lines
2.1 KiB
TypeScript

import {
css,
CSSResultGroup,
html,
LitElement,
PropertyValues,
TemplateResult,
} from "lit";
import { customElement, property, state } from "lit/decorators";
import { DOMAINS_TOGGLE } from "../../../common/const";
import "../../../components/ha-switch";
import type { HaSwitch } from "../../../components/ha-switch";
import { forwardHaptic } from "../../../data/haptics";
import type { HomeAssistant } from "../../../types";
import { turnOnOffEntities } from "../common/entity/turn-on-off-entities";
@customElement("hui-entities-toggle")
class HuiEntitiesToggle extends LitElement {
@property({ type: Array }) public entities?: string[];
@property({ attribute: false }) protected hass?: HomeAssistant;
@state() private _toggleEntities?: string[];
public willUpdate(changedProperties: PropertyValues): void {
super.willUpdate(changedProperties);
if (changedProperties.has("entities")) {
this._toggleEntities = this.entities!.filter(
(entityId) =>
entityId in this.hass!.states &&
DOMAINS_TOGGLE.has(entityId.split(".", 1)[0])
);
}
}
protected render(): TemplateResult {
if (!this._toggleEntities?.length) {
return html``;
}
return html`
<ha-switch
aria-label=${this.hass!.localize(
"ui.panel.lovelace.card.entities.toggle"
)}
.checked=${this._toggleEntities!.some((entityId) => {
const stateObj = this.hass!.states[entityId];
return stateObj && stateObj.state === "on";
})}
@change=${this._callService}
></ha-switch>
`;
}
static get styles(): CSSResultGroup {
return css`
:host {
width: 38px;
display: block;
}
ha-switch {
padding: 13px 5px;
margin: -4px -8px;
}
`;
}
private _callService(ev: MouseEvent): void {
forwardHaptic("light");
const turnOn = (ev.target as HaSwitch).checked;
turnOnOffEntities(this.hass!, this._toggleEntities!, turnOn!);
}
}
declare global {
interface HTMLElementTagNameMap {
"hui-entities-toggle": HuiEntitiesToggle;
}
}