diff --git a/README.md b/README.md index 4905390..21fd5c6 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![GitHub Release][releases-shield]][releases] [![hacs_badge](https://img.shields.io/badge/HACS-default-orange.svg?style=for-the-badge)](https://github.com/custom-components/hacs) -A button card with integrated slider for `light, switch, fan, cover, input_boolean, media_player, climate, lock` entities. +A button card with integrated slider for `automation, light, switch, fan, cover, input_boolean, media_player, climate, lock` entities. ![Preview][preview] ![Preview 2][preview-2] @@ -71,7 +71,7 @@ Slider Button Card supports Lovelace's Visual Editor. | Name | Type | Requirement | Description | Default | | ----------------- | ------- | ------------ | ------------------------------------------- | ------------------- | | type | string | **Required** | `custom:slider-button-card` | -| entity | string | **Required** | HA entity ID from domain `light, switch, fan, cover, input_boolean, media_player, climate, lock` | | +| entity | string | **Required** | HA entity ID from domain `automation, light, switch, fan, cover, input_boolean, media_player, climate, lock` | | | name | string | **Optional** | Name | `entity.friendly_name` | | show_attribute | boolean | **Optional** | Show attribute | `false` (except for `media_player` entities) | | show_name | boolean | **Optional** | Show name | `true` | diff --git a/src/controllers/automation-controller.ts b/src/controllers/automation-controller.ts new file mode 100644 index 0000000..bc7b3a5 --- /dev/null +++ b/src/controllers/automation-controller.ts @@ -0,0 +1,35 @@ +import { STATES_OFF } from 'custom-card-helpers'; +import { Controller } from './controller'; + +export class AutomationController extends Controller { + _min = 0; + _max = 1; + _targetValue; + _invert = false; + + get _value(): number { + return !STATES_OFF.includes(this.stateObj.state) + ? 1 + : 0; + } + + set _value(value) { + const service = value > 0 ? 'turn_on' : 'turn_off'; + this._hass.callService('automation', service, { + // eslint-disable-next-line @typescript-eslint/camelcase + entity_id: this.stateObj.entity_id + }); + } + + get _step(): number { + return 1; + } + + get label(): string { + if (this.percentage > 0) { + return this._hass.localize('component.automation.state._.on'); + } + return this._hass.localize('component.automation.state._.off'); + } + +} diff --git a/src/controllers/get-controller.ts b/src/controllers/get-controller.ts index b482775..0c8d107 100644 --- a/src/controllers/get-controller.ts +++ b/src/controllers/get-controller.ts @@ -1,5 +1,6 @@ import { computeDomain } from 'custom-card-helpers'; import { Domain, SliderButtonCardConfig } from '../types'; +import { AutomationController } from './automation-controller'; import { ClimateController } from './climate-controller'; import { Controller } from './controller'; import { CoverController } from './cover-controller'; @@ -17,6 +18,7 @@ export class ControllerFactory { [Domain.LIGHT]: LightController, [Domain.FAN]: FanController, [Domain.SWITCH]: SwitchController, + [Domain.AUTOMATION]: AutomationController, [Domain.COVER]: CoverController, [Domain.INPUT_BOOLEAN]: InputBooleanController, [Domain.MEDIA_PLAYER]: MediaController, diff --git a/src/types.ts b/src/types.ts index 5c65adb..600cc6d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -83,6 +83,7 @@ export enum Domain { MEDIA_PLAYER = 'media_player', CLIMATE = 'climate', LOCK = 'lock', + AUTOMATION = 'automation', } export const ActionButtonConfigDefault: ActionButtonConfig = { @@ -144,6 +145,15 @@ export const SliderConfigDefaultDomain: Map = new Map([ force_square: false, show_attribute: false, }], + [Domain.AUTOMATION, { + direction: SliderDirections.LEFT_RIGHT, + background: SliderBackground.SOLID, + use_state_color: false, + use_percentage_bg_opacity: false, + show_track: false, + toggle_on_click: true, + force_square: false, + }], [Domain.COVER, { direction: SliderDirections.TOP_BOTTOM, background: SliderBackground.STRIPED,