Skip to content

Commit 1630cd0

Browse files
authored
Merge pull request #7 from kuuji/0.0.1
new release
2 parents e6dedd9 + 351973d commit 1630cd0

File tree

7 files changed

+216
-46
lines changed

7 files changed

+216
-46
lines changed

README.md

Lines changed: 121 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
11
# Button card
22

3-
Simple button card for your entities.
3+
Lovelace Button card for your entities.
44

5-
![all](all.png)
5+
![all](all.gif)
66

77
## Features
88

99
- works with any toggleable entity
1010
- 2 actions on tap `toggle` and `more_info` (more to come)
11-
- shows state if no icon specified for a very simple on/off button
12-
- custom color for `on` state
13-
- custom size
14-
- custom icon
15-
- icon automagically takes the color of light entities if rgb attribute is present
11+
- state display (optional)
12+
- custom color for `on` and `off` state (optional)
13+
- custom size (optional)
14+
- custom icon (optional)
15+
- custom css style (optional)
16+
- automatic color for light (optional)
17+
- custom default color for lights (when color cannot be determined) (optional)
18+
- 2 color types
19+
- `icon` : apply color settings to the icon only
20+
- `card` : apply color settings to the card only
21+
- automatic font color if color_type is set to `card`
1622

1723
## Options
1824

19-
| Name | Type | Default | Example | Description
25+
| Name | Type | Default | Supported options | Description
2026
| ---- | ---- | ------- | --------- | -----------
2127
| type | string | **Required** | `custom:button-card` | Type of the card
2228
| entity | string | **Required** | `switch.ac` | entity_id
2329
| icon | string | optional | `mdi:air-conditioner` | Icon to display in place of the state
24-
| color | string | `var(--primary-text-color)` | `rgb(28, 128, 199)` | Color of the icon when state is `on`. Can be any html color
30+
| color_type | string | `icon` | `icon` \| `card` | Color either the background of the card or the icon inside the card. Setting this to `card` enable automatic `font` and `icon` color. This allows the text/icon to be readable even if the background color is bright/dark.
31+
| color | string | `var(--primary-text-color)` | `auto` \| `rgb(28, 128, 199)` | Color of the icon/card when state is `on`. `auto` sets the color based on the color of a light.
32+
| color_off | string | `var(--disabled-text-color)` | `rgb(28, 128, 199)` | Color of the icon/card when state is `off`.
2533
| size | string | `40%` | `20px` | Size of the icon. Can be percentage or pixel
2634
| action | string | `toggle` | `toggle` \| `more_info` | Define the type of action
2735
| name | string | optional | `Air conditioner` | Define an optional text to show below the icon
36+
| show_state | boolean | `false` | `true` \| `false` | Show the state on the card. defaults to false if not set
2837
| style | object | optional | `- text-transform: none` | Define a list of css attribute and their value to apply to the card
2938

3039
## Instructions
@@ -44,47 +53,70 @@ resources:
4453

4554

4655
Show a button for the air conditioner (blue when on):
56+
57+
![ac](ac.png)
58+
4759
```yaml
4860
- type: "custom:button-card"
4961
entity: switch.ac
5062
icon: mdi:air-conditioner
5163
color: rgb(28, 128, 199)
5264
```
53-
![ac](ac.png)
65+
---------
5466

5567
Show an ON/OFF button for the home_lights group:
68+
69+
![no-icon](no_icon.png)
70+
5671
```yaml
5772
- type: "custom:button-card"
5873
entity: group.home_lights
74+
show_state: true
5975
```
60-
![no-icon](no_icon.png)
6176

62-
Light entity with custom icon and "more info" pop-in
77+
78+
----------------
79+
80+
Light entity with custom icon and "more info" pop-in:6
81+
82+
![sofa](sofa.png)
83+
6384
```yaml
6485
- type: "custom:button-card"
6586
entity: light.living_room_lights
6687
icon: mdi:sofa
88+
color: auto
6789
action: more_info
6890
```
69-
![sofa](sofa.png)
7091

71-
Light card with text
92+
93+
-------------------------
94+
95+
Light card with text:
96+
97+
![text](text.png)
7298

7399
```yaml
74100
- type: "custom:button-card"
75101
entity: light.living_room_lights
76102
icon: mdi:sofa
103+
color: auto
77104
name: Living room
78105
```
79106

80-
![text](text.png)
81107

