Add yaml editor to automation conditions (#4305)

This commit is contained in:
Bram Kragten 2019-12-02 12:02:35 +01:00 committed by GitHub
parent 7b2be54f8f
commit 94c120cdb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 102 additions and 41 deletions

View File

@ -3,6 +3,8 @@ import "@polymer/paper-dropdown-menu/paper-dropdown-menu-light";
import "@polymer/paper-listbox/paper-listbox"; import "@polymer/paper-listbox/paper-listbox";
import "@polymer/paper-item/paper-item"; import "@polymer/paper-item/paper-item";
import YAMLTextArea from "../yaml_textarea";
import DeviceCondition from "./device"; import DeviceCondition from "./device";
import LogicalCondition from "./logical"; import LogicalCondition from "./logical";
import NumericStateCondition from "./numeric_state"; import NumericStateCondition from "./numeric_state";
@ -31,6 +33,7 @@ export default class ConditionEdit extends Component<any> {
super(); super();
this.typeChanged = this.typeChanged.bind(this); this.typeChanged = this.typeChanged.bind(this);
this.onYamlChange = this.onYamlChange.bind(this);
} }
public typeChanged(ev) { public typeChanged(ev) {
@ -44,20 +47,24 @@ export default class ConditionEdit extends Component<any> {
} }
} }
public render({ index, condition, onChange, hass, localize }) { public render({ index, condition, onChange, hass, localize, yamlMode }) {
// tslint:disable-next-line: variable-name // tslint:disable-next-line: variable-name
const Comp = TYPES[condition.condition]; const Comp = TYPES[condition.condition];
const selected = OPTIONS.indexOf(condition.condition); const selected = OPTIONS.indexOf(condition.condition);
if (!Comp) { if (yamlMode || !Comp) {
return ( return (
<div style="margin-right: 24px;">
{!Comp && (
<div> <div>
{localize( {localize(
"ui.panel.config.automation.editor.conditions.unsupported_condition", "ui.panel.config.automation.editor.conditions.unsupported_condition",
"condition", "condition",
condition.condition condition.condition
)} )}
<pre>{JSON.stringify(condition, null, 2)}</pre> </div>
)}
<YAMLTextArea value={condition} onChange={this.onYamlChange} />
</div> </div>
); );
} }
@ -94,4 +101,8 @@ export default class ConditionEdit extends Component<any> {
</div> </div>
); );
} }
private onYamlChange(condition) {
this.props.onChange(this.props.index, condition);
}
} }

View File

