Skip to content

Commit c87ab9e

Browse files
authored
add custom button text
1 parent e8d51c3 commit c87ab9e

File tree

1 file changed

+92
-83
lines changed

1 file changed

+92
-83
lines changed

dist/binary-control-button-row.js

Lines changed: 92 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,36 @@ class CustomBinaryRow extends Polymer.Element {
99
}
1010
.switch {
1111
min-width: 30px;
12-
max-width: 30px;
13-
height: 30px;
14-
margin-left: 2px;
12+
max-width: 30px;
13+
height: 30px;
14+
margin-left: 2px;
1515
margin-right: 2px;
1616
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-
}
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+
}
2525
2626
</style>
2727
<hui-generic-entity-row hass="[[hass]]" config="[[_config]]">
2828
<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>
29+
<button
30+
class='switch'
31+
style='[[_onColor]]'
32+
toggles name="on"
33+
on-click='setState'
34+
disabled='[[_isOnState]]'>[[_onText]]</button>
35+
<button
36+
class='switch'
37+
style='[[_offColor]]'
38+
toggles name="off"
39+
on-click='setState'
40+
disabled='[[_isOffState]]'>[[_offText]]</button>
41+
</div>
4242
</hui-generic-entity-row>
4343
`;
4444
}
@@ -51,9 +51,11 @@ class CustomBinaryRow extends Polymer.Element {
5151
},
5252
_config: Object,
5353
_stateObj: Object,
54-
_onColor: String,
55-
_offColor: String,
56-
_isOffState: Boolean,
54+
_onColor: String,
55+
_offColor: String,
56+
_onText: String,
57+
_offText: String,
58+
_isOffState: Boolean,
5759
_isOnState: Boolean,
5860
}
5961
}
@@ -62,82 +64,89 @@ class CustomBinaryRow extends Polymer.Element {
6264
this._config = config;
6365

6466
this._config = {
65-
customTheme: false,
66-
IsOnColor: '#43A047',
67-
IsOffColor: '#f44c09',
68-
ButtonInactiveColor: '#759aaa',
69-
...config
67+
customTheme: false,
68+
IsOnColor: '#43A047',
69+
IsOffColor: '#f44c09',
70+
ButtonInactiveColor: '#759aaa',
71+
customOffText: 'OFF',
72+
customOnText: 'ON',
73+
...config
7074
};
7175
}
7276

7377
hassChanged(hass) {
7478

7579
const config = this._config;
7680
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+
const custTheme = config.customTheme;
82+
const custOnClr = config.IsOnColor;
83+
const custOffClr = config.IsOffColor;
84+
const custInactiveClr = config.ButtonInactiveColor;
85+
const custOffTxt = config.customOffText;
86+
const custOnTxt = config.customOnText;
8187

8288

8389

8490

85-
let state;
86-
if (stateObj) {
87-
state = stateObj.state;
88-
}
89-
90-
let onstate;
91-
let offstate;
92-
91+
let state;
9392
if (stateObj) {
94-
if (stateObj.state == 'on') {
95-
onstate = 'on';
96-
} else {
97-
offstate = 'on';
98-
}
93+
state = stateObj.state;
9994
}
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-
}
95+
96+
let onstate;
97+
let offstate;
98+
99+
if (stateObj) {
100+
if (stateObj.state == 'on') {
101+
onstate = 'on';
102+
} else {
103+
offstate = 'on';
104+
}
105+
}
106+
107+
let oncolor;
108+
let offcolor;
109+
110+
if (custTheme) {
111111

112-
if (offstate == 'on') {
113-
offcolor = 'background-color:' + custOffClr;
114-
} else {
115-
offcolor = 'background-color:' + custInactiveClr;
116-
}
112+
if (onstate == 'on') {
113+
oncolor = 'background-color:' + custOnClr;
114+
} else {
115+
oncolor = 'background-color:' + custInactiveClr;
116+
}
117117

118+
if (offstate == 'on') {
119+
offcolor = 'background-color:' + custOffClr;
118120
} else {
121+
offcolor = 'background-color:' + custInactiveClr;
122+
}
119123

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-
}
124+
} else {
125+
if (onstate == 'on') {
126+
oncolor = 'background-color: var(--primary-color)';
127+
} else {
128+
oncolor = 'background-color: var(--disabled-text-color)';
131129
}
132130

131+
if (offstate == 'on') {
132+
offcolor = 'background-color: var(--primary-color)';
133+
} else {
134+
offcolor = 'background-color: var(--disabled-text-color)';
135+
}
136+
}
137+
138+
let offtext = custOffTxt;
139+
let ontext = custOnTxt;
133140

134-
this.setProperties({
135-
_stateObj: stateObj,
136-
_isOffState: stateObj.state == 'off',
137-
_isOnState: stateObj.state === 'on',
138-
_onColor: oncolor,
139-
_offColor: offcolor,
140-
});
141+
this.setProperties({
142+
_stateObj: stateObj,
143+
_isOffState: stateObj.state == 'off',
144+
_isOnState: stateObj.state === 'on',
145+
_onColor: oncolor,
146+
_offColor: offcolor,
147+
_offText: offtext,
148+
_onText: ontext,
149+
});
141150
}
142151

143152
stopPropagation(e) {

0 commit comments

Comments
 (0)