82-
Light card with text and custom style
108+
109+
-------------
110+
111+
Light card with text and custom style:
112+
113+
![home-custom](home-custom.png)
83114

84115
```yaml
85116
- type: "custom:button-card"
86117
entity: light._
87118
icon: mdi:home
119+
color: auto
88120
action: more_info
89121
name: Home
90122
style:
@@ -93,7 +125,79 @@ Light card with text and custom style
93125
- font-weight: bold
94126
```
95127

96-
![home-custom](home-custom.png)
128+
129+
-----
130+
131+
Light card with card color type, name, and automatic color:
132+
133+
![color](color.gif)
134+
135+
```yaml
136+
- type: "custom:button-card"
137+
entity: light._
138+
icon: mdi:home
139+
color: auto
140+
color_type: card
141+
default_color: rgb(255, 233, 155)
142+
action: more_info
143+
name: Home
144+
style:
145+
- font-size: 12px
146+
- font-weight: bold
147+
```
148+
149+
---------------
150+
151+
Home + all rooms in an horizontal stack
152+
153+
![all-home](all-home.png)
154+
155+
156+
```yaml
157+
- type: horizontal-stack
158+
cards:
159+
- type: "custom:button-card"
160+
entity: light.living_room_lights
161+
icon: mdi:sofa
162+
color: auto
163+
action: more_info
164+
default_color: rgb(255, 233, 155)
165+
color_type: card
166+
name: Living room
167+
style:
168+
- font-size: 12px
169+
- font-weight: bold
170+
- type: "custom:button-card"
171+
entity: light.harry
172+
color: auto
173+
icon: mdi:ceiling-light
174+
action: more_info
175+
name: Ceiling
176+
style:
177+
- font-size: 12px
178+
- font-weight: bold
179+
- type: "custom:button-card"
180+
entity: light.ron
181+
color: auto
182+
icon: mdi:lamp
183+
action: more_info
184+
name: TV
185+
style:
186+
- font-size: 12px
187+
- font-weight: bold
188+
- type: "custom:button-card"
189+
entity: light.snape
190+
icon: mdi:floor-lamp
191+
color: auto
192+
action: more_info
193+
name: Floor
194+
style:
195+
- font-size: 12px
196+
- font-weight: bold
197+
```
198+
199+
200+
97201

98202
## Credits
99203

all-home.png

6.58 KB
Loading

all.gif

11.2 MB
Loading

all.png

-320 Bytes
Loading

button-card.js