@ -8,10 +8,16 @@ import "../../../../components/ha-card";
import ConditionEdit from "./condition_edit"; import ConditionEdit from "./condition_edit";
export default class ConditionRow extends Component<any> { export default class ConditionRow extends Component<any> {
public state: { yamlMode: boolean };
constructor() { constructor() {
super(); super();
this.state = {
yamlMode: false,
};
this.onDelete = this.onDelete.bind(this); this.onDelete = this.onDelete.bind(this);
this.switchYamlMode = this.switchYamlMode.bind(this);
} }
public onDelete() { public onDelete() {
@ -27,22 +33,32 @@ export default class ConditionRow extends Component<any> {
} }
} }
public render(props) { public render(props, { yamlMode }) {
return ( return (
<ha-card> <ha-card>
<div class="card-content"> <div class="card-content">
<div class="card-menu"> <div class="card-menu" style="z-index: 3">
<paper-menu-button <paper-menu-button
no-animations no-animations
horizontal-align="right" horizontal-align="right"
horizontal-offset="-5" horizontal-offset="-5"
vertical-offset="-5" vertical-offset="-5"
close-on-activate
> >
<paper-icon-button <paper-icon-button
icon="hass:dots-vertical" icon="hass:dots-vertical"
slot="dropdown-trigger" slot="dropdown-trigger"
/> />
<paper-listbox slot="dropdown-content"> <paper-listbox slot="dropdown-content">
<paper-item onTap={this.switchYamlMode}>
{yamlMode
? props.localize(
"ui.panel.config.automation.editor.edit_ui"
)
: props.localize(
"ui.panel.config.automation.editor.edit_yaml"
)}
</paper-item>
<paper-item disabled> <paper-item disabled>
{props.localize( {props.localize(
"ui.panel.config.automation.editor.conditions.duplicate" "ui.panel.config.automation.editor.conditions.duplicate"
@ -56,9 +72,15 @@ export default class ConditionRow extends Component<any> {
</paper-listbox> </paper-listbox>
</paper-menu-button> </paper-menu-button>
</div> </div>
<ConditionEdit {...props} /> <ConditionEdit {...props} yamlMode={yamlMode} />
</div> </div>
</ha-card> </ha-card>
); );
} }
private switchYamlMode() {
this.setState({
yamlMode: !this.state.yamlMode,
});
}
} }

View File

@ -1,4 +1,4 @@
import { h, Component } from "preact"; import { h } from "preact";
import "../../../../components/device/ha-device-picker"; import "../../../../components/device/ha-device-picker";
import "../../../../components/device/ha-device-condition-picker"; import "../../../../components/device/ha-device-condition-picker";
@ -8,7 +8,10 @@ import {
fetchDeviceConditionCapabilities, fetchDeviceConditionCapabilities,
deviceAutomationsEqual, deviceAutomationsEqual,
} from "../../../../data/device_automation"; } from "../../../../data/device_automation";
export default class DeviceCondition extends Component<any, any> {
import { AutomationComponent } from "../automation-component";
export default class DeviceCondition extends AutomationComponent {
private _origCondition; private _origCondition;
constructor() { constructor() {
@ -20,10 +23,16 @@ export default class DeviceCondition extends Component<any, any> {
} }
public devicePicked(ev) { public devicePicked(ev) {
if (!this.initialized) {
return;
}
this.setState({ ...this.state, device_id: ev.target.value }); this.setState({ ...this.state, device_id: ev.target.value });
} }
public deviceConditionPicked(ev) { public deviceConditionPicked(ev) {
if (!this.initialized) {
return;
}
let condition = ev.target.value; let condition = ev.target.value;
if ( if (
this._origCondition && this._origCondition &&
@ -74,6 +83,7 @@ export default class DeviceCondition extends Component<any, any> {
} }
public componentDidMount() { public componentDidMount() {
this.initialized = true;
if (!this.state.capabilities) { if (!this.state.capabilities) {
this._getCapabilities(); this._getCapabilities();
} }
@ -98,6 +108,9 @@ export default class DeviceCondition extends Component<any, any> {
} }
private _extraFieldsChanged(ev) { private _extraFieldsChanged(ev) {
if (!this.initialized) {
return;
}
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, {
...this.props.condition, ...this.props.condition,
...ev.detail.value, ...ev.detail.value,

View File

@ -1,30 +1,23 @@
import { h, Component } from "preact"; import { h } from "preact";
import Condition from "./index"; import Condition from "./index";
import { AutomationComponent } from "../automation-component";
export default class LogicalCondition extends Component<any, any> { export default class LogicalCondition extends AutomationComponent {
private _mounted = false;
constructor() { constructor() {
super(); super();
this.conditionChanged = this.conditionChanged.bind(this); this.conditionChanged = this.conditionChanged.bind(this);
} }
public conditionChanged(conditions) { public conditionChanged(conditions) {
if (this._mounted) { if (!this.initialized) {
return;
}
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, {
...this.props.condition, ...this.props.condition,
conditions, conditions,
}); });
} }
}
public componentWillMount() {
this._mounted = true;
}
public componentWillUnmount() {
this._mounted = false;
}
/* eslint-disable camelcase */ /* eslint-disable camelcase */
public render({ condition, hass, localize }) { public render({ condition, hass, localize }) {

View File

@ -1,11 +1,12 @@
import { h, Component } from "preact"; import { h } from "preact";
import "@polymer/paper-input/paper-input"; import "@polymer/paper-input/paper-input";
import "../../../../components/ha-textarea"; import "../../../../components/ha-textarea";
import "../../../../components/entity/ha-entity-picker"; import "../../../../components/entity/ha-entity-picker";
import { onChangeEvent } from "../../../../common/preact/event"; import { onChangeEvent } from "../../../../common/preact/event";
import { AutomationComponent } from "../automation-component";
export default class NumericStateCondition extends Component<any> { export default class NumericStateCondition extends AutomationComponent {
private onChange: (obj: any) => void; private onChange: (obj: any) => void;
constructor() { constructor() {
super(); super();
@ -15,6 +16,9 @@ export default class NumericStateCondition extends Component<any> {
} }
public entityPicked(ev) { public entityPicked(ev) {
if (!this.initialized) {
return;
}
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, {
...this.props.condition, ...this.props.condition,
entity_id: ev.target.value, entity_id: ev.target.value,

View File

@ -1,10 +1,11 @@
import { h, Component } from "preact"; import { h } from "preact";
import "@polymer/paper-input/paper-input"; import "@polymer/paper-input/paper-input";
import "../../../../components/entity/ha-entity-picker"; import "../../../../components/entity/ha-entity-picker";
import { onChangeEvent } from "../../../../common/preact/event"; import { onChangeEvent } from "../../../../common/preact/event";
import { AutomationComponent } from "../automation-component";
export default class StateCondition extends Component<any> { export default class StateCondition extends AutomationComponent {
private onChange: (obj: any) => void; private onChange: (obj: any) => void;
constructor() { constructor() {
super(); super();
@ -14,6 +15,9 @@ export default class StateCondition extends Component<any> {
} }
public entityPicked(ev) { public entityPicked(ev) {
if (!this.initialized) {
return;
}
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, {
...this.props.condition, ...this.props.condition,
entity_id: ev.target.value, entity_id: ev.target.value,

View File

@ -1,11 +1,12 @@
import { h, Component } from "preact"; import { h } from "preact";
import "@polymer/paper-input/paper-input"; import "@polymer/paper-input/paper-input";
import "@polymer/paper-radio-button/paper-radio-button"; import "@polymer/paper-radio-button/paper-radio-button";
import "@polymer/paper-radio-group/paper-radio-group"; import "@polymer/paper-radio-group/paper-radio-group";
import { onChangeEvent } from "../../../../common/preact/event"; import { onChangeEvent } from "../../../../common/preact/event";
import { AutomationComponent } from "../automation-component";
export default class SunCondition extends Component<any> { export default class SunCondition extends AutomationComponent {
private onChange: (obj: any) => void; private onChange: (obj: any) => void;
private afterPicked: (obj: any) => void; private afterPicked: (obj: any) => void;
private beforePicked: (obj: any) => void; private beforePicked: (obj: any) => void;
@ -19,6 +20,9 @@ export default class SunCondition extends Component<any> {
} }
public radioGroupPicked(key, ev) { public radioGroupPicked(key, ev) {
if (!this.initialized) {
return;
}
const condition = { ...this.props.condition }; const condition = { ...this.props.condition };
if (ev.target.selected) { if (ev.target.selected) {

View File

@ -1,9 +1,10 @@
import { h, Component } from "preact"; import { h } from "preact";
import "../../../../components/ha-textarea"; import "../../../../components/ha-textarea";
import { onChangeEvent } from "../../../../common/preact/event"; import { onChangeEvent } from "../../../../common/preact/event";
import { AutomationComponent } from "../automation-component";
export default class TemplateCondition extends Component<any> { export default class TemplateCondition extends AutomationComponent {
private onChange: (obj: any) => void; private onChange: (obj: any) => void;
constructor() { constructor() {
super(); super();

View File

@ -1,9 +1,10 @@
import { h, Component } from "preact"; import { h } from "preact";
import "@polymer/paper-input/paper-input"; import "@polymer/paper-input/paper-input";
import { onChangeEvent } from "../../../../common/preact/event"; import { onChangeEvent } from "../../../../common/preact/event";
import { AutomationComponent } from "../automation-component";
export default class TimeCondition extends Component<any> { export default class TimeCondition extends AutomationComponent {
private onChange: (obj: any) => void; private onChange: (obj: any) => void;
constructor() { constructor() {
super(); super();

View File

@ -1,13 +1,15 @@
import { h, Component } from "preact"; import { h } from "preact";
import "../../../../components/entity/ha-entity-picker"; import "../../../../components/entity/ha-entity-picker";
import { hasLocation } from "../../../../common/entity/has_location"; import { hasLocation } from "../../../../common/entity/has_location";
import { computeStateDomain } from "../../../../common/entity/compute_state_domain"; import { computeStateDomain } from "../../../../common/entity/compute_state_domain";
import { AutomationComponent } from "../automation-component";
function zoneAndLocationFilter(stateObj) { function zoneAndLocationFilter(stateObj) {
return hasLocation(stateObj) && computeStateDomain(stateObj) !== "zone"; return hasLocation(stateObj) && computeStateDomain(stateObj) !== "zone";
} }
export default class ZoneCondition extends Component<any> { export default class ZoneCondition extends AutomationComponent {
constructor() { constructor() {
super(); super();
@ -16,6 +18,9 @@ export default class ZoneCondition extends Component<any> {
} }
public entityPicked(ev) { public entityPicked(ev) {
if (!this.initialized) {
return;
}
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, {
...this.props.condition, ...this.props.condition,
entity_id: ev.target.value, entity_id: ev.target.value,
@ -23,6 +28,9 @@ export default class ZoneCondition extends Component<any> {
} }
public zonePicked(ev) { public zonePicked(ev) {
if (!this.initialized) {
return;
}
this.props.onChange(this.props.index, { this.props.onChange(this.props.index, {
...this.props.condition, ...this.props.condition,
zone: ev.target.value, zone: ev.target.value,