PlatoNL

Triplevaluecard

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