-
-
Notifications
You must be signed in to change notification settings - Fork 340
/
fan-card.ts
230 lines (206 loc) · 8.21 KB
/
fan-card.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import {
ActionHandlerEvent,
computeRTL,
handleAction,
hasAction,
HomeAssistant,
LovelaceCard,
LovelaceCardEditor,
} from "custom-card-helpers";
import { css, CSSResultGroup, html, LitElement, PropertyValues, TemplateResult } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
import { styleMap } from "lit/directives/style-map.js";
import { computeStateDisplay } from "../../ha/common/entity/compute-state-display";
import { isActive, isAvailable } from "../../ha/data/entity";
import "../../shared/badge-icon";
import "../../shared/button";
import "../../shared/card";
import "../../shared/shape-icon";
import "../../shared/state-info";
import "../../shared/state-item";
import { cardStyle } from "../../utils/card-styles";
import { registerCustomCard } from "../../utils/custom-cards";
import { actionHandler } from "../../utils/directives/action-handler-directive";
import { stateIcon } from "../../utils/icons/state-icon";
import { getLayoutFromConfig } from "../../utils/layout";
import { FAN_CARD_EDITOR_NAME, FAN_CARD_NAME, FAN_ENTITY_DOMAINS } from "./const";
import "./controls/fan-oscillate-control";
import "./controls/fan-percentage-control";
import { FanCardConfig } from "./fan-card-config";
import { getPercentage } from "./utils";
registerCustomCard({
type: FAN_CARD_NAME,
name: "Mushroom Fan Card",
description: "Card for fan entity",
});
@customElement(FAN_CARD_NAME)
export class FanCard extends LitElement implements LovelaceCard {
public static async getConfigElement(): Promise<LovelaceCardEditor> {
await import("./fan-card-editor");
return document.createElement(FAN_CARD_EDITOR_NAME) as LovelaceCardEditor;
}
public static async getStubConfig(hass: HomeAssistant): Promise<FanCardConfig> {
const entities = Object.keys(hass.states);
const fans = entities.filter((e) => FAN_ENTITY_DOMAINS.includes(e.split(".")[0]));
return {
type: `custom:${FAN_CARD_NAME}`,
entity: fans[0],
};
}
@property({ attribute: false }) public hass!: HomeAssistant;
@state() private _config?: FanCardConfig;
getCardSize(): number | Promise<number> {
return 1;
}
setConfig(config: FanCardConfig): void {
this._config = {
tap_action: {
action: "toggle",
},
hold_action: {
action: "more-info",
},
double_tap_action: {
action: "more-info",
},
...config,
};
this.updatePercentage();
}
protected updated(changedProperties: PropertyValues) {
super.updated(changedProperties);
if (this.hass && changedProperties.has("hass")) {
this.updatePercentage();
}
}
@state()
private percentage?: number;
updatePercentage() {
this.percentage = undefined;
if (!this._config || !this.hass || !this._config.entity) return;
const entity_id = this._config.entity;
const entity = this.hass.states[entity_id];
if (!entity) return;
this.percentage = getPercentage(entity);
}
private onCurrentPercentageChange(e: CustomEvent<{ value?: number }>): void {
if (e.detail.value != null) {
this.percentage = e.detail.value;
}
}
private _handleAction(ev: ActionHandlerEvent) {
handleAction(this, this.hass!, this._config!, ev.detail.action!);
}
protected render(): TemplateResult {
if (!this._config || !this.hass || !this._config.entity) {
return html``;
}
const entity_id = this._config.entity;
const entity = this.hass.states[entity_id];
const name = this._config.name || entity.attributes.friendly_name;
const icon = this._config.icon || stateIcon(entity);
const layout = getLayoutFromConfig(this._config);
const hideState = this._config.hide_state;
const stateDisplay = computeStateDisplay(this.hass.localize, entity, this.hass.locale);
const active = isActive(entity);
let iconStyle = {};
const percentage = getPercentage(entity);
if (active) {
if (percentage) {
const speed = 1.5 * (percentage / 100) ** 0.5;
iconStyle["--animation-duration"] = `${1 / speed}s`;
} else {
iconStyle["--animation-duration"] = `1s`;
}
}
let stateValue = `${stateDisplay}`;
if (this.percentage) {
stateValue += ` - ${this.percentage}%`;
}
const rtl = computeRTL(this.hass);
return html`
<mushroom-card .layout=${layout} ?rtl=${rtl}>
<mushroom-state-item
?rtl=${rtl}
.layout=${layout}
@action=${this._handleAction}
.actionHandler=${actionHandler({
hasHold: hasAction(this._config.hold_action),
hasDoubleClick: hasAction(this._config.double_tap_action),
})}
>
<mushroom-shape-icon
slot="icon"
class=${classMap({
spin: active && !!this._config.icon_animation,
})}
style=${styleMap(iconStyle)}
.disabled=${!active}
.icon=${icon}
></mushroom-shape-icon>
${!isAvailable(entity)
? html`
<mushroom-badge-icon
class="unavailable"
slot="badge"
icon="mdi:help"
></mushroom-badge-icon>
`
: null}
<mushroom-state-info
slot="info"
.primary=${name}
.secondary=${!hideState && stateValue}
></mushroom-state-info>
</mushroom-state-item>
${this._config.show_percentage_control || this._config.show_oscillate_control
? html`
<div class="actions" ?rtl=${rtl}>
${this._config.show_percentage_control
? html`
<mushroom-fan-percentage-control
.hass=${this.hass}
.entity=${entity}
@current-change=${this.onCurrentPercentageChange}
></mushroom-fan-percentage-control>
`
: null}
${this._config.show_oscillate_control
? html`
<mushroom-fan-oscillate-control
.hass=${this.hass}
.entity=${entity}
></mushroom-fan-oscillate-control>
`
: null}
</div>
`
: null}
</mushroom-card>
`;
}
static get styles(): CSSResultGroup {
return [
cardStyle,
css`
mushroom-state-item {
cursor: pointer;
}
mushroom-shape-icon {
--icon-color: rgb(var(--rgb-state-fan));
--shape-color: rgba(var(--rgb-state-fan), 0.2);
}
mushroom-shape-icon.spin {
--icon-animation: var(--animation-duration) infinite linear spin;
}
mushroom-shape-icon ha-icon {
color: red !important;
}
mushroom-fan-percentage-control {
flex: 1;
}
`,
];
}
}