Convert scene action to service call (#11705)

* Convert scene action to service call

* fix describeAction

* rename to metadata

* Update script.ts
This commit is contained in:
Bram Kragten 2022-02-16 20:47:21 +01:00 committed by GitHub
parent f43655eea5
commit 89f4fe9d20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 12 deletions

View File

@ -70,10 +70,15 @@ export interface DelayAction {
delay: number | Partial<DelayActionParts> | string; delay: number | Partial<DelayActionParts> | string;
} }
export interface SceneAction { export interface ServiceSceneAction extends ServiceAction {
service: "scene.turn_on";
metadata: Record<string, any>;
}
export interface LegacySceneAction {
alias?: string; alias?: string;
scene: string; scene: string;
} }
export type SceneAction = ServiceSceneAction | LegacySceneAction;
export interface WaitAction { export interface WaitAction {
alias?: string; alias?: string;
@ -153,7 +158,8 @@ export interface ActionTypes {
check_condition: Condition; check_condition: Condition;
fire_event: EventAction; fire_event: EventAction;
device_action: DeviceAction; device_action: DeviceAction;
activate_scene: SceneAction; legacy_activate_scene: LegacySceneAction;
activate_scene: ServiceSceneAction;
repeat: RepeatAction; repeat: RepeatAction;
choose: ChooseAction; choose: ChooseAction;
wait_for_trigger: WaitForTriggerAction; wait_for_trigger: WaitForTriggerAction;
@ -218,7 +224,7 @@ export const getActionType = (action: Action): ActionType => {
return "device_action"; return "device_action";
} }
if ("scene" in action) { if ("scene" in action) {
return "activate_scene"; return "legacy_activate_scene";
} }
if ("repeat" in action) { if ("repeat" in action) {
return "repeat"; return "repeat";
@ -233,6 +239,14 @@ export const getActionType = (action: Action): ActionType => {
return "variables"; return "variables";
} }
if ("service" in action) { if ("service" in action) {
if ("metadata" in action) {
if (
(action as ServiceAction).service === "scene.turn_on" &&
!Array.isArray((action as ServiceAction)?.target?.entity_id)
) {
return "activate_scene";
}
}
return "service"; return "service";
} }
return "unknown"; return "unknown";

View File

@ -11,7 +11,8 @@ import {
DelayAction, DelayAction,
EventAction, EventAction,
getActionType, getActionType,
SceneAction, LegacySceneAction,
ServiceSceneAction,
VariablesAction, VariablesAction,
WaitForTriggerAction, WaitForTriggerAction,
} from "./script"; } from "./script";
@ -102,14 +103,22 @@ export const describeAction = <T extends ActionType>(
return `Delay ${duration}`; return `Delay ${duration}`;
} }
if (actionType === "activate_scene") { if (actionType === "legacy_activate_scene") {
const config = action as SceneAction; const config = action as LegacySceneAction;
const sceneStateObj = hass.states[config.scene]; const sceneStateObj = hass.states[config.scene];
return `Activate scene ${ return `Activate scene ${
sceneStateObj ? computeStateName(sceneStateObj) : config.scene sceneStateObj ? computeStateName(sceneStateObj) : config.scene
}`; }`;
} }
if (actionType === "activate_scene") {
const config = action as ServiceSceneAction;
const sceneStateObj = hass.states[config.target!.entity_id as string];
return `Activate scene ${
sceneStateObj ? computeStateName(sceneStateObj) : config.target!.entity_id
}`;
}
if (actionType === "wait_for_trigger") { if (actionType === "wait_for_trigger") {
const config = action as WaitForTriggerAction; const config = action as WaitForTriggerAction;
return `Wait for ${ensureArray(config.wait_for_trigger) return `Wait for ${ensureArray(config.wait_for_trigger)

View File

@ -16,7 +16,7 @@ import "../../../../components/ha-card";
import "../../../../components/ha-alert"; import "../../../../components/ha-alert";
import "../../../../components/ha-icon-button"; import "../../../../components/ha-icon-button";
import type { HaYamlEditor } from "../../../../components/ha-yaml-editor"; import type { HaYamlEditor } from "../../../../components/ha-yaml-editor";
import type { Action } from "../../../../data/script"; import type { Action, ServiceSceneAction } from "../../../../data/script";
import { showConfirmationDialog } from "../../../../dialogs/generic/show-dialog-box"; import { showConfirmationDialog } from "../../../../dialogs/generic/show-dialog-box";
import { haStyle } from "../../../../resources/styles"; import { haStyle } from "../../../../resources/styles";
import type { HomeAssistant } from "../../../../types"; import type { HomeAssistant } from "../../../../types";
@ -44,8 +44,28 @@ const OPTIONS = [
"device_id", "device_id",
]; ];
const getType = (action: Action | undefined) => const getType = (action: Action | undefined) => {
action ? OPTIONS.find((option) => option in action) : undefined; if (!action) {
return undefined;
}
if ("metadata" in action && action.service) {
switch (action.service) {
case "scene.turn_on":
// we dont support arrays of entities
if (
!Array.isArray(
(action as unknown as ServiceSceneAction).target?.entity_id
)
) {
return "scene";
}
break;
default:
break;
}
}
return OPTIONS.find((option) => option in action);
};
declare global { declare global {
// for fire event // for fire event

View File

@ -16,11 +16,23 @@ export class HaSceneAction extends LitElement implements ActionElement {
@property() public action!: SceneAction; @property() public action!: SceneAction;
public static get defaultConfig(): SceneAction { public static get defaultConfig(): SceneAction {
return { scene: "" }; return {
service: "scene.turn_on",
target: {
entity_id: "",
},
metadata: {},
};
} }
protected render() { protected render() {
const { scene } = this.action; let scene;
if ("scene" in this.action) {
scene = this.action.scene;
} else {
scene = this.action.target?.entity_id;
}
return html` return html`
<ha-entity-picker <ha-entity-picker
@ -36,7 +48,13 @@ export class HaSceneAction extends LitElement implements ActionElement {
private _entityPicked(ev: PolymerChangedEvent<string>) { private _entityPicked(ev: PolymerChangedEvent<string>) {
ev.stopPropagation(); ev.stopPropagation();
fireEvent(this, "value-changed", { fireEvent(this, "value-changed", {
value: { ...this.action, scene: ev.detail.value }, value: {
service: "scene.turn_on",
target: {
entity_id: ev.detail.value,
},
metadata: {},
},
}); });
} }
} }