|
| 1 | +class CustomBinaryRow extends Polymer.Element { |
| 2 | + |
| 3 | + static get template() { |
| 4 | + return Polymer.html` |
| 5 | + <style is="custom-style" include="iron-flex iron-flex-alignment"></style> |
| 6 | + <style> |
| 7 | + :host { |
| 8 | + line-height: inherit; |
| 9 | + } |
| 10 | + .switch { |
| 11 | + min-width: 30px; |
| 12 | + max-width: 30px; |
| 13 | + height: 30px; |
| 14 | + margin-left: 2px; |
| 15 | + margin-right: 2px; |
| 16 | + background-color: #759aaa; |
| 17 | + border: 1px solid lightgrey; |
| 18 | + border-radius: 4px; |
| 19 | + font-size: 10px !important; |
| 20 | + color: inherit; |
| 21 | + text-align: center; |
| 22 | + float: right !important; |
| 23 | + padding: 1px; |
| 24 | + } |
| 25 | + |
| 26 | + </style> |
| 27 | + <hui-generic-entity-row hass="[[hass]]" config="[[_config]]"> |
| 28 | + <div class='horizontal justified layout' on-click="stopPropagation"> |
| 29 | + <button |
| 30 | + class='switch' |
| 31 | + style='[[_onColor]]' |
| 32 | + toggles name="on" |
| 33 | + on-click='setState' |
| 34 | + disabled='[[_isOnState]]'>ON</button> |
| 35 | + <button |
| 36 | + class='switch' |
| 37 | + style='[[_offColor]]' |
| 38 | + toggles name="off" |
| 39 | + on-click='setState' |
| 40 | + disabled='[[_isOffState]]'>OFF</button> |
| 41 | + </div> |
| 42 | + </hui-generic-entity-row> |
| 43 | + `; |
| 44 | + } |
| 45 | + |
| 46 | + static get properties() { |
| 47 | + return { |
| 48 | + hass: { |
| 49 | + type: Object, |
| 50 | + observer: 'hassChanged' |
| 51 | + }, |
| 52 | + _config: Object, |
| 53 | + _stateObj: Object, |
| 54 | + _onColor: String, |
| 55 | + _offColor: String, |
| 56 | + _isOffState: Boolean, |
| 57 | + _isOnState: Boolean, |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + setConfig(config) { |
| 62 | + this._config = config; |
| 63 | + |
| 64 | + this._config = { |
| 65 | + customTheme: false, |
| 66 | + IsOnColor: '#43A047', |
| 67 | + IsOffColor: '#f44c09', |
| 68 | + ButtonInactiveColor: '#759aaa', |
| 69 | + ...config |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + hassChanged(hass) { |
| 74 | + |
| 75 | + const config = this._config; |
| 76 | + const stateObj = hass.states[config.entity]; |
| 77 | + const custTheme = config.customTheme; |
| 78 | + const custOnClr = config.IsOnColor; |
| 79 | + const custOffClr = config.IsOffColor; |
| 80 | + const custInactiveClr = config.ButtonInactiveColor; |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + let state; |
| 86 | + if (stateObj) { |
| 87 | + state = stateObj.state; |
| 88 | + } |
| 89 | + |
| 90 | + let onstate; |
| 91 | + let offstate; |
| 92 | + |
| 93 | + if (stateObj) { |
| 94 | + if (stateObj.state == 'on') { |
| 95 | + onstate = 'on'; |
| 96 | + } else { |
| 97 | + offstate = 'on'; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + let oncolor; |
| 102 | + let offcolor; |
| 103 | + |
| 104 | + if (custTheme) { |
| 105 | + |
| 106 | + if (onstate == 'on') { |
| 107 | + oncolor = 'background-color:' + custOnClr; |
| 108 | + } else { |
| 109 | + oncolor = 'background-color:' + custInactiveClr; |
| 110 | + } |
| 111 | + |
| 112 | + if (offstate == 'on') { |
| 113 | + offcolor = 'background-color:' + custOffClr; |
| 114 | + } else { |
| 115 | + offcolor = 'background-color:' + custInactiveClr; |
| 116 | + } |
| 117 | + |
| 118 | + } else { |
| 119 | + |
| 120 | + if (onstate == 'on') { |
| 121 | + oncolor = 'background-color: var(--primary-color)'; |
| 122 | + } else { |
| 123 | + oncolor = 'background-color: var(--disabled-text-color)'; |
| 124 | + } |
| 125 | + |
| 126 | + if (offstate == 'on') { |
| 127 | + offcolor = 'background-color: var(--primary-color)'; |
| 128 | + } else { |
| 129 | + offcolor = 'background-color: var(--disabled-text-color)'; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + this.setProperties({ |
| 135 | + _stateObj: stateObj, |
| 136 | + _isOffState: stateObj.state == 'off', |
| 137 | + _isOnState: stateObj.state === 'on', |
| 138 | + _onColor: oncolor, |
| 139 | + _offColor: offcolor, |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + stopPropagation(e) { |
| 144 | + e.stopPropagation(); |
| 145 | + } |
| 146 | + |
| 147 | + setState(e) { |
| 148 | + const state = e.currentTarget.getAttribute('name'); |
| 149 | + if( state == 'off' ){ |
| 150 | + this.hass.callService('homeassistant', 'turn_off', {entity_id: this._config.entity}); |
| 151 | + } else { |
| 152 | + this.hass.callService('homeassistant', 'turn_on', {entity_id: this._config.entity}); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | +} |
| 157 | + |
| 158 | +customElements.define('binary-control-button-row', CustomBinaryRow); |
0 commit comments