-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclimate-mode-entity-row.js
191 lines (170 loc) · 5.09 KB
/
climate-mode-entity-row.js
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
((LitElement) => {
const html = LitElement.prototype.html;
const css = LitElement.prototype.css;
const defaultColors = {
auto: "#43a047",
cool: "#2b9af9",
dry: "#FFC107",
fan_only: "#B0BEC5",
heat: "#ff8100",
heat_cool: "#43a047",
off: "#ef5350",
eco: "#66bb6a",
away: "#90CAF9",
boost: "#ef5350",
comfort: "#FFC107",
home: "#ff8100",
sleep: "#9575CD",
activity: "#43a047"
};
const defaultIcons = {
auto: "mdi:calendar-sync",
cool: "mdi:snowflake",
dry: "mdi:water-percent",
fan_only: "mdi:fan",
heat: "mdi:fire",
heat_cool: "mdi:autorenew",
off: "mdi:power",
away: "mdi:snowflake",
eco: "mdi:leaf",
boost: "mdi:rocket-launch",
comfort: "mdi:weather-sunny",
home: "mdi:home",
sleep: "mdi:moon-waning-crescent",
activity: "mdi:motion-sensor"
};
class ClimateModeEntity extends LitElement {
static get properties() {
return {
_hass: {},
_config: {},
state: {},
};
}
static get styles() {
return css`
.flex {
display: flex;
align-items: center;
flex-wrap: nowrap;
justify-content: space-between;
align-items: center;
}
ha-icon {
cursor: pointer;
color: var(--secondary-text-color);
}
ha-icon:not(:last-child) {
margin-right: 8px;
}
ha-icon.active {
color: var(--primary-color);
}
`;
}
render() {
const config = this._config;
const hass = this._hass;
return html`
<hui-generic-entity-row .hass="${hass}" .config="${config}" .catchInteraction=${false}>
<div class="flex">
${config.modes.map((mode) => this.renderMode(mode))}
</div>
</hui-generic-entity-row>
`;
}
renderMode(mode) {
const isActive =
(mode.preset_mode == null ||
mode.preset_mode === this.state.preset_mode) &&
(mode.hvac_mode == null || mode.hvac_mode === this.state.hvac_mode) &&
(mode.fan_mode == null || mode.fan_mode === this.state.fan_mode) &&
(mode.swing_mode == null ||
mode.swing_mode === this.state.swing_mode) &&
(mode.temperature == null ||
mode.temperature === this.state.temperature);
const onClick = () => {
this.setMode(mode);
}
const defaultColor = defaultColors[mode.preset_mode || mode.hvac_mode];
const defaultIcon = defaultIcons[mode.preset_mode || mode.hvac_mode];
const color = mode.color || defaultColor;
const style = color ? `color: ${color}` : "";
return html`
<ha-icon
style="${isActive ? style : ""}"
class="${isActive ? "active" : ""}"
icon="${mode.icon || defaultIcon || "mdi-thermostat"}"
@click="${onClick}"
></ha-icon>
`;
}
setMode(mode) {
if (mode.hvac_mode != null && mode.hvac_mode != this.state.hvac_mode) {
this._hass.callService("climate", "set_hvac_mode", {
entity_id: this._config.entity,
hvac_mode: mode.hvac_mode,
});
}
if (
mode.preset_mode != null &&
mode.preset_mode != this.state.preset_mode
) {
this._hass.callService("climate", "set_preset_mode", {
entity_id: this._config.entity,
preset_mode: mode.preset_mode,
});
}
if (mode.fan_mode != null && mode.fan_mode != this.state.fan_mode) {
this._hass.callService("climate", "set_fan_mode", {
entity_id: this._config.entity,
fan_mode: mode.fan_mode,
});
}
if (mode.swing_mode != null && mode.swing_mode != this.state.swing_mode) {
this._hass.callService("climate", "set_swing_mode", {
entity_id: this._config.entity,
swing_mode: mode.swing_mode,
});
}
if (
mode.temperature != null &&
mode.temperature != this.state.temperature
) {
this._hass.callService("climate", "set_temperature", {
entity_id: this._config.entity,
temperature: mode.temperature,
});
}
}
setConfig(config) {
if (!config.entity) throw new Error("Please define a entity.");
if (config.entity.split(".")[0] !== "climate")
throw new Error("Please define a climate entity.");
this._config = config;
}
set hass(hass) {
this._hass = hass;
const entity = hass.states[this._config.entity];
const hvac_mode = entity.state;
const preset_mode = entity.attributes.preset_mode;
const fan_mode = entity.attributes.fan_mode;
const swing_mode = entity.attributes.swing_mode;
const temperature = entity.attributes.temperature;
this.state = {
entity,
hvac_mode,
preset_mode,
fan_mode,
swing_mode,
temperature,
};
}
}
customElements.define("climate-mode-entity-row", ClimateModeEntity);
})(
window.LitElement ||
Object.getPrototypeOf(
customElements.get("hui-masonry-view") || customElements.get("hui-view")
)
);