First commit

This commit is contained in:
Alexandre Garcia 2018-08-26 11:52:54 -04:00
commit 8ac16fae35
5 changed files with 133 additions and 0 deletions

9
LICENSE Normal file
View File

@ -0,0 +1,9 @@
The MIT License (MIT)
Copyright (c) 2018, Alexandre Garcia
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

44
README.md Normal file
View File

@ -0,0 +1,44 @@
# Button card
Button card is a simple button card to toggle your entities.
The card toggles on click/tap.
![icon-button-card](icon_button.png)
![no-icon-button-card](no_icon.png)
## Options
| Name | Type | Default | Description
| ---- | ---- | ------- | -----------
| type | string | **Required** | `custom:button-card`
| entity | string | **Required** | `switch.ac`
| icon | string | optional | Icon to display in place of the state e.g. `mdi:air-conditioner`
| color | string | `var(--primary-text-color)` | Color of the icon when state is `on`. Can be any html color e.g. `rgb(28, 128, 199)`
| size | string | `40%` | Size of the icon. Can be percentage or pixel
## Examples
Import the card in `ui-lovelace.yaml`
```yaml
title: Home
resources:
- url: /local/button-card.js
type: module
```
Show a button for the air conditioner (blue when on):
```yaml
- type: "custom:button-card"
entity: switch.ac
icon: mdi:air-conditioner
color: rgb(28, 128, 199)
```
Show an ON/OFF button for the home lights:
```yaml
- type: "custom:button-card"
entity: group.home_lights
```

80
button-card.js Normal file
View File

@ -0,0 +1,80 @@
import {
LitElement, html
} from 'https://unpkg.com/@polymer/lit-element@^0.5.2/lit-element.js?module';
class ButtonCard extends LitElement {
static get properties() {
return {
hass: Object,
config: Object,
}
}
_render({ hass, config }) {
let state = hass.states[config.entity];
if (config.icon){
return this.icon(state,config);
}
return this.no_icon(state);
}
no_icon(state) {
return html`
<style>
paper-button {
display: block;
margin: auto;
text-align: center;
}
</style>
<ha-card>
<paper-button on-tap="${ev => this._toggle(state)}">
${state.state}
</paper-button>
</ha-card>
`;
}
icon(state,config) {
return html`
<style>
ha-icon {
width: ${config.size ? config.size : "40%" };
height: ${config.size ? config.size : "40%" };
display: block;
margin: auto;
color: ${state.state === 'on' ? (config.color ? config.color : "var(--primary-text-color)") : "var(--disabled-text-color)"};
}
</style>
<ha-card>
<paper-button state="${state.state}" on-tap="${ev => this._toggle(state)}">
<ha-icon icon="${config.icon}"></ha-icon>
</paper-button>
</ha-card>
`;
}
setConfig(config) {
if (!config.entity) {
throw new Error('You need to define entity');
}
this.config = config;
}
// The height of your card. Home Assistant uses this to automatically
// distribute all cards over the available columns.
getCardSize() {
return 3;
}
_toggle(state) {
this.hass.callService('homeassistant', 'toggle', {
entity_id: state.entity_id
});
}
}
customElements.define('button-card', ButtonCard);

BIN
icon_button.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

BIN
no_icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 984 B