Advertisement
Guest User

linky-card.js

a guest
Oct 18th, 2018
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class LinkyCard extends HTMLElement {
  2.   set hass(hass) {
  3.     if (!this.content) {
  4.       const card = document.createElement('ha-card');
  5.       const link = document.createElement('link');
  6.       link.type = 'text/css';
  7.       link.rel = 'stylesheet';
  8.       link.href = '/local/custom_ui/linky-card.css?v=31';
  9.       card.appendChild(link);
  10.       this.content = document.createElement('div');
  11.       this.content.className = 'card';
  12.       card.appendChild(this.content);
  13.       this.appendChild(card);
  14.     }
  15.    
  16.     const lastWeek = {
  17.       2: hass.states[this.config.cons_2d],
  18.       3: hass.states[this.config.cons_3d],
  19.       4: hass.states[this.config.cons_4d],
  20.       5: hass.states[this.config.cons_5d],
  21.       6: hass.states[this.config.cons_6d]
  22.     }
  23.  
  24.     this.content.innerHTML = `
  25.       <div class="icon-block">
  26.         <span class="linky-icon bigger" style="background: none, url(/local/images/linky.png) no-repeat; background-size: contain;"></span>
  27.       </div>
  28.       <div class="hp-hc-block">
  29.         <span class="conso-hc">${Number.parseFloat(hass.states[this.config.hc_cons].state).toFixed(1)}</span><span class="conso-unit-hc"> ${hass.states[this.config.hc_cons].attributes.unit_of_measurement} <span class="more-unit">(en HC)</span></span><br />
  30.         <span class="conso-hp">${Number.parseFloat(hass.states[this.config.hp_cons].state).toFixed(1)}</span><span class="conso-unit-hp"> ${hass.states[this.config.hp_cons].attributes.unit_of_measurement} <span class="more-unit">(en HP)</span></span>
  31.       </div>
  32.       <div class="cout-block">
  33.         <span class="cout" title="Coût journalier">${Number.parseFloat(hass.states[this.config.daily_cost].state).toFixed(2)}</span><span class="cout-unit"> ${hass.states[this.config.daily_cost].attributes.unit_of_measurement}</span>
  34.       </div>
  35.       <div class="clear"></div>
  36.       <span>
  37.         <ul class="variations-linky right">
  38.             <li><span class="ha-icon"><ha-icon icon="mdi:flash"></ha-icon></span>${Math.round(hass.states[this.config.hc_hp_percent].state)}<span class="unit"> ${hass.states[this.config.hc_hp_percent].attributes.unit_of_measurement} HC</span></li>
  39.         </ul>
  40.         <ul class="variations-linky">
  41.             <li><span class="ha-icon"><ha-icon icon="mdi:arrow-right" style="transform: rotate(${(hass.states[this.config.monthly_cons_evol].state < 0) ? '45' : ((hass.states[this.config.monthly_cons_evol].state == 0) ? "0" : "-45")}deg)"></ha-icon></span>${Math.round(hass.states[this.config.monthly_cons_evol].state)}<span class="unit"> ${hass.states['sensor.evolution_conso_mensuelle'].attributes.unit_of_measurement}</span><span class="previous-month">par rapport au mois précédent</span></li>
  42.         </ul>
  43.       </span>
  44.       <div class="week-history clear">
  45.           ${Object.keys(lastWeek).map((day, index) => `
  46.           <div class="day">
  47.               <span class="dayname">${new Date((new Date().getTime()) - day*60*60*24*1000).toLocaleDateString('fr-FR', {weekday: "long"}).split(' ')[0]}</span>
  48.               <br><span class="cons-val">${Number.parseFloat(lastWeek[day].state).toFixed(1)} ${lastWeek[day].attributes.unit_of_measurement}</span>
  49.           </div>`).join('')}
  50.       </div>`;
  51.   }
  52.  
  53.   setConfig(config) {
  54.     if (!config.cons_6d || !config.cons_2d || !config.cons_3d || !config.cons_4d || !config.cons_5d || !config.hp_cons || !config.hc_cons || !config.hc_hp_percent || !config.daily_cost || !config.monthly_cons_evol) {
  55.       throw new Error('Please define entities');
  56.     }
  57.     this.config = config;
  58.   }
  59.  
  60.   // @TODO: This requires more intelligent logic
  61.   getCardSize() {
  62.     return 3;
  63.   }
  64. }
  65.  
  66. customElements.define('linky-card', LinkyCard);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement