Advertisement
Guest User

Untitled

a guest
Mar 27th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ((LitElement, ButtonBase) => {
  2.   var html = LitElement.prototype.html;
  3.   var css = LitElement.prototype.css;
  4.  
  5.   customElements.define(
  6.     'button-card-button',
  7.     class extends ButtonBase {
  8.       static get styles() {
  9.         return css`
  10.           ${ButtonBase.styles}
  11.           .mdc-button {
  12.             height: auto;
  13.             padding: 0;
  14.             color: inherit !important;
  15.           }
  16.         `
  17.       }
  18.     },
  19.   );
  20.  
  21.   class ButtonCard extends LitElement {
  22.     static get properties() {
  23.       return {
  24.         hass: Object,
  25.         config: Object,
  26.       };
  27.     }
  28.  
  29.     static get styles() {
  30.       return css`
  31.         ha-icon {
  32.           display: flex;
  33.           margin: auto;
  34.         }
  35.         button-card-button {
  36.           display: flex;
  37.           margin: auto;
  38.           text-align: center;
  39.         }
  40.         button-card-button div {
  41.           padding: 2.6%;
  42.           text-transform: none;
  43.           font-size: 1.1rem;
  44.           font-weight: 500;
  45.           white-space: nowrap;
  46.         }
  47.       `;
  48.     }
  49.  
  50.     render() {
  51.       const state = this.__hass.states[this.config.entity];
  52.       switch (this.config.color_type) {
  53.         case 'blank-card':
  54.           return this.blankCardColoredHtml(state, this.config);
  55.         case 'label-card':
  56.           return this.labelCardColoredHtml(state, this.config);
  57.         case 'card':
  58.           return this.cardColoredHtml(state, this.config);
  59.         case 'link-card':
  60.           return this.linkCardColoredHtml(state, this.config);
  61.         case 'space-card':
  62.           return this.cardSpacerHtml(state, this.config);
  63.         case 'icon':
  64.         default:
  65.           return this.iconColoredHtml(state, this.config);
  66.       }
  67.     }
  68.  
  69.  
  70.     getFontColorBasedOnBackgroundColor(backgroundColor) {
  71.       const parsedRgbColor= backgroundColor.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);
  72.       const parsedBackgroundColor = parsedRgbColor ? parsedRgbColor : this.hexToRgb(backgroundColor.substring(1));
  73.       let fontColor = ''; // don't override by default
  74.       if (parsedBackgroundColor) {
  75.         // Counting the perceptive luminance - human eye favors green color...
  76.         const luminance = (0.299 * parsedBackgroundColor[1] + 0.587 * parsedBackgroundColor[2] + 0.114 * parsedBackgroundColor[3]) / 255;
  77.         if (luminance > 0.5) {
  78.           fontColor = 'rgb(62, 62, 62)'; // bright colors - black font
  79.         } else {
  80.           fontColor = 'rgb(234, 234, 234)';// dark colors - white font
  81.         }
  82.       }
  83.       return fontColor;
  84.     }
  85.  
  86.  
  87.     hexToRgb(hex) {
  88.       var bigint = parseInt(hex, 16);
  89.       var r = (bigint >> 16) & 255;
  90.       var g = (bigint >> 8) & 255;
  91.       var b = bigint & 255;
  92.  
  93.       return [,r,g,b];
  94.     }
  95.  
  96.  
  97.     buildCssColorAttribute(state, config) {
  98.       let color = config.color;
  99.       if (state) {
  100.         let configState = config.state ? config.state.find(configState => { return configState.value === state.state; }) : false;
  101.         if(configState){
  102.           color = configState.color ? configState.color : config.color_off;
  103.           if (configState.color === 'auto') {
  104.             color = state.attributes.rgb_color ? `rgb(${state.attributes.rgb_color.join(',')})` : configState.default_color;
  105.           }
  106.         }else{
  107.           if (config.color === 'auto') {
  108.             color = state.attributes.rgb_color ? `rgb(${state.attributes.rgb_color.join(',')})` : config.default_color;
  109.           }
  110.           color = state.state === 'on' ? color : config.color_off;
  111.         }
  112.       }
  113.       return color;
  114.     }
  115.  
  116.     buildIcon(state, config) {
  117.       let iconOff = config.icon;
  118.       if (config.icon == 'attribute') {
  119.         if (state) {
  120.           const icon = state.attributes.icon;
  121.           return icon;
  122.         }
  123.         return iconOff;
  124.       }
  125.       let configState = config.state ? config.state.find(configState => { return configState.value === state.state; }) : false;
  126.       if (configState && configState.icon) {
  127.         const icon = configState.icon;
  128.         return icon;
  129.       }
  130.       return iconOff;
  131.     }
  132.  
  133.     blankCardColoredHtml(state, config) {
  134.       const color = this.buildCssColorAttribute(state, config);
  135.       const fontColor = this.getFontColorBasedOnBackgroundColor(color);
  136.       return html`
  137.       <ha-card style="color: ${fontColor}; background-color: ${color}; ${config.card_style}" on-tap="${ev => this._toggle(state, config)}">
  138.       </ha-card>
  139.       `;
  140.     }
  141.  
  142.     labelCardColoredHtml(state, config) {
  143.       const color = this.buildCssColorAttribute(state, config);
  144.       const fontColor = this.getFontColorBasedOnBackgroundColor(color);
  145.       return html`
  146.       <ha-card style="color: ${fontColor};">
  147.         <button-card-button disabled style="background-color: ${color}">
  148.         <div style="${config.card_style}">
  149.           ${config.icon ? html`<ha-icon style="width: ${config.size}; height: ${config.size};" icon="${config.icon}"></ha-icon>` : ''}
  150.           ${config.name ? html`<span>${config.name}</span>` : ''}
  151.          </div>
  152.         </button-card-button>
  153.       </ha-card>
  154.       `;
  155.     }
  156.  
  157.     cardColoredHtml(state, config) {
  158.       const color = this.buildCssColorAttribute(state, config);
  159.       const fontColor = this.getFontColorBasedOnBackgroundColor(color);
  160.       return html`
  161.       <ha-card style="color: ${fontColor};" @tap="${ev => this._toggle(state, config)}">
  162.         <button-card-button style="background-color: ${color}; ${config.card_style}">
  163.         <div style="${config.card_style}">
  164.           ${config.icon ? html`<ha-icon style="width: ${config.size}; height: ${config.size};" icon="${config.icon}"></ha-icon>` : ''}
  165.           ${config.name ? html`<span>${config.name}</span>` : ''}
  166.           ${config.show_state ? html`<span>${state.state} ${state.attributes.unit_of_measurement ? state.attributes.unit_of_measurement : ''}</span>` : ''}
  167.          </div>
  168.         </button-card-button>
  169.       </ha-card>
  170.       `;
  171.     }
  172.  
  173.     linkCardColoredHtml(state, config) {
  174.       const color = this.buildCssColorAttribute(state, config);
  175.       const fontColor = this.getFontColorBasedOnBackgroundColor(color);
  176.       return html`
  177.       <ha-card style="color: ${fontColor}">
  178.         <a href="${config.url}">
  179.           <button-card-button style="background-color: ${color}; ${config.card_style}">
  180.           <div style="${config.card_style}">
  181.             ${config.icon ? html`<ha-icon style="width: ${config.size}; height: ${config.size};" icon="${config.icon}"></ha-icon>` : ''}
  182.             ${config.name ? html`<span>${config.name}</span>` : ''}
  183.           </div>
  184.           </button-card-button>
  185.         </a>
  186.       </ha-card>
  187.       `;
  188.     }
  189.  
  190.     cardSpacerHtml(state, config) {
  191.       const color = this.buildCssColorAttribute(state, config);
  192.       const fontColor = this.getFontColorBasedOnBackgroundColor(color);
  193.       return html`
  194.         <div style="height: ${config.div}; background-color: transparent">
  195.         </div>
  196.       `;
  197.     }
  198.  
  199.     iconColoredHtml(state, config) {
  200.       const color = this.buildCssColorAttribute(state, config);
  201.       const icon = this.buildIcon(state, config);
  202.       const fontColor = this.getFontColorBasedOnBackgroundColor(color);
  203.       return html`
  204.       <ha-card style="color: ${fontColor};" @tap="${ev => this._toggle(state, config)}">
  205.         <button-card-button style="background-color: ${color}; ${config.card_style}">
  206.         <div style="${config.card_style}">
  207.           ${config.icon ? html`<ha-icon style="color: ${color}; width: ${config.size}; height: ${config.size};" icon="${icon}"></ha-icon>` : ''}
  208.           ${config.name ? html`<span>${config.name}</span>` : ''}
  209.           ${config.show_state ? html`<span>${state.state} ${state.attributes.unit_of_measurement ? state.attributes.unit_of_measurement : ''}</span>` : ''}
  210.         </div>
  211.         </button-card-button>
  212.       </ha-card>
  213.       `;
  214.     }
  215.  
  216.     setConfig(config) {
  217.       // if (!config.entity) {
  218.       //   throw new Error('You need to define entity');
  219.       // }
  220.       this.config = {...config};
  221.       this.config.color = config.color ? config.color : 'var(--primary-text-color)';
  222.       this.config.size = config.size ? config.size : '40%';
  223.       this.config.div = config.div ? config.div : '60px';
  224.       let cardStyle = '';
  225.       if (config.style) {
  226.         config.style.forEach((cssObject) => {
  227.           const attribute = Object.keys(cssObject)[0];
  228.           const value = cssObject[attribute];
  229.           cardStyle += `${attribute}: ${value};\n`;
  230.         });
  231.       }
  232.       this.config.color_type = config.color_type ? config.color_type : 'icon';
  233.       this.config.color_off = config.color_off ? config.color_off : 'var(--disabled-text-color)';
  234.       this.config.default_color = config.default_color ? config.default_color : 'var(--primary-text-color)';
  235.       this.config.card_style = cardStyle;
  236.       this.config.name = config.name ? config.name : '';
  237.     }
  238.  
  239.     // The height of your card. Home Assistant uses this to automatically
  240.     // distribute all cards over the available columns.
  241.     getCardSize() {
  242.       return 3;
  243.     }
  244.  
  245.     _toggle(state, config) {
  246.       switch (config.action) {
  247.         case 'toggle':
  248.           this.hass.callService('homeassistant', 'toggle', {
  249.             entity_id: state.entity_id,
  250.           });
  251.           break;
  252.         case 'more_info': {
  253.           const node = this.shadowRoot;
  254.           const options = {};
  255.           const detail = { entityId: state.entity_id };
  256.           const event = new Event('hass-more-info', {
  257.             bubbles: options.bubbles === undefined ? true : options.bubbles,
  258.             cancelable: Boolean(options.cancelable),
  259.             composed: options.composed === undefined ? true : options.composed,
  260.           });
  261.           event.detail = detail;
  262.           node.dispatchEvent(event);
  263.           return event;
  264.         }
  265.         case 'service':
  266.           this.hass.callService(config.service.domain, config.service.action, config.service.data);
  267.           break;
  268.         default:
  269.           this.hass.callService('homeassistant', 'toggle', {
  270.             entity_id: state.entity_id,
  271.           });
  272.           break;
  273.       }
  274.     }
  275.   }
  276.  
  277.   customElements.define('button-card', ButtonCard);
  278. })(window.LitElement || Object.getPrototypeOf(customElements.get("home-assistant-main")), customElements.get('mwc-button') || customElements.get('paper-button'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement