-
Notifications
You must be signed in to change notification settings - Fork 4
/
waze-card.js
342 lines (342 loc) · 13.1 KB
/
waze-card.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
"use strict";
customElements.whenDefined('card-tools').then(() => {
var cardTools = customElements.get('card-tools');
// YOUR CODE GOES IN HERE
class WazeCard extends cardTools.LitElement {
styles() {
return cardTools.LitHtml `
<style>
.ha-card-waze { /* Zebra striping */ }
.ha-card-waze h3, h3 { padding-left: 10px ; padding-right: 10px ; margin-bottom: 0 ; color: white ; }
.ha-card-waze table { width: 100%; }
.ha-card-waze tr:nth-of-type(odd) { /*background: #eee; */ }
.ha-card-waze th { /*background: #3498db;*/ color: white; font-weight: bold; }
.ha-card-waze td { padding-left: 10px ; padding-right: 10px ; color: white ; text-align: left; }
.ha-card-waze th { padding-left: 10px ; padding-right: 10px ; color: white ; text-align: left; }
</style>
`;
}
render() {
return cardTools.LitHtml `
<ha-card class="ha-card-waze">
${this.styles()}
${this.config.title.length > 0 ? cardTools.LitHtml `<h2>${this.config.title}</h2>` : cardTools.LitHtml ``}
<table class="ha-card-waze">
${this.config.header ? cardTools.LitHtml `
<thead>${this.config.columns.map(column => cardTools.LitHtml `<th>${(column || '').toLowerCase()}</th>`)}</thead>
` : cardTools.LitHtml ``}
<tbody>
${this.currentStates ? this.currentStates.map(state => cardTools.LitHtml `
<tr onclick="window.open('https://www.waze.com/ul?navigate=yes&ll=${state.destination.lat}%2C${state.destination.long}&from=${state.origin.lat}%2C${state.origin.long}&at=now');">
${this.config.columns.map(column => cardTools.LitHtml `
<td>${state[column]}</td>
`)}
</tr>
`) : ''}
</tbody>
</table>
</ha-card>
`;
}
getAllStates(entities) {
const wazeStates = entities
.map(entity => {
const state = this._hass.states[entity.entity || ''];
const origin = this._hass.states[entity.origin] || { attributes: { latitude: this._hass.config.latitude, longitude: this._hass.config.longitude } };
const destination = this._hass.states[entity.destination || ''];
if (state && destination) {
state.to_unit_system = entity.to_unit_system || this._hass.config.unit_system.length;
state.name = entity.name || destination.attributes.friendly_name;
state.origin = { lat: origin.attributes.latitude, long: origin.attributes.longitude };
state.destination = { lat: destination.attributes.latitude, long: destination.attributes.longitude };
return state;
}
})
.filter(Boolean);
const nextStates = wazeStates.map(state => {
return {
origin: state.origin,
destination: state.destination,
name: state.name || state.entity || '',
distance: this.computeDistance(state),
duration: this.computeDuration(state),
route: state.attributes && state.attributes.route || ''
};
});
return (nextStates);
}
/**
* generates the duration for a route
* @param {Object} state the card state
* @return {string} the formatted duration for a ruote
*/
computeDuration(state) {
let duration = state.attributes && state.attributes.duration || 0;
let unit_of_measurement = state.attributes && state.attributes.unit_of_measurement || '';
return `${parseInt(duration)} ${unit_of_measurement}`;
}
/**
* computes the distance for a route for metric/imperial system
* @param {Object} state the card state
* @return {string} the formatted distance
*/
computeDistance(state) {
let distance = state.attributes && state.attributes.distance || 0;
if (this._hass.config.unit_system.length !== state.to_unit_system) {
if ('km' == state.to_unit_system) {
distance = distance / 1.60934;
}
else {
distance = distance * 1.60934;
}
}
distance = Number(Math.round(distance * 100) / 100).toFixed(1);
distance = `${distance} ${this._hass.config.unit_system.length}`;
return distance;
}
/**
* System
* @returns {{hass: ObjectConstructor; config: ObjectConstructor}}
*/
static get properties() {
return {
hass: Object,
config: Object,
};
}
setConfig(config) {
this.name = config.name;
// setup config
this.config = Object.assign({ title: 'Waze Routes', group: false, header: true, columns: ['name', 'distance', 'duration', 'route'] }, config);
}
/**
* Assign the external hass object to an internal class var.
* This is called everytime a state change occurs in HA
*
* @param hass
*/
set hass(hass) {
this._hass = hass;
const wazeStates = this.getAllStates(this.config.entities);
// if data is the same as last time then do nothing
if (JSON.stringify(wazeStates) === JSON.stringify(this.currentStates || [])) {
return;
}
this.currentStates = wazeStates;
}
/**
* System
* The height of your card. Home Assistant uses this to automatically
* distribute all cards over the available columns.
* @returns {any}
*/
getCardSize() {
return this.config.entities.length + 1;
}
_render() {
return cardTools.LitHtml `
${this.name}
`;
}
}
customElements.define("waze-card", WazeCard);
}); // END OF .then(() => {
setTimeout(() => {
if (customElements.get('card-tools'))
return;
customElements.define('waze-card', class extends HTMLElement {
setConfig() { throw new Error("Can't find card-tools. See https://github.com/thomasloven/lovelace-card-tools"); }
});
}, 2000);
// //import {Polymer} from "@polymer/polymer/polymer-legacy";
//
// import { LitElement, html, } from 'https://unpkg-gcp.firebaseapp.com/@polymer/lit-element@0.5.2/lit-element.js?module';
//
// //let LitElement = window.LitElement || Object.getPrototypeOf(customElements.get("hui-error-entity-row"));
// // let html = LitElement.prototype.html;
//
// function loadCSS(url) {
// const link = document.createElement('link');
// link.type = 'text/css';
// link.rel = 'stylesheet';
// link.href = url;
// document.head.appendChild(link);
// }
//
// //loadCSS( "/local/card-waze/waze-card.css" ) ;
//
// // Create your custom component
// class WazeCard extends LitElement {
// private currentStates ;
//
// private styles() {
// return html`
// <style>
// .ha-card-waze { /* Zebra striping */ }
// .ha-card-waze h3, h3 { padding-left: 10px ; padding-right: 10px ; margin-bottom: 0 ; color: white ; }
// .ha-card-waze table { width: 100%; }
// .ha-card-waze tr:nth-of-type(odd) { /*background: #eee; */ }
// .ha-card-waze th { /*background: #3498db;*/ color: white; font-weight: bold; }
// .ha-card-waze td { padding-left: 10px ; padding-right: 10px ; color: white ; text-align: left; }
// .ha-card-waze th { padding-left: 10px ; padding-right: 10px ; color: white ; text-align: left; }
// </style>
// `;
// }
//
// _render() {
// return html`
// <ha-card class="ha-card-waze">
// ${this.styles()}
// ${this.config.title.length > 0 ? html`<h2>${this.config.title}</h2>` : html`` }
// <table class="ha-card-waze">
// ${this.config.header ? html`
// <thead>${this.config.columns.map(column => html`<th>${(column || '').toLowerCase()}</th>`)}</thead>
// ` : html`` }
// <tbody>
// ${this.currentStates ? this.currentStates.map( state => html`
// <tr onclick="window.open('https://www.waze.com/ul?navigate=yes&ll=${state.destination.lat}%2C${state.destination.long}&from=${state.origin.lat}%2C${state.origin.long}&at=now');">
// ${this.config.columns.map(column => html`
// <td>${state[ column ]}</td>
// `)}
// </tr>
// `) : ''}
// </tbody>
// </table>
// </ha-card>
// `;
// }
//
// getAllStates(entities) {
// const wazeStates = entities
// .map( entity => {
// const state = this._hass.states[entity.entity || ''];
// const origin = this._hass.states[entity.origin ] || { attributes : {latitude: this._hass.config.latitude, longitude: this._hass.config.longitude }};
// const destination = this._hass.states[entity.destination || ''];
//
// if(state && destination) {
// state.to_unit_system = entity.to_unit_system || this._hass.config.unit_system.length ;
// state.name = entity.name || destination.attributes.friendly_name;
// state.origin = {lat: origin.attributes.latitude, long: origin.attributes.longitude};
// state.destination = {lat: destination.attributes.latitude, long: destination.attributes.longitude};
// return state;
// }
// })
// .filter(Boolean);
//
// const nextStates = wazeStates.map(state => {
// return {
// origin: state.origin,
// destination: state.destination,
// name: state.name || state.entity || '',
// distance: this.computeDistance(state),
// duration: this.computeDuration(state),
// route: state.attributes && state.attributes.route || ''
// };
// });
//
// return( nextStates ) ;
// }
//
// /**
// * generates the duration for a route
// * @param {Object} state the card state
// * @return {string} the formatted duration for a ruote
// */
// private computeDuration( state ) {
// let duration = state.attributes && state.attributes.duration || 0;
// let unit_of_measurement = state.attributes && state.attributes.unit_of_measurement || '';
// return `${parseInt(duration)} ${unit_of_measurement}`;
// }
//
// /**
// * computes the distance for a route for metric/imperial system
// * @param {Object} state the card state
// * @return {string} the formatted distance
// */
// private computeDistance(state) {
// let distance = state.attributes && state.attributes.distance || 0;
// if(this._hass.config.unit_system.length !== state.to_unit_system ) {
// if( 'km' == state.to_unit_system ) {
// distance = distance / 1.60934 ;
// } else {
// distance = distance * 1.60934 ;
// }
// }
//
// distance = Number(Math.round(distance * 100) / 100).toFixed(1);
// distance = `${distance} ${this._hass.config.unit_system.length}`;
// return distance;
// }
//
// /**
// * System
// * @returns {{hass: ObjectConstructor; config: ObjectConstructor}}
// */
// static get properties() {
// return {
// hass: Object,
// config: Object,
// }
// }
//
// /**
// * System
// * @param config
// */
// setConfig(config) {
// if (!config.entities) {
// throw new Error('You need to define entities');
// }
//
// // setup config
// this.config = {
// title: 'Waze Routes',
// group: false,
// header: true,
// columns: ['name', 'distance', 'duration', 'route'],
// ...config
// };
//
// // add click event to open waze routes
// // this.getElementsByClassName( 'ha-card-waze' ).addEventListener('click', event => {
// // const source = event.target || event.srcElement;
// // if(!source || !source.dataset || !source.dataset.location) return;
// //
// // const location = JSON.parse(source.dataset.location);
// // window.open(`https://www.waze.com/ul?navigate=yes&ll=${location.lat}%2C${location.long}`);
// // });
// //this.config = config;
// }
//
// /**
// * Assign the external hass object to an internal class var.
// * This is called everytime a state change occurs in HA
// *
// * @param hass
// */
// set hass(hass) {
// this._hass = hass;
//
// const wazeStates = this.getAllStates( this.config.entities ) ;
//
// // if data is the same as last time then do nothing
// if(JSON.stringify(wazeStates) === JSON.stringify(this.currentStates || [])){
// return;
// }
//
// this.currentStates = wazeStates ;
// }
//
// /**
// * System
// * The height of your card. Home Assistant uses this to automatically
// * distribute all cards over the available columns.
// * @returns {any}
// */
// getCardSize() {
// return this.config.entities.length + 1;
// }
// }
//
// customElements.define('waze-card', WazeCard);
//# sourceMappingURL=waze-card.js.map