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
7 changes: 5 additions & 2 deletions src/panels/config/js/condition/condition_edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "@polymer/paper-listbox/paper-listbox";
import "@polymer/paper-item/paper-item";

import DeviceCondition from "./device";
import LogicalCondition from "./logical";
import NumericStateCondition from "./numeric_state";
import StateCondition from "./state";
import SunCondition from "./sun";
Expand All @@ -12,9 +13,11 @@ import TimeCondition from "./time";
import ZoneCondition from "./zone";

const TYPES = {
and: LogicalCondition,
device: DeviceCondition,
state: StateCondition,
numeric_state: NumericStateCondition,
or: LogicalCondition,
state: StateCondition,
sun: SunCondition,
template: TemplateCondition,
time: TimeCondition,
Expand All @@ -23,7 +26,7 @@ const TYPES = {

const OPTIONS = Object.keys(TYPES).sort();

export default class ConditionRow extends Component<any> {
export default class ConditionEdit extends Component<any> {
constructor() {
super();

Expand Down
46 changes: 46 additions & 0 deletions src/panels/config/js/condition/logical.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { h, Component } from "preact";

import Condition from "./index";

export default class LogicalCondition extends Component<any, any> {
private _mounted = false;
constructor() {
super();
this.conditionChanged = this.conditionChanged.bind(this);
}

public conditionChanged(conditions) {
if (this._mounted) {
Copy link
Copy Markdown
Collaborator Author

@emontnemery emontnemery Sep 16, 2019

Choose a reason for hiding this comment

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

Without this check, things do not work correctly, reproduction steps:

  • Create a new automation
  • Add a condition and set type to and
  • Add two sub-conditions under the and condition, and populate them, for example
    image
  • Change type of the and condition to or:
    image
  • Add a second sub-condition under the or condition - the or condition is reverted back to and:
    image

this.props.onChange(this.props.index, {
...this.props.condition,
conditions,
});
}
}

public componentWillMount() {
this._mounted = true;
}

public componentWillUnmount() {
this._mounted = false;
}

/* eslint-disable camelcase */
public render({ condition, hass, localize }) {
return (
<div>
<Condition
condition={condition.conditions || []}
onChange={this.conditionChanged}
hass={hass}
localize={localize}
/>
</div>
);
}
}

(LogicalCondition as any).defaultConfig = {
conditions: [{ condition: "state" }],
};
14 changes: 10 additions & 4 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -802,19 +802,25 @@
"unsupported_condition": "Unsupported condition: {condition}",
"type_select": "Condition type",
"type": {
"and": {
"label": "And"
},
"device": {
"label": "Device"
},
"state": {
"label": "[%key:ui::panel::config::automation::editor::triggers::type::state::label%]",
"state": "[%key:ui::panel::config::automation::editor::triggers::type::state::label%]"
},
"numeric_state": {
"label": "[%key:ui::panel::config::automation::editor::triggers::type::numeric_state::label%]",
"above": "[%key:ui::panel::config::automation::editor::triggers::type::numeric_state::above%]",
"below": "[%key:ui::panel::config::automation::editor::triggers::type::numeric_state::below%]",
"value_template": "[%key:ui::panel::config::automation::editor::triggers::type::numeric_state::value_template%]"
},
"or": {
"label": "Or"
},
"state": {
"label": "[%key:ui::panel::config::automation::editor::triggers::type::state::label%]",
"state": "[%key:ui::panel::config::automation::editor::triggers::type::state::label%]"
},
"sun": {
"label": "[%key:ui::panel::config::automation::editor::triggers::type::sun::label%]",
"before": "Before:",
Expand Down