Geen omschrijving
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gauge-card.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. class GaugeCard 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. const root = this.shadowRoot;
  11. if (root.lastChild) root.removeChild(root.lastChild);
  12. const cardConfig = Object.assign({}, config);
  13. if (!cardConfig.scale) cardConfig.scale = "50px";
  14. if (!cardConfig.min) cardConfig.min = 0;
  15. if (!cardConfig.max) cardConfig.max = 100;
  16. const entityParts = this._splitEntityAndAttribute(cardConfig.entity);
  17. cardConfig.entity = entityParts.entity;
  18. if (entityParts.attribute) cardConfig.attribute = entityParts.attribute;
  19. const card = document.createElement('ha-card');
  20. const shadow = card.attachShadow({ mode: 'open' });
  21. const content = document.createElement('div');
  22. const style = document.createElement('style');
  23. style.textContent = `
  24. ha-card {
  25. --base-unit: ${cardConfig.scale};
  26. height: calc(var(--base-unit)*3);
  27. position: relative;
  28. }
  29. .container{
  30. width: calc(var(--base-unit) * 4);
  31. height: calc(var(--base-unit) * 2);
  32. position: absolute;
  33. top: calc(var(--base-unit)*1.5);
  34. left: 50%;
  35. overflow: hidden;
  36. text-align: center;
  37. transform: translate(-50%, -50%);
  38. }
  39. .gauge-a{
  40. z-index: 1;
  41. position: absolute;
  42. background-color: var(--primary-background-color);
  43. width: calc(var(--base-unit) * 4);
  44. height: calc(var(--base-unit) * 2);
  45. top: 0%;
  46. border-radius:calc(var(--base-unit) * 2.5) calc(var(--base-unit) * 2.5) 0px 0px ;
  47. }
  48. .gauge-b{
  49. z-index: 3;
  50. position: absolute;
  51. background-color: var(--paper-card-background-color);
  52. width: calc(var(--base-unit) * 2.5);
  53. height: calc(var(--base-unit) * 1.25);
  54. top: calc(var(--base-unit) * 0.75);
  55. margin-left: calc(var(--base-unit) * 0.75);
  56. margin-right: auto;
  57. border-radius: calc(var(--base-unit) * 2.5) calc(var(--base-unit) * 2.5) 0px 0px ;
  58. }
  59. .gauge-c{
  60. z-index: 2;
  61. position: absolute;
  62. background-color: var(--label-badge-yellow);
  63. width: calc(var(--base-unit) * 4);
  64. height: calc(var(--base-unit) * 2);
  65. top: calc(var(--base-unit) * 2);
  66. margin-left: auto;
  67. margin-right: auto;
  68. border-radius: 0px 0px calc(var(--base-unit) * 2) calc(var(--base-unit) * 2) ;
  69. transform-origin: center top;
  70. transition: all 1.3s ease-in-out;
  71. }
  72. .gauge-data{
  73. z-index: 4;
  74. color: var(--primary-text-color);
  75. line-height: calc(var(--base-unit) * 0.3);
  76. position: absolute;
  77. width: calc(var(--base-unit) * 4);
  78. height: calc(var(--base-unit) * 2.1);
  79. top: calc(var(--base-unit) * 1.2);
  80. margin-left: auto;
  81. margin-right: auto;
  82. transition: all 1s ease-out;
  83. }
  84. .gauge-data #percent{
  85. font-size: calc(var(--base-unit) * 0.55);
  86. }
  87. .gauge-data #title{
  88. padding-top: calc(var(--base-unit) * 0.15);
  89. font-size: calc(var(--base-unit) * 0.30);
  90. }
  91. `;
  92. content.innerHTML = `
  93. <div class="container">
  94. <div class="gauge-a"></div>
  95. <div class="gauge-b"></div>
  96. <div class="gauge-c" id="gauge"></div>
  97. <div class="gauge-data"><div id="percent"></div><div id="title"></div></div>
  98. </div>
  99. `;
  100. card.appendChild(content);
  101. card.appendChild(style);
  102. card.addEventListener('click', event => {
  103. this._fire('hass-more-info', { entityId: cardConfig.entity });
  104. });
  105. root.appendChild(card);
  106. this._config = cardConfig;
  107. }
  108. _splitEntityAndAttribute(entity) {
  109. let parts = entity.split('.');
  110. if (parts.length < 3) {
  111. return { entity: entity };
  112. }
  113. return { attribute: parts.pop(), entity: parts.join('.') };
  114. }
  115. _fire(type, detail, options) {
  116. const node = this.shadowRoot;
  117. options = options || {};
  118. detail = (detail === null || detail === undefined) ? {} : detail;
  119. const event = new Event(type, {
  120. bubbles: options.bubbles === undefined ? true : options.bubbles,
  121. cancelable: Boolean(options.cancelable),
  122. composed: options.composed === undefined ? true : options.composed
  123. });
  124. event.detail = detail;
  125. node.dispatchEvent(event);
  126. return event;
  127. }
  128. _translateTurn(value, config) {
  129. return 5 * (value - config.min) / (config.max - config.min)
  130. }
  131. _computeSeverity(stateValue, sections) {
  132. let numberValue = Number(stateValue);
  133. const severityMap = {
  134. red: "var(--label-badge-red)",
  135. green: "var(--label-badge-green)",
  136. amber: "var(--label-badge-yellow)",
  137. normal: "var(--label-badge-blue)",
  138. }
  139. if (!sections) return severityMap["normal"];
  140. let sortable = [];
  141. for (let severity in sections) {
  142. sortable.push([severity, sections[severity]]);
  143. }
  144. sortable.sort((a, b) => { return a[1] - b[1] });
  145. if (numberValue >= sortable[0][1] && numberValue < sortable[1][1]) {
  146. return severityMap[sortable[0][0]]
  147. }
  148. if (numberValue >= sortable[1][1] && numberValue < sortable[2][1]) {
  149. return severityMap[sortable[1][0]]
  150. }
  151. if (numberValue >= sortable[2][1]) {
  152. return severityMap[sortable[2][0]]
  153. }
  154. return severityMap["normal"];
  155. }
  156. _getEntityStateValue(entity, attribute) {
  157. if (!attribute) {
  158. return entity.state;
  159. }
  160. return entity.attributes[attribute];
  161. }
  162. set hass(hass) {
  163. const config = this._config;
  164. const entityState = this._getEntityStateValue(hass.states[config.entity], config.attribute);
  165. let measurement = "";
  166. if (config.measurement == null)
  167. measurement = hass.states[config.entity].attributes.unit_of_measurement;
  168. else
  169. measurement = config.measurement;
  170. const root = this.shadowRoot;
  171. if (entityState !== this._entityState) {
  172. root.getElementById("percent").textContent = `${entityState} ${measurement}`;
  173. root.getElementById("title").textContent = config.title;
  174. const turn = this._translateTurn(entityState, config) / 10;
  175. root.getElementById("gauge").style.transform = `rotate(${turn}turn)`;
  176. root.getElementById("gauge").style.backgroundColor = this._computeSeverity(entityState, config.severity);
  177. this._entityState = entityState;
  178. }
  179. root.lastChild.hass = hass;
  180. }
  181. getCardSize() {
  182. return 1;
  183. }
  184. }
  185. customElements.define('gauge-card', GaugeCard);