-
Notifications
You must be signed in to change notification settings - Fork 30
/
custom-ui.js
260 lines (243 loc) · 8.6 KB
/
custom-ui.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Define constants for the custom-ui component
const Name = "Custom-ui";
const Version = "20240118";
const Description = "add attributes icon_color and templates";
const Url = "https://github.com/Mariusthvdb/custom-ui";
// Log information about the custom-ui component
console.groupCollapsed(
`%c ${Name} ${Version} is installed \n%c ${Description} `,
"color: gold; font-weight: bold; background: black",
"color: white; font-weight: bold; background: steelblue"),
console.log("Readme:",Url),
console.groupEnd()
// Define the custom-ui object and its methods
window.customUI = {
// Helper function to find an element either in shadowRoot or regular DOM
lightOrShadow: (elem, selector) =>
elem.shadowRoot
? elem.shadowRoot.querySelector(selector)
: elem.querySelector(selector),
// Apply template attributes to an entity's attributes
async maybeApplyTemplateAttributes(hass, states, entity) {
const newAttributes = {};
const templateKeys = Object.keys(entity.attributes.templates);
for (const key of templateKeys) {
if (key === "state") {
console.warn(
`State templating is not supported anymore, please check your customization for ${entity.entity_id}`
);
continue;
}
const template = entity.attributes.templates[key];
const value = await window.customUI.computeTemplate(
template,
hass,
states,
entity,
entity.attributes,
entity.untemplated_attributes?.[key] || entity.attributes[key],
entity.untemplated_state || entity.state
);
if (value === null) continue;
newAttributes[key] = value;
}
const newEntity = {
...entity,
attributes: {
...entity.attributes,
...newAttributes
},
untemplated_attributes: entity.untemplated_attributes ?? entity.attributes
};
return newEntity;
},
// Install a hook to update the states with template attributes
installTemplateAttributesHook() {
customElements.whenDefined("home-assistant").then(() => {
const homeAssistant = customElements.get("home-assistant");
if (!homeAssistant?.prototype?._updateHass) return;
const originalUpdate = homeAssistant.prototype._updateHass;
homeAssistant.prototype._updateHass = function update(obj) {
const { hass } = this;
if (obj.states) {
Object.keys(obj.states).forEach(async (key) => {
const entity = obj.states[key];
if (!entity.attributes.templates) {
return;
}
const newEntity =
await window.customUI.maybeApplyTemplateAttributes(
hass,
obj.states,
entity
);
if (hass.states && JSON.stringify(entity) !== JSON.stringify(hass.states[key])) {
obj.states[key] = newEntity;
} else if (JSON.stringify(entity) !== JSON.stringify(newEntity)) {
obj.states[key] = newEntity;
}
});
}
originalUpdate.call(this, obj);
};
});
},
// Install a hook to update the button card with custom styling
installButtonCardStylingHook() {
customElements.whenDefined("hui-button-card").then(() => {
const buttonCard = customElements.get("hui-button-card");
if (!buttonCard) return;
if (buttonCard.prototype?.updated) {
const originalUpdated = buttonCard.prototype.updated;
buttonCard.prototype.updated = function customUpdated(changedProps) {
if (!changedProps.has('_stateObj')) {
return;
}
const { _stateObj } = this;
if (_stateObj.attributes?.icon_color) {
this.style?.setProperty('--icon-primary-color', _stateObj.attributes.icon_color);
}
originalUpdated.call(this, changedProps);
}
}
});
},
// Install a hook to update the entity card with custom styling
installEntityCardStylingHook() {
customElements.whenDefined("hui-entity-card").then(() => {
const entityCard = customElements.get("hui-entity-card");
if (!entityCard) return;
if (entityCard.prototype?.updated) {
const originalUpdated = entityCard.prototype.updated;
entityCard.prototype.updated = function customUpdated(changedProps) {
if (
!changedProps.has('_config') ||
!changedProps.has('hass')
) {
return;
}
const { _config, hass } = this;
const entityId = _config?.entity;
const states = hass?.states;
const iconColor = states?.[entityId]?.attributes?.icon_color;
if (iconColor) {
this.style?.setProperty('--paper-item-icon-color', iconColor);
}
originalUpdated.call(this, changedProps);
}
}
});
},
// Install a hook to update the Tile card with custom styling
installTileCardStylingHook() {
customElements.whenDefined("hui-tile-card").then(() => {
const tileCard = customElements.get("hui-tile-card");
if (!tileCard) return;
if (tileCard.prototype?.updated) {
const originalUpdated = tileCard.prototype.updated;
tileCard.prototype.updated = function customUpdated(changedProps) {
if (
!changedProps.has('_config') ||
!changedProps.has('hass')
) {
return;
}
const { _config, hass } = this;
const entityId = _config?.entity;
const states = hass?.states;
const iconColor = states?.[entityId]?.attributes?.icon_color;
if (iconColor) {
this.style?.setProperty('--icon-primary-color', iconColor);
}
originalUpdated.call(this, changedProps);
}
}
});
},
// Install a hook to update the state badge with custom styling
installStateBadgeStylingHook() {
customElements.whenDefined("state-badge").then(() => {
const stateBadge = customElements.get("state-badge");
if (!stateBadge) return;
if (stateBadge.prototype?.updated) {
const originalUpdated = stateBadge.prototype.updated;
stateBadge.prototype.updated = function customUpdated(changedProps) {
if (!changedProps.has("stateObj")) return;
const { stateObj } = this;
if (
stateObj.attributes.icon_color &&
!stateObj.attributes.entity_picture
) {
this.style.backgroundImage = "";
this._showIcon = true;
this._iconStyle = {
color: stateObj.attributes.icon_color
};
}
originalUpdated.call(this, changedProps);
};
}
});
},
// Install the hooks for updating states, entity cards, and state badges
installCustomHooks() {
window.customUI.installTemplateAttributesHook();
window.customUI.installButtonCardStylingHook();
window.customUI.installEntityCardStylingHook();
window.customUI.installTileCardStylingHook();
window.customUI.installStateBadgeStylingHook();
},
async init() {
// Check if initialization has already been done
if (window.customUI.initDone) return;
// Wait for the hass.states to be populated
const main = window.customUI.lightOrShadow(document, "home-assistant");
await new Promise((resolve) => {
const intervalId = setInterval(() => {
if (main?.hass?.states) {
clearInterval(intervalId);
resolve();
}
}, 100);
});
// Install the hooks and mark initialization as done
window.customUI.installCustomHooks();
window.customUI.initDone = true;
// Push custom-ui information to a global list
window.CUSTOM_UI_LIST = window.CUSTOM_UI_LIST || [];
window.CUSTOM_UI_LIST.push({
name: Name,
version: `${Version} ${Description}`,
url: Url
});
},
// Evaluate a template expression
computeTemplate(
template,
hass,
entities,
entity,
attributes,
attribute,
state
) {
const functionBody =
template.indexOf("return") >= 0 ? template : `return \`${template}\`;`;
try {
return new Function(
"hass",
"entities",
"entity",
"attributes",
"attribute",
"state",
functionBody
)(hass, entities, entity, attributes, attribute, state);
} catch (e) {
console.warn(`${e.name}: ${e.message} in custom-ui template ${functionBody}`);
return null;
}
}
};
// Initialize the custom-ui component
window.customUI.init();