-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathutil.js
58 lines (48 loc) · 2.08 KB
/
util.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
import { LAST_CHANGED, LAST_UPDATED, SECONDARY_INFO_VALUES, UNAVAILABLE_STATES } from './lib/constants';
export const isObject = (obj) => typeof obj === 'object' && !Array.isArray(obj) && !!obj;
export const isUnavailable = (stateObj) => !stateObj || UNAVAILABLE_STATES.includes(stateObj.state);
export const hideUnavailable = (stateObj, config) =>
config.hide_unavailable &&
(isUnavailable(stateObj) ||
(config.attribute &&
![LAST_CHANGED, LAST_UPDATED].includes(config.attribute) &&
stateObj.attributes[config.attribute] === undefined));
export const hideIf = (stateObj, config) => {
if (hideUnavailable(stateObj, config)) {
return true;
}
if (config.hide_if === undefined) {
return false;
}
const value = config.attribute ? stateObj.attributes[config.attribute] : stateObj.state;
let hideValues = [];
if (isObject(config.hide_if)) {
if (config.hide_if.below && value < config.hide_if.below) {
return true;
}
if (config.hide_if.above && value > config.hide_if.above) {
return true;
}
if (config.hide_if.value) {
hideValues = hideValues.concat(config.hide_if.value);
}
} else {
hideValues = hideValues.concat(config.hide_if);
}
return hideValues.some((hideValue) => (typeof hideValue === 'number' ? hideValue === +value : hideValue === value));
};
export const hasGenericSecondaryInfo = (config) => typeof config === 'string' && SECONDARY_INFO_VALUES.includes(config);
export const getEntityIds = (config) =>
[config.entity, config.secondary_info?.entity]
.concat(config.entities?.map((entity) => (typeof entity === 'string' ? entity : entity.entity)))
.filter((entity) => entity);
export const hasConfigOrEntitiesChanged = (node, changedProps) => {
if (changedProps.has('config')) {
return true;
}
const oldHass = changedProps.get('_hass');
if (oldHass) {
return node.entityIds.some((entity) => oldHass.states[entity] !== node._hass.states[entity]);
}
return false;
};