commit 8ac16fae35d1ad585ed606653e08693d8e52122a Author: Alexandre Garcia Date: Sun Aug 26 11:52:54 2018 -0400 First commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..68ee7f2 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..64c309c --- /dev/null +++ b/README.md @@ -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 +``` diff --git a/button-card.js b/button-card.js new file mode 100644 index 0000000..c443002 --- /dev/null +++ b/button-card.js @@ -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` + + + + ${state.state} + + + `; + } + + icon(state,config) { + return html` + + + + + + + + `; + } + + 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); \ No newline at end of file diff --git a/icon_button.png b/icon_button.png new file mode 100644 index 0000000..7547760 Binary files /dev/null and b/icon_button.png differ diff --git a/no_icon.png b/no_icon.png new file mode 100644 index 0000000..3b9a5cb Binary files /dev/null and b/no_icon.png differ