PlatoNL

Untitled

May 9th, 2019
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class DoubleValueCard 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 title = document.createElement('div');
  20.     title.id = "title"
  21.     const style = document.createElement('style');
  22.     style.textContent = `
  23.       ha-card {
  24.         box-shadow: none;
  25.         text-align: center;
  26.         --doublevalue-fill-color: var(--label-badge-blue);
  27.         --doublevalue-percent: 100%;
  28.         --doublevalue-direction: ${cardConfig.from};
  29.         --base-unit: ${cardConfig.scale};
  30.         padding: calc(var(--base-unit)*0.6) calc(var(--base-unit)*0.3);
  31.         background: linear-gradient(to var(--doublevalue-direction), var(--paper-card-background-color) var(--doublevalue-percent), var(--doublevalue-fill-color) var(--doublevalue-percent));
  32.       }
  33.       #value {
  34.         font-size: calc(var(--base-unit) * 1.3);
  35.         line-height: calc(var(--base-unit) * 1.3);
  36.         color: var(--primary-text-color);
  37.       }
  38.       #title {
  39.         font-size: calc(var(--base-unit) * 1.0);
  40.         line-height: calc(var(--base-unit) * 1.0);
  41.         color: var(--primary-text-color);
  42.       }
  43.     `;
  44.     card.appendChild(content);
  45.     card.appendChild(title);
  46.     card.appendChild(style);
  47.     card.addEventListener('click', event => {
  48.       this._fire('hass-more-info', { entityId: cardConfig.entity });
  49.     });
  50.     root.appendChild(card);
  51.     this._config = cardConfig;
  52.   }
  53.  
  54.   _fire(type, detail, options) {
  55.     const node = this.shadowRoot;
  56.     options = options || {};
  57.     detail = (detail === null || detail === undefined) ? {} : detail;
  58.     const event = new Event(type, {
  59.       bubbles: options.bubbles === undefined ? true : options.bubbles,
  60.       cancelable: Boolean(options.cancelable),
  61.       composed: options.composed === undefined ? true : options.composed
  62.     });
  63.     event.detail = detail;
  64.     node.dispatchEvent(event);
  65.     return event;
  66.   }
  67.  
  68.   _computeSeverity(stateValue, sections) {
  69.     const numberValue = Number(stateValue);
  70.     let style;
  71.     sections.forEach(section => {
  72.       if (numberValue <= section.value && !style) {
  73.         style = section.style;
  74.       }
  75.     });
  76.     return style || 'var(--label-badge-blue)';
  77.   }
  78.  
  79.   _translatePercent(value, min, max) {
  80.     return 100-100 * (value - min) / (max - min);
  81.   }
  82.  
  83.   set hass(hass) {
  84.     const config = this._config;
  85.     const root = this.shadowRoot;
  86.     const entityState = hass.states[config.entity].state;
  87.     const measurement = hass.states[config.entity].attributes.unit_of_measurement || "";
  88.     const title_entityState = hass.states[config.title_entity].state;
  89.     const title_measurement = hass.states[config.title_entity].attributes.unit_of_measurement || "";
  90.  
  91.  
  92.     if (entityState !== this._entityState) {
  93.       if (config.min !== undefined && config.max !== undefined) {
  94.         root.querySelector("ha-card").style.setProperty('--doublevalue-percent', `${this._translatePercent(entityState, config.min, config.max)}%`);
  95.       }
  96.       if (config.severity) {
  97.         root.querySelector("ha-card").style.setProperty('--doublevalue-fill-color', `${this._computeSeverity(entityState, config.severity)}`);
  98.       }
  99.       root.getElementById("value").textContent = `${entityState} ${measurement}`;
  100.       this._entityState = entityState;
  101.       root.getElementById("title").textContent = `${title_entityState} ${title_measurement}`;
  102.       this._title_entityState = title_entityState
  103.     }
  104.     root.lastChild.hass = hass;
  105.   }
  106.  
  107.   getCardSize() {
  108.     return 1;
  109.   }
  110. }
  111.  
  112. customElements.define('doublevalue-card', DoubleValueCard);
Advertisement
Add Comment
Please, Sign In to add comment