-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathindex.js
177 lines (154 loc) · 6.08 KB
/
index.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
import { handleClick, secondsToDuration } from 'custom-card-helpers';
import { checkEntity, entityName, entityStateDisplay, entityStyles, entityUnit, entityValue } from './entity';
import {
getEntityIds,
hasConfigOrEntitiesChanged,
hasGenericSecondaryInfo,
hasToggle,
isObject,
hideUnavailable,
} from './util';
import { style } from './styles';
const LitElement =
window.LitElement ||
Object.getPrototypeOf(customElements.get('hui-masonry-view') || customElements.get('hui-view'));
const { html, css } = LitElement.prototype;
console.info(
'%c MULTIPLE-ENTITY-ROW %c 4.1.0 ',
'color: cyan; background: black; font-weight: bold;',
'color: darkblue; background: white; font-weight: bold;'
);
class MultipleEntityRow extends LitElement {
static get properties() {
return {
_hass: Object,
config: Object,
stateObj: Object,
};
}
setConfig(config) {
if (!config || !config.entity) {
throw new Error('Please define a main entity.');
}
if (config.entities) {
config.entities.forEach((entity) => checkEntity(entity));
}
if (config.secondary_info) {
checkEntity(config.secondary_info);
}
this.entityIds = getEntityIds(config);
this.onRowClick = this.clickHandler(config.entity, config.tap_action);
this.config = config;
}
shouldUpdate(changedProps) {
return hasConfigOrEntitiesChanged(this, changedProps);
}
set hass(hass) {
this._hass = hass;
if (hass && this.config) {
this.stateObj = hass.states[this.config.entity];
if (isObject(this.config.secondary_info)) {
this.info = hass.states[this.config.secondary_info.entity] ?? this.stateObj;
}
this.entities =
this.config.entities?.map((config) => {
const conf = typeof config === 'string' ? { entity: config } : config;
return { ...conf, stateObj: conf.entity ? hass.states[conf.entity] : this.stateObj };
}) ?? [];
}
}
static get styles() {
return style(css);
}
render() {
if (!this._hass || !this.config) return html``;
if (!this.stateObj) return this.renderWarning();
return html`<hui-generic-entity-row
.hass="${this._hass}"
.config="${this.config}"
.secondaryText="${this.renderSecondaryInfo()}"
>
<div class="${this.config.column ? 'entities-column' : 'entities-row'}">
${this.entities.map((entity) => this.renderEntity(entity.stateObj, entity))}${this.renderMainEntity()}
</div>
</hui-generic-entity-row>`;
}
renderSecondaryInfo() {
if (
!this.config.secondary_info ||
hasGenericSecondaryInfo(this.config.secondary_info) ||
hideUnavailable(this.info, this.config.secondary_info)
) {
return null;
}
if (typeof this.config.secondary_info === 'string') {
return html`${this.config.secondary_info}`;
}
const name = entityName(this.info, this.config.secondary_info);
return html`${name} ${this.renderValue(this.info, this.config.secondary_info)}`;
}
renderMainEntity() {
if (this.config.show_state === false) {
return null;
}
return html`<div class="state entity" style="${entityStyles(this.config)}" @click="${this.onRowClick}">
${this.config.state_header && html`<span>${this.config.state_header}</span>`}
<div>${this.renderValue(this.stateObj, this.config)}</div>
</div>`;
}
renderEntity(stateObj, config) {
if (!stateObj || hideUnavailable(stateObj, config)) {
return null;
}
const onClick = this.clickHandler(stateObj.entity_id, config.tap_action);
return html`<div class="entity" style="${entityStyles(config)}" @click="${onClick}">
<span>${entityName(stateObj, config)}</span>
<div>${config.icon ? this.renderIcon(stateObj, config) : this.renderValue(stateObj, config)}</div>
</div>`;
}
renderValue(stateObj, config) {
if (hasToggle(stateObj, config)) {
return html`<ha-entity-toggle .stateObj="${stateObj}" .hass="${this._hass}"></ha-entity-toggle>`;
}
if (config.format) {
const value = entityValue(stateObj, config);
const unit = entityUnit(stateObj, config);
if (isNaN(parseFloat(value)) || !isFinite(value)) {
return value;
}
if (config.format === 'brightness') {
return `${Math.round((value / 255) * 100)} %`;
}
if (config.format === 'duration') {
return secondsToDuration(value);
}
if (config.format.startsWith('precision')) {
const precision = parseInt(config.format.slice(-1), 10);
return `${parseFloat(value).toFixed(precision)}${unit ? ` ${unit}` : ''}`;
}
return html`<hui-timestamp-display
.ts=${new Date(value)}
.format=${config.format}
.hass=${this._hass}
></hui-timestamp-display>`;
}
return entityStateDisplay(this._hass, stateObj, config);
}
renderIcon(stateObj, config) {
return html`<state-badge
class="icon-small"
.stateObj="${stateObj}"
.overrideIcon="${config.icon === true ? stateObj.attributes.icon || null : config.icon}"
.stateColor="${config.state_color}"
></state-badge>`;
}
renderWarning() {
return html`<hui-warning>
${this._hass.localize('ui.panel.lovelace.warning.entity_not_found', 'entity', this.config.entity)}
</hui-warning>`;
}
clickHandler(entity, actionConfig) {
return () => handleClick(this, this._hass, { entity, tap_action: actionConfig }, false, false);
}
}
customElements.define('multiple-entity-row', MultipleEntityRow);