Skip to content

Commit 8c57bf8

Browse files
committed
initial script domain support
1 parent 7f3e715 commit 8c57bf8

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

dist/slider-button-card.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3951,6 +3951,7 @@ var Domain;
39513951
Domain["AUTOMATION"] = "automation";
39523952
Domain["SENSOR"] = "sensor";
39533953
Domain["BINARY_SENSOR"] = "binary_sensor";
3954+
Domain["SCRIPT"] = "script";
39543955
})(Domain || (Domain = {}));
39553956
const ActionButtonConfigDefault = {
39563957
mode: ActionButtonMode.TOGGLE,
@@ -4137,6 +4138,18 @@ const SliderConfigDefaultDomain = new Map([
41374138
action: 'more-info'
41384139
},
41394140
}],
4141+
[Domain.SCRIPT, {
4142+
direction: SliderDirections.LEFT_RIGHT,
4143+
background: SliderBackground.SOLID,
4144+
use_state_color: false,
4145+
use_percentage_bg_opacity: false,
4146+
show_track: false,
4147+
disable_sliding: true,
4148+
force_square: false,
4149+
tap_action: {
4150+
action: 'more-info'
4151+
},
4152+
}],
41404153
]);
41414154
var LightAttributes;
41424155
(function (LightAttributes) {
@@ -6355,6 +6368,36 @@ class BinarySensorController extends Controller {
63556368
}
63566369
}
63576370

6371+
class ScriptController extends Controller {
6372+
constructor() {
6373+
super(...arguments);
6374+
this._min = 0;
6375+
this._max = 1;
6376+
this._invert = false;
6377+
}
6378+
get _value() {
6379+
return !D.includes(this.stateObj.state)
6380+
? 1
6381+
: 0;
6382+
}
6383+
set _value(value) {
6384+
const service = value > 0 ? 'turn_on' : 'turn_off';
6385+
this._hass.callService('script', service, {
6386+
// eslint-disable-next-line @typescript-eslint/camelcase
6387+
entity_id: this.stateObj.entity_id
6388+
});
6389+
}
6390+
get _step() {
6391+
return 1;
6392+
}
6393+
get label() {
6394+
if (this.percentage > 0) {
6395+
return this._hass.localize('component.script.state._.on');
6396+
}
6397+
return this._hass.localize('component.script.state._.off');
6398+
}
6399+
}
6400+
63586401
class ControllerFactory {
63596402
static getInstance(config) {
63606403
const domain = f(config.entity);
@@ -6371,6 +6414,7 @@ class ControllerFactory {
63716414
[Domain.LOCK]: LockController,
63726415
[Domain.SENSOR]: SensorController,
63736416
[Domain.BINARY_SENSOR]: BinarySensorController,
6417+
[Domain.SCRIPT]: ScriptController,
63746418
};
63756419
if (typeof mapping[domain] === 'undefined') {
63766420
throw new Error(`Unsupported entity type: ${domain}`);

src/controllers/get-controller.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { MediaController } from './media-controller';
1313
import { SwitchController } from './switch-controller';
1414
import { SensorController } from './sensor-controller';
1515
import { BinarySensorController } from './binary-sensor-controller';
16+
import { ScriptController } from './script-controller';
1617

1718
export class ControllerFactory {
1819
static getInstance(config: SliderButtonCardConfig): Controller {
@@ -30,6 +31,7 @@ export class ControllerFactory {
3031
[Domain.LOCK]: LockController,
3132
[Domain.SENSOR]: SensorController,
3233
[Domain.BINARY_SENSOR]: BinarySensorController,
34+
[Domain.SCRIPT]: ScriptController,
3335
};
3436
if (typeof mapping[domain] === 'undefined') {
3537
throw new Error(`Unsupported entity type: ${domain}`)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { STATES_OFF } from 'custom-card-helpers';
2+
import { Controller } from './controller';
3+
4+
export class ScriptController extends Controller {
5+
_min = 0;
6+
_max = 1;
7+
_targetValue;
8+
_invert = false;
9+
_clickPosition;
10+
_clickPositionLock;
11+
_originalValue;
12+
_originalValueLock;
13+
14+
get _value(): number {
15+
return !STATES_OFF.includes(this.stateObj.state)
16+
? 1
17+
: 0;
18+
}
19+
20+
set _value(value) {
21+
const service = value > 0 ? 'turn_on' : 'turn_off';
22+
this._hass.callService('script', service, {
23+
// eslint-disable-next-line @typescript-eslint/camelcase
24+
entity_id: this.stateObj.entity_id
25+
});
26+
}
27+
28+
get _step(): number {
29+
return 1;
30+
}
31+
32+
get label(): string {
33+
if (this.percentage > 0) {
34+
return this._hass.localize('component.script.state._.on');
35+
}
36+
return this._hass.localize('component.script.state._.off');
37+
}
38+
39+
}

src/types.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export enum Domain {
8787
AUTOMATION = 'automation',
8888
SENSOR = 'sensor',
8989
BINARY_SENSOR = 'binary_sensor',
90+
SCRIPT = 'script',
9091
}
9192

9293
export const ActionButtonConfigDefault: ActionButtonConfig = {
@@ -277,6 +278,18 @@ export const SliderConfigDefaultDomain: Map<string, SliderConfig> = new Map([
277278
action: 'more-info'
278279
},
279280
}],
281+
[Domain.SCRIPT, {
282+
direction: SliderDirections.LEFT_RIGHT,
283+
background: SliderBackground.SOLID,
284+
use_state_color: false,
285+
use_percentage_bg_opacity: false,
286+
show_track: false,
287+
disable_sliding: true,
288+
force_square: false,
289+
tap_action: {
290+
action: 'more-info'
291+
},
292+
}],
280293
]);
281294

282295
export enum LightAttributes {

0 commit comments

Comments
 (0)