-
Notifications
You must be signed in to change notification settings - Fork 7
/
battery-entity-row.js
191 lines (163 loc) · 7.49 KB
/
battery-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) => {
console.info(
'%c BATTERY-ENTITY-ROW %c 1.3.1 ',
'color: cyan; background: black; font-weight: bold;',
'color: darkblue; background: white; font-weight: bold;',
);
const {html, css} = LitElement.prototype;
const defaultOnStates = ['on', 'charging', 'true'];
class BatteryEntityRow extends LitElement {
static get properties() {
return {
_hass: Object,
_config: Object,
stateObj: Object
}
}
static get styles() {
return css`
:host {
display: flex;
align-items: center;
}
.flex {
flex: 1;
margin-left: 16px;
display: flex;
justify-content: space-between;
align-items: center;
min-width: 0;
}
.info, state-badge {
cursor: pointer;
}
.secondary {
color: var(--secondary-text-color);
}
.good {
color: var(--label-badge-green);
}
.warning {
color: var(--label-badge-yellow);
}
.critical {
color: var(--label-badge-red);
}`;
}
render() {
if (!this._hass || !this._config) return html``;
if (!this.stateObj) return this.renderWarning();
const charging = this.getChargingState(this._config.charging)
const batteryValue = this.getBatteryLevel(this._config.attribute);
const isUnavailable = !batteryValue || ['unavailable', 'unknown'].includes(batteryValue);
const isNumeric = !isNaN(parseFloat(batteryValue)) && isFinite(batteryValue);
const numericValue = isUnavailable ? null : isNumeric ? batteryValue : this.parseStringValue(batteryValue);
const icon = this._config.icon || this.getIcon(numericValue, charging);
const color = this.getColor(numericValue);
const name = this._config.name || this.stateObj.attributes.friendly_name;
const unit = this._config.unit === false ? null : (this._config.unit || (isNumeric ? '%' : null));
const state = isUnavailable
? this._hass.localize('state.default.unknown')
: html`${batteryValue}${unit && html` ${unit}`}`;
return html`
<state-badge
.stateObj="${this.stateObj}"
.overrideIcon="${icon}"
@click="${this.moreInfo}"
class="pointer ${color}">
</state-badge>
<div class="flex" @click="${this.moreInfo}">
<div class="info">${name}${this.renderSecondaryInfo()}</div>
<div class="state">${state}</div>
</div>`;
}
renderSecondaryInfo() {
const secondaryInfo = this._config.secondary_info;
let content = undefined;
if (secondaryInfo === 'last-changed') {
content = html`<ha-relative-time .datetime="${this.stateObj.last_changed}" .hass="${this._hass}"></ha-relative-time>`;
} else if (secondaryInfo === 'last-updated') {
content = html`<ha-relative-time .datetime="${this.stateObj.last_updated}" .hass="${this._hass}"></ha-relative-time>`;
} else if (secondaryInfo in this.stateObj.attributes) {
content = this.stateObj.attributes[secondaryInfo];
}
return content ? html`<div class="secondary">${content}</div>` : null;
}
renderWarning() {
return html`<hui-warning>
${this._hass.localize('ui.panel.lovelace.warning.entity_not_found', 'entity', this._config.entity)}
</hui-warning>`;
}
setConfig(config) {
if (!config.entity) throw new Error('Please define a valid entity.');
this.moreInfo = () => this.fireEvent(this, 'hass-more-info', {entityId: config.entity});
this._config = config;
}
shouldUpdate(changedProps) {
return changedProps.has('stateObj');
}
set hass(hass) {
this._hass = hass;
if (hass && this._config) {
this.stateObj = this._config.entity in hass.states ? hass.states[this._config.entity] : null;
}
}
getBatteryLevel(attribute) {
let batteryValue = this.stateObj.state;
if (this.stateObj.attributes.battery) batteryValue = this.stateObj.attributes.battery;
if (this.stateObj.attributes.battery_level) batteryValue = this.stateObj.attributes.battery_level;
if (this.stateObj.attributes[attribute]) batteryValue = this.stateObj.attributes[attribute];
return !isNaN(parseFloat(batteryValue)) && isFinite(batteryValue)
? Math.round(parseInt(batteryValue, 10)) : batteryValue;
}
getChargingState(chargingConfig) {
if (!chargingConfig) return false;
if (chargingConfig === true) {
return defaultOnStates.includes(this.stateObj.state.toString().toLowerCase());
}
const additionalStates = chargingConfig.state || [];
const onStates = defaultOnStates.concat(additionalStates).map(value => value.toString().toLowerCase());
const entity = (chargingConfig.entity && chargingConfig.entity in this._hass.states)
? this._hass.states[chargingConfig.entity] : this.stateObj;
const state = chargingConfig.attribute ? entity.attributes[chargingConfig.attribute] : entity.state;
return onStates.includes(state.toString().toLowerCase());
}
getIcon(batteryLevel, charging) {
if (!batteryLevel) return 'mdi:battery-unknown';
const roundedLevel = Math.round(batteryLevel / 10) * 10;
return roundedLevel >= 100
? (charging ? 'mdi:battery-charging-100' : 'mdi:battery')
: roundedLevel === 0
? (charging ? 'mdi:battery-charging-outline' : 'mdi:battery-outline')
: (charging ? 'mdi:battery-charging-' : 'mdi:battery-') + roundedLevel;
}
getColor(batteryLevel) {
if (!batteryLevel) return 'unknown';
const warning = this._config.warning || 35;
const critical = this._config.critical || 15;
return (batteryLevel > warning)
? 'good'
: (batteryLevel > critical)
? 'warning'
: 'critical';
}
parseStringValue(v) {
const val = v.toString().toLowerCase();
if (['full', 'high', 'max', 'maximum'].includes(val)) return 90;
if (['medium', 'med', 'normal'].includes(val)) return 50;
if (['low', 'min', 'minimal'].includes(val)) return 10;
if (['empty', 'critical', 'none'].includes(val)) return 0;
return null;
}
fireEvent(node, type, detail = {}, options = {}) {
const event = new Event(type, {
bubbles: options.bubbles || true,
cancelable: options.cancelable || true,
composed: options.composed || true,
});
event.detail = detail;
node.dispatchEvent(event);
}
}
customElements.define('battery-entity-row', BatteryEntityRow);
})(window.LitElement || Object.getPrototypeOf(customElements.get('hui-masonry-view') || customElements.get('hui-view')));