Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/data/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,15 @@ export interface DelayAction {
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;
scene: string;
}
export type SceneAction = ServiceSceneAction | LegacySceneAction;

export interface WaitAction {
alias?: string;
Expand Down Expand Up @@ -153,7 +158,8 @@ export interface ActionTypes {
check_condition: Condition;
fire_event: EventAction;
device_action: DeviceAction;
activate_scene: SceneAction;
legacy_activate_scene: LegacySceneAction;
activate_scene: ServiceSceneAction;
repeat: RepeatAction;
choose: ChooseAction;
wait_for_trigger: WaitForTriggerAction;
Expand Down Expand Up @@ -218,7 +224,7 @@ export const getActionType = (action: Action): ActionType => {
return "device_action";
}
if ("scene" in action) {
return "activate_scene";
return "legacy_activate_scene";
}
if ("repeat" in action) {
return "repeat";
Expand All @@ -233,6 +239,14 @@ export const getActionType = (action: Action): ActionType => {
return "variables";
}
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 "unknown";
Expand Down
15 changes: 12 additions & 3 deletions src/data/script_i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
DelayAction,
EventAction,
getActionType,
SceneAction,
LegacySceneAction,
ServiceSceneAction,
VariablesAction,
WaitForTriggerAction,
} from "./script";
Expand Down Expand Up @@ -102,14 +103,22 @@ export const describeAction = <T extends ActionType>(
return `Delay ${duration}`;
}

if (actionType === "activate_scene") {
const config = action as SceneAction;
if (actionType === "legacy_activate_scene") {
const config = action as LegacySceneAction;
const sceneStateObj = hass.states[config.scene];
return `Activate 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") {
const config = action as WaitForTriggerAction;
return `Wait for ${ensureArray(config.wait_for_trigger)
Expand Down
26 changes: 23 additions & 3 deletions src/panels/config/automation/action/ha-automation-action-row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import "../../../../components/ha-card";
import "../../../../components/ha-alert";
import "../../../../components/ha-icon-button";
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 { haStyle } from "../../../../resources/styles";
import type { HomeAssistant } from "../../../../types";
Expand Down Expand Up @@ -44,8 +44,28 @@ const OPTIONS = [
"device_id",
];

const getType = (action: Action | undefined) =>
action ? OPTIONS.find((option) => option in action) : undefined;
const getType = (action: Action | undefined) => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we re-use getActionType ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we would have to change all the element names 😅 but yes, I do want to do that, found it a bit late though

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aaah ok . yeah not this pr.

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 {
// for fire event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,23 @@ export class HaSceneAction extends LitElement implements ActionElement {
@property() public action!: SceneAction;

public static get defaultConfig(): SceneAction {
return { scene: "" };
return {
service: "scene.turn_on",
target: {
entity_id: "",
},
metadata: {},
};
}

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`
<ha-entity-picker
Expand All @@ -36,7 +48,13 @@ export class HaSceneAction extends LitElement implements ActionElement {
private _entityPicked(ev: PolymerChangedEvent<string>) {
ev.stopPropagation();
fireEvent(this, "value-changed", {
value: { ...this.action, scene: ev.detail.value },
value: {
service: "scene.turn_on",
target: {
entity_id: ev.detail.value,
},
metadata: {},
},
});
}
}
Expand Down