Lines changed: 74 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,66 +7,96 @@ class ButtonCard extends LitElement {
77
static get properties() {
88
return {
99
hass: Object,
10-
config: Object,
10+
config: Object
1111
}
1212
}
1313

1414
_render({ hass, config }) {
1515
const state = hass.states[config.entity];
16-
if (config.icon) {
17-
return this.icon(state, config);
16+
switch (config.color_type) {
17+
case "card":
18+
return this.card_colored_html(state, config);
19+
break;
20+
case "icon":
21+
default:
22+
return this.icon_colored_html(state, config);
23+
break;
24+
}
25+
}
26+
27+
28+
get_font_color_based_on_background_color(background_color) {
29+
background_color = background_color.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);
30+
let fontColor = ""; // don't override by default
31+
if (background_color) {
32+
// Counting the perceptive luminance - human eye favors green color...
33+
const luminance = (0.299 * background_color[1] + 0.587 * background_color[2] + 0.114 * background_color[3]) / 255;
34+
if (luminance > 0.5) {
35+
fontColor = "rgb(62, 62, 62)"; // bright colors - black font
36+
} else {
37+
fontColor = "rgb(234, 234, 234)"// dark colors - white font
38+
}
1839
}
19-
return this.no_icon(state, config);
40+
return fontColor;
2041
}
2142

43+
build_css_color_attribute(state, config) {
44+
let color_on = config.color;
45+
if (config.color == "auto") {
46+
color_on = state.attributes.rgb_color ? `rgb(${state.attributes.rgb_color.join(',')})` : config.default_color;
47+
}
48+
let color = state.state === 'on' ? color_on : config.color_off;
49+
return color;
50+
}
2251

23-
no_icon(state, config) {
52+
card_colored_html(state, config) {
53+
const color = this.build_css_color_attribute(state, config);
54+
const fontColor = this.get_font_color_based_on_background_color(color);
2455
return html`
2556
<style>
57+
ha-icon {
58+
display: flex;
59+
margin: auto;
60+
}
2661
paper-button {
27-
display: block;
62+
display: flex;
2863
margin: auto;
2964
text-align: center;
3065
}
3166
</style>
32-
<ha-card>
33-
<paper-button on-tap="${ev => this._toggle(state, config)}">
34-
${state.state}
67+
<ha-card style="color: ${fontColor};" on-tap="${ev => this._toggle(state, config)}">
68+
<paper-button style="background-color: ${color}; ${config.card_style}">
69+
<div>
70+
${config.icon ? html`<ha-icon style="width: ${config.size}; height: ${config.size};" icon="${config.icon}"></ha-icon>` : ''}
71+
${config.name ? html`<span>${config.name}</span>` : ''}
72+
${config.show_state ? html`<span>${state.state}</span>` : ''}
73+
</div>
3574
</paper-button>
3675
</ha-card>
3776
`;
3877
}
3978

40-
icon(state, config) {
41-
const color = state.attributes.rgb_color ? `rgb(${state.attributes.rgb_color.join(',')})` : (
42-
config.color ? config.color : "var(--primary-text-color)"
43-
)
44-
const color_on = state.state === 'on' ? color : "var(--disabled-text-color)";
45-
let card_style = '';
46-
if (config.style){
47-
config.style.forEach( cssObject => {
48-
const attribute = Object.keys(cssObject)[0]
49-
const value = cssObject[attribute]
50-
card_style += `${attribute}: ${value};\n`
51-
})
52-
}
53-
79+
icon_colored_html(state, config) {
80+
const color = this.build_css_color_attribute(state, config);
5481
return html`
5582
<style>
5683
ha-icon {
57-
display: block;
84+
display: flex;
5885
margin: auto;
5986
}
6087
paper-button {
61-
display: block;
88+
display: flex;
6289
margin: auto;
6390
text-align: center;
6491
}
6592
</style>
66-
<ha-card>
67-
<paper-button style="${card_style != '' ? card_style : ""}" on-tap="${ev => this._toggle(state, config)}">
68-
<ha-icon style="color: ${color_on}; width: ${config.size ? config.size : "40%"}; height: ${config.size ? config.size : "40%"};" icon="${config.icon}"></ha-icon>
69-
${config.name ? config.name : ""}
93+
<ha-card on-tap="${ev => this._toggle(state, config)}">
94+
<paper-button style="${config.card_style}">
95+
<div>
96+
${config.icon ? html`<ha-icon style="color: ${color}; width: ${config.size}; height: ${config.size};" icon="${config.icon}"></ha-icon>` : ''}
97+
${config.name ? html`<span>${config.name}</span>` : ''}
98+
${config.show_state ? html`<span>${state.state}</span>` : ''}
99+
</div>
70100
</paper-button>
71101
</ha-card>
72102
`;
@@ -77,6 +107,21 @@ class ButtonCard extends LitElement {
77107
throw new Error('You need to define entity');
78108
}
79109
this.config = config;
110+
this.config.color = config.color ? config.color : "var(--primary-text-color)";
111+
this.config.size = config.size ? config.size : "40%";
112+
let card_style = '';
113+
if (config.style) {
114+
config.style.forEach(cssObject => {
115+
const attribute = Object.keys(cssObject)[0]
116+
const value = cssObject[attribute]
117+
card_style += `${attribute}: ${value};\n`
118+
})
119+
}
120+
this.config.color_type = config.color_type ? config.color_type : "icon";
121+
this.config.color_off = config.color_off ? config.color_off : "var(--disabled-text-color)";
122+
this.config.default_color = config.default_color ? config.default_color : "var(--primary-text-color)";
123+
this.config.card_style = card_style;
124+
this.config.name = config.name ? config.name : "";
80125
}
81126

82127
// The height of your card. Home Assistant uses this to automatically

changelog.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## 0.0.1
2+
Initial release that supports versioning
3+
4+
### New features :
5+
6+
- Color on off state
7+
- Default color on light (when color set to `auto` and detection fails)
8+
- Background color card
9+
- Boolean to show/hide state
10+
- Automatic font color based on card color (for readability)
11+
12+
### Changed features :
13+
14+
- Automatic color mode for lights now has to be enabled via `color: auto`
15+
- State display has to be enabled explicity via `show_state: true` and is not enabled by default when an icon is not set
16+
17+
### Other
18+
19+
- Lots of refactoring
20+
- Versionning
21+

color.gif

1.11 MB
Loading

0 commit comments

Comments
 (0)