Advertisement
PlatoNL

singlevalue-card.js

Jul 24th, 2019
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class SingleValueCard extends HTMLElement {
  2.   constructor() {
  3.     super();
  4.     this.attachShadow({ mode: 'open' });
  5.   }
  6.   setConfig(config) {
  7.     if (!config.entity) {
  8.       throw new Error('Please define an entity');
  9.     }
  10.  
  11.     const root = this.shadowRoot;
  12.     if (root.lastChild) root.removeChild(root.lastChild);
  13.     const cardConfig = Object.assign({}, config);
  14.     if (!cardConfig.scale) cardConfig.scale = "50px";
  15.     if (!cardConfig.from) cardConfig.from = "left";
  16.     const card = document.createElement('ha-card');
  17.     const content = document.createElement('div');
  18.     content.id = "value"
  19.     const style = document.createElement('style');
  20.     style.textContent = `
  21.       ha-card {
  22.         box-shadow: none;
  23.         text-align: left;
  24.         --singlevalue-fill-color: var(--label-badge-blue);
  25.         --singlevalue-percent: 100%;
  26.         --singlevalue-direction: ${cardConfig.from};
  27.         --base-unit: ${cardConfig.scale};
  28.         padding: calc(var(--base-unit)*0.8) calc(var(--base-unit)*0.8);
  29.         background: linear-gradient(to var(--singlevalue-direction), var(--paper-card-background-color) var(--singlevalue-percent), var(--singlevalue-fill-color) var(--singlevalue-percent));
  30.       }
  31.       #value {
  32.         font-size: calc(var(--base-unit) * 1.0);
  33.         line-height: calc(var(--base-unit) * 1.0);
  34.         color: var(--primary-text-color);
  35.       }
  36.     `;
  37.     card.appendChild(content);
  38.     card.appendChild(style);
  39.     card.addEventListener('click', event => {
  40.       this._fire('hass-more-info', { entityId: cardConfig.entity });
  41.     });
  42.     root.appendChild(card);
  43.     this._config = cardConfig;
  44.   }
  45.  
  46.   _fire(type, detail, options) {
  47.     const node = this.shadowRoot;
  48.     options = options || {};
  49.     detail = (detail === null || detail === undefined) ? {} : detail;
  50.     const event = new Event(type, {
  51.       bubbles: options.bubbles === undefined ? true : options.bubbles,
  52.       cancelable: Boolean(options.cancelable),
  53.       composed: options.composed === undefined ? true : options.composed
  54.     });
  55.     event.detail = detail;
  56.     node.dispatchEvent(event);
  57.     return event;
  58.   }
  59.  
  60.   _computeSeverity(stateValue, sections) {
  61.     const numberValue = Number(stateValue);
  62.     let style;
  63.     sections.forEach(section => {
  64.       if (numberValue <= section.value && !style) {
  65.         style = section.style;
  66.       }
  67.     });
  68.     return style || 'var(--label-badge-blue)';
  69.   }
  70.  
  71.   _translatePercent(value, min, max) {
  72.     return 100-100 * (value - min) / (max - min);
  73.   }
  74.  
  75.   set hass(hass) {
  76.     const config = this._config;
  77.     const root = this.shadowRoot;
  78.     const entityState = hass.states[config.entity].state;
  79.     const measurement = hass.states[config.entity].attributes.unit_of_measurement || "";
  80.  
  81.     if (entityState !== this._entityState) {
  82.       if (config.min !== undefined && config.max !== undefined) {
  83.         root.querySelector("ha-card").style.setProperty('--singlevalue-percent', `${this._translatePercent(entityState, config.min, config.max)}%`);
  84.       }
  85.       if (config.severity) {
  86.         root.querySelector("ha-card").style.setProperty('--singlevalue-fill-color', `${this._computeSeverity(entityState, config.severity)}`);
  87.       }
  88.       root.getElementById("value").textContent = `${entityState} ${measurement}`;
  89.       this._entityState = entityState;
  90.     }
  91.     root.lastChild.hass = hass;
  92.   }
  93.  
  94.   getCardSize() {
  95.     return 1;
  96.   }
  97. }
  98.  
  99. customElements.define('singlevalue-card', SingleValueCard);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement