mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-28 03:36:44 +00:00
Add device actions to automation editor. (#3637)
* Add device actions to automation editor. * Copy automation on selection * Fix types * Remove device from device action schema
This commit is contained in:
parent
f2999c30f3
commit
5448cbf1c5
32
src/components/device/ha-device-action-picker.ts
Normal file
32
src/components/device/ha-device-action-picker.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { customElement } from "lit-element";
|
||||
import {
|
||||
DeviceAction,
|
||||
fetchDeviceActions,
|
||||
localizeDeviceAutomationAction,
|
||||
} from "../../data/device_automation";
|
||||
import "../../components/ha-paper-dropdown-menu";
|
||||
import { HaDeviceAutomationPicker } from "./ha-device-automation-picker";
|
||||
|
||||
@customElement("ha-device-action-picker")
|
||||
class HaDeviceActionPicker extends HaDeviceAutomationPicker<DeviceAction> {
|
||||
protected NO_AUTOMATION_TEXT = "No actions";
|
||||
protected UNKNOWN_AUTOMATION_TEXT = "Unknown action";
|
||||
|
||||
constructor() {
|
||||
super(
|
||||
localizeDeviceAutomationAction,
|
||||
fetchDeviceActions,
|
||||
(deviceId?: string) => ({
|
||||
device_id: deviceId || "",
|
||||
domain: "",
|
||||
entity_id: "",
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface HTMLElementTagNameMap {
|
||||
"ha-device-action-picker": HaDeviceActionPicker;
|
||||
}
|
||||
}
|
@ -10,6 +10,9 @@ export interface DeviceAutomation {
|
||||
event?: string;
|
||||
}
|
||||
|
||||
// tslint:disable-next-line: no-empty-interface
|
||||
export interface DeviceAction extends DeviceAutomation {}
|
||||
|
||||
export interface DeviceCondition extends DeviceAutomation {
|
||||
condition: string;
|
||||
}
|
||||
@ -18,6 +21,12 @@ export interface DeviceTrigger extends DeviceAutomation {
|
||||
platform: string;
|
||||
}
|
||||
|
||||
export const fetchDeviceActions = (hass: HomeAssistant, deviceId: string) =>
|
||||
hass.callWS<DeviceAction[]>({
|
||||
type: "device_automation/action/list",
|
||||
device_id: deviceId,
|
||||
});
|
||||
|
||||
export const fetchDeviceConditions = (hass: HomeAssistant, deviceId: string) =>
|
||||
hass.callWS<DeviceCondition[]>({
|
||||
type: "device_automation/condition/list",
|
||||
@ -52,6 +61,24 @@ export const deviceAutomationsEqual = (
|
||||
return true;
|
||||
};
|
||||
|
||||
export const localizeDeviceAutomationAction = (
|
||||
hass: HomeAssistant,
|
||||
action: DeviceAction
|
||||
) => {
|
||||
const state = action.entity_id ? hass.states[action.entity_id] : undefined;
|
||||
return hass.localize(
|
||||
`component.${action.domain}.device_automation.action_type.${action.type}`,
|
||||
"entity_name",
|
||||
state ? compute_state_name(state) : "<unknown>",
|
||||
"subtype",
|
||||
hass.localize(
|
||||
`component.${action.domain}.device_automation.action_subtype.${
|
||||
action.subtype
|
||||
}`
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
export const localizeDeviceAutomationCondition = (
|
||||
hass: HomeAssistant,
|
||||
condition: DeviceCondition
|
||||
|
@ -7,6 +7,12 @@ export interface EventAction {
|
||||
event_data_template?: { [key: string]: any };
|
||||
}
|
||||
|
||||
export interface DeviceAction {
|
||||
device_id: string;
|
||||
domain: string;
|
||||
entity_id: string;
|
||||
}
|
||||
|
||||
export const triggerScript = (
|
||||
hass: HomeAssistant,
|
||||
entityId: string,
|
||||
|
@ -23,6 +23,7 @@ declare global {
|
||||
"ha-service-picker": any;
|
||||
"mwc-button": any;
|
||||
"ha-device-trigger-picker": any;
|
||||
"ha-device-action-picker": any;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import "@polymer/paper-item/paper-item";
|
||||
import CallServiceAction from "./call_service";
|
||||
import ConditionAction from "./condition";
|
||||
import DelayAction from "./delay";
|
||||
import DeviceAction from "./device";
|
||||
import EventAction from "./event";
|
||||
import WaitAction from "./wait";
|
||||
|
||||
@ -15,6 +16,7 @@ const TYPES = {
|
||||
wait_template: WaitAction,
|
||||
condition: ConditionAction,
|
||||
event: EventAction,
|
||||
device_id: DeviceAction,
|
||||
};
|
||||
|
||||
const OPTIONS = Object.keys(TYPES).sort();
|
||||
|
63
src/panels/config/js/script/device.tsx
Normal file
63
src/panels/config/js/script/device.tsx
Normal file
@ -0,0 +1,63 @@
|
||||
import { h, Component } from "preact";
|
||||
|
||||
import "../../../../components/device/ha-device-picker";
|
||||
import "../../../../components/device/ha-device-action-picker";
|
||||
import { HomeAssistant } from "../../../../types";
|
||||
import { DeviceAction } from "../../../../data/script";
|
||||
|
||||
export default class DeviceActionEditor extends Component<
|
||||
{
|
||||
index: number;
|
||||
action: DeviceAction;
|
||||
hass: HomeAssistant;
|
||||
onChange(index: number, action: DeviceAction);
|
||||
},
|
||||
{
|
||||
device_id: string | undefined;
|
||||
}
|
||||
> {
|
||||
public static defaultConfig: DeviceAction = {
|
||||
device_id: "",
|
||||
domain: "",
|
||||
entity_id: "",
|
||||
};
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.devicePicked = this.devicePicked.bind(this);
|
||||
this.deviceActionPicked = this.deviceActionPicked.bind(this);
|
||||
this.state = { device_id: undefined };
|
||||
}
|
||||
|
||||
public render() {
|
||||
const { action, hass } = this.props;
|
||||
const deviceId = this.state.device_id || action.device_id;
|
||||
|
||||
return (
|
||||
<div>
|
||||
<ha-device-picker
|
||||
value={deviceId}
|
||||
onChange={this.devicePicked}
|
||||
hass={hass}
|
||||
label="Device"
|
||||
/>
|
||||
<ha-device-action-picker
|
||||
value={action}
|
||||
deviceId={deviceId}
|
||||
onChange={this.deviceActionPicked}
|
||||
hass={hass}
|
||||
label="Action"
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
private devicePicked(ev) {
|
||||
this.setState({ device_id: ev.target.value });
|
||||
}
|
||||
|
||||
private deviceActionPicked(ev) {
|
||||
const deviceAction = { ...ev.target.value };
|
||||
this.props.onChange(this.props.index, deviceAction);
|
||||
}
|
||||
}
|
@ -2,7 +2,6 @@ import { h, Component } from "preact";
|
||||
|
||||
import "../../../../components/device/ha-device-picker";
|
||||
import "../../../../components/device/ha-device-trigger-picker";
|
||||
import "../../../../components/device/ha-device-automation-picker";
|
||||
|
||||
export default class DeviceTrigger extends Component<any, any> {
|
||||
constructor() {
|
||||
|
@ -871,6 +871,9 @@
|
||||
"label": "Fire event",
|
||||
"event": "[%key:ui::panel::config::automation::editor::triggers::type::homeassistant::event%]",
|
||||
"service_data": "[%key:ui::panel::config::automation::editor::actions::type::service::service_data%]"
|
||||
},
|
||||
"device_id": {
|
||||
"label": "Device"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user