|
| 1 | +<link rel="import" href="../../bower_components/polymer/polymer.html"> |
| 2 | + |
| 3 | +<link rel="import" href="../../bower_components/iron-flex-layout/iron-flex-layout-classes.html"> |
| 4 | +<link rel="import" href="../util/hass-util.html"> |
| 5 | + |
| 6 | +<dom-module id="ha-attributes"> |
| 7 | + <template> |
| 8 | + <style is="custom-style" include="iron-flex iron-flex-alignment"></style> |
| 9 | + <style> |
| 10 | + .data-entry .value { |
| 11 | + max-width: 200px; |
| 12 | + } |
| 13 | + .attribution { |
| 14 | + color: var(--secondary-text-color); |
| 15 | + text-align: right; |
| 16 | + } |
| 17 | + </style> |
| 18 | + |
| 19 | + <div class='layout vertical'> |
| 20 | + <template is='dom-repeat' items="[[computeDisplayAttributes(stateObj, filtersArray)]]" as="attribute"> |
| 21 | + <div class='data-entry layout justified horizontal'> |
| 22 | + <div class='key'>[[formatAttribute(attribute)]]</div> |
| 23 | + <div class='value'>[[formatAttributeValue(stateObj, attribute)]]</div> |
| 24 | + </div> |
| 25 | + </template> |
| 26 | + <div class='attribution' hidden$='[[!computeAttribution(stateObj)]]'>[[computeAttribution(stateObj)]]</div> |
| 27 | + </div> |
| 28 | + </template> |
| 29 | +</dom-module> |
| 30 | + |
| 31 | +<script> |
| 32 | +(function () { |
| 33 | + 'use strict'; |
| 34 | + |
| 35 | + var FILTER_KEYS = [ |
| 36 | + 'entity_picture', 'friendly_name', 'icon', 'unit_of_measurement', |
| 37 | + 'emulated_hue', 'emulated_hue_name', 'haaska_hidden', 'haaska_name', |
| 38 | + 'homebridge_hidden', 'homebridge_name', 'supported_features', 'attribution', |
| 39 | + ]; |
| 40 | + Polymer({ |
| 41 | + is: 'ha-attributes', |
| 42 | + properties: { |
| 43 | + stateObj: { |
| 44 | + type: Object, |
| 45 | + }, |
| 46 | + extraFilters: { |
| 47 | + type: String, |
| 48 | + value: '', |
| 49 | + }, |
| 50 | + filtersArray: { |
| 51 | + type: Array, |
| 52 | + computed: 'computeFiltersArray(extraFilters)', |
| 53 | + }, |
| 54 | + }, |
| 55 | + |
| 56 | + computeFiltersArray: function (extraFilters) { |
| 57 | + return FILTER_KEYS + (extraFilters ? extraFilters.split(',') : []); |
| 58 | + }, |
| 59 | + |
| 60 | + computeDisplayAttributes: function (stateObj, filtersArray) { |
| 61 | + if (!stateObj) { |
| 62 | + return []; |
| 63 | + } |
| 64 | + return Object.keys(stateObj.attributes).filter( |
| 65 | + function (key) { |
| 66 | + return filtersArray.indexOf(key) === -1; |
| 67 | + }); |
| 68 | + }, |
| 69 | + |
| 70 | + formatAttribute: function (attribute) { |
| 71 | + return attribute.replace(/_/g, ' '); |
| 72 | + }, |
| 73 | + |
| 74 | + formatAttributeValue: function (stateObj, attribute) { |
| 75 | + var value = stateObj.attributes[attribute]; |
| 76 | + if (Array.isArray(value)) { |
| 77 | + return value.join(', '); |
| 78 | + } |
| 79 | + return (value instanceof Object) ? JSON.stringify(value, null, 2) : value; |
| 80 | + }, |
| 81 | + |
| 82 | + computeAttribution: function (stateObj) { |
| 83 | + return stateObj.attributes.attribution; |
| 84 | + }, |
| 85 | + }); |
| 86 | +}()); |
| 87 | +</script> |
0 commit comments