-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathentity.js
89 lines (79 loc) · 3.76 KB
/
entity.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
import { secondsToDuration } from './lib/seconds_to_duration';
import { formatNumber } from './lib/format_number';
import { computeStateDisplay } from './lib/compute_state_display';
import { isObject, isUnavailable } from './util';
export const checkEntity = (config) => {
if (isObject(config) && !(config.entity || config.attribute || config.icon)) {
throw new Error(`Entity object requires at least one 'entity', 'attribute' or 'icon'.`);
} else if (typeof config === 'string' && config === '') {
throw new Error('Entity ID string must not be blank.');
} else if (typeof config !== 'string' && !isObject(config)) {
throw new Error('Entity config must be a valid entity ID string or entity object.');
}
};
export const computeEntity = (entityId) => entityId.substr(entityId.indexOf('.') + 1);
export const entityName = (stateObj, config) => {
if (config.name === false) return null;
return (
config.name ||
(config.entity ? stateObj.attributes.friendly_name || computeEntity(stateObj.entity_id) : null) ||
null
);
};
export const entityStateDisplay = (hass, stateObj, config) => {
if (isUnavailable(stateObj)) {
return hass.localize(`state.default.${stateObj.state}`);
}
let value = config.attribute ? stateObj.attributes[config.attribute] : stateObj.state;
let unit =
config.unit === false
? undefined
: config.attribute !== undefined
? config.unit
: config.unit || stateObj.attributes.unit_of_measurement;
if (config.format) {
if (isNaN(parseFloat(value)) || !isFinite(value)) {
// do nothing if not a number
} else if (config.format === 'brightness') {
value = Math.round((value / 255) * 100);
unit = '%';
} else if (config.format === 'duration') {
value = secondsToDuration(value);
unit = undefined;
} else if (config.format === 'duration-m') {
value = secondsToDuration(value / 1000);
unit = undefined;
} else if (config.format === 'duration-h') {
value = secondsToDuration(value * 3600);
unit = undefined;
} else if (config.format.startsWith('precision')) {
const precision = parseInt(config.format.slice(-1), 10);
value = formatNumber(parseFloat(value), hass.locale, {
minimumFractionDigits: precision,
maximumFractionDigits: precision,
});
} else if (config.format === 'kilo') {
value = formatNumber(value / 1000, hass.locale, { maximumFractionDigits: 2 });
} else if (config.format === 'invert') {
value = formatNumber(value - value * 2, hass.locale);
} else if (config.format === 'position') {
value = formatNumber(100 - value, hass.locale);
} else if (config.format === 'celsius_to_fahrenheit') {
value = formatNumber(value * 1.8 + 32, hass.locale, { maximumFractionDigits: 0 });
} else if (config.format === 'fahrenheit_to_celsius') {
value = formatNumber(((value - 32) * 5) / 9, hass.locale, { maximumFractionDigits: 1 });
}
return `${value}${unit ? ` ${unit}` : ''}`;
}
if (config.attribute) {
return `${isNaN(value) ? value : formatNumber(value, hass.locale)}${unit ? ` ${unit}` : ''}`;
}
const modifiedStateObj = { ...stateObj, attributes: { ...stateObj.attributes, unit_of_measurement: unit } };
return computeStateDisplay(hass.localize, modifiedStateObj, hass.locale, hass.entities);
};
export const entityStyles = (config) =>
isObject(config?.styles)
? Object.keys(config.styles)
.map((key) => `${key}: ${config.styles[key]};`)
.join('')
: '';