connected automation conditions to the backend
This commit is contained in:
parent
fa4bae7c10
commit
0d2a2e92ba
3 changed files with 367 additions and 248 deletions
|
@ -236,10 +236,9 @@ class AutomationSaveModal extends Component {
|
|||
this.onChangeName = this.onChangeName.bind(this);
|
||||
|
||||
// Conditions
|
||||
this.setNewCondition = this._setter("newCondition");
|
||||
this.setNewCondition = this._setter('newCondition');
|
||||
this.addCondition = this.addCondition.bind(this);
|
||||
this.removeCondition = this.removeCondition.bind(this);
|
||||
|
||||
}
|
||||
|
||||
openModal = (e) => {
|
||||
|
@ -269,6 +268,20 @@ class AutomationSaveModal extends Component {
|
|||
// throw new Error("Trigger kind not handled");
|
||||
}
|
||||
|
||||
conditionKind(condition) {
|
||||
if ('operand' in condition && 'value' in condition) {
|
||||
return 'rangeTrigger';
|
||||
}
|
||||
if ('on' in condition) {
|
||||
return 'booleanTrigger';
|
||||
}
|
||||
|
||||
if ('operand' in condition && 'mode' in condition) {
|
||||
return 'thermostatCondition';
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
_checkNewTrigger(trigger, isCondition = false) {
|
||||
const error = {
|
||||
result: false,
|
||||
|
@ -331,13 +344,13 @@ class AutomationSaveModal extends Component {
|
|||
(t) => t.device === trigger.device && t.operand === trigger.operand,
|
||||
);
|
||||
}
|
||||
const type = isCondition ? "condition" : "trigger"
|
||||
const type = isCondition ? 'condition' : 'trigger';
|
||||
const duplicationMessage = `You have already created a ${type} for this device with the same conditions`;
|
||||
return {
|
||||
result: isNotDuplicate,
|
||||
message: isNotDuplicate
|
||||
? null
|
||||
: duplicationMessage
|
||||
: duplicationMessage,
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -461,6 +474,25 @@ class AutomationSaveModal extends Component {
|
|||
);
|
||||
}
|
||||
|
||||
for (const condition of this.state.conditionsList) {
|
||||
const kind = condition.kind || this.conditionKind(condition);
|
||||
const loSpagnolo = (kind === 'thermostatCondition' ? { operator: condition.operand, mode: condition.mode }
|
||||
: {
|
||||
operator: condition.operand,
|
||||
range: parseInt(condition.value),
|
||||
});
|
||||
automation.conditions.push(
|
||||
{
|
||||
deviceId: condition.device,
|
||||
kind,
|
||||
...(kind === 'booleanTrigger'
|
||||
? { on: condition.on }
|
||||
: loSpagnolo
|
||||
),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
this.props
|
||||
.fastUpdateAutomation(automation)
|
||||
.then(this.closeModal)
|
||||
|
@ -471,6 +503,7 @@ class AutomationSaveModal extends Component {
|
|||
automation,
|
||||
triggerList: this.state.triggerList,
|
||||
order: this.state.order,
|
||||
conditionList: this.state.conditionsList,
|
||||
})
|
||||
.then(this.closeModal)
|
||||
.catch(console.error);
|
||||
|
|
|
@ -17,7 +17,7 @@ import CreateAutomation, { operands } from './AutomationCreationModal';
|
|||
const Automation = ({
|
||||
automation, devices, scenes, removeAutomation,
|
||||
}) => {
|
||||
const { triggers } = automation;
|
||||
const { triggers, conditions } = automation;
|
||||
const scenePriorities = automation.scenes;
|
||||
const getOperator = (operand) => operands.filter((o) => o.key === operand)[0].text;
|
||||
|
||||
|
@ -85,6 +85,36 @@ const Automation = ({
|
|||
</Grid.Row>
|
||||
</Grid>
|
||||
</Segment>
|
||||
<Grid columns={1} stackable textAlign="center">
|
||||
<Grid.Row verticalAlign="middle">
|
||||
<Grid.Column>
|
||||
<Header>Conditions</Header>
|
||||
<List divided relaxed>
|
||||
{conditions !== undefined
|
||||
&& conditions.map((condition) => {
|
||||
const device = devices.filter(
|
||||
(d) => d.id === condition.deviceId,
|
||||
)[0];
|
||||
return (
|
||||
<Menu key={condition.id} compact>
|
||||
<Menu.Item as="span">
|
||||
{device.name}
|
||||
{' '}
|
||||
{condition.operator
|
||||
? `${getOperator(condition.operator)
|
||||
} ${
|
||||
condition.range}`
|
||||
: condition.on
|
||||
? ' - on'
|
||||
: ' - off'}
|
||||
</Menu.Item>
|
||||
</Menu>
|
||||
);
|
||||
})}
|
||||
</List>
|
||||
</Grid.Column>
|
||||
</Grid.Row>
|
||||
</Grid>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
|
|
@ -556,19 +556,30 @@ export const RemoteService = {
|
|||
* with user-fiendly errors as a RemoteError
|
||||
*/
|
||||
saveAutomation: (data) => {
|
||||
const { automation, triggerList, order } = data;
|
||||
const {
|
||||
automation, triggerList, order, conditionList,
|
||||
} = data;
|
||||
automation.triggers = [];
|
||||
automation.scenes = [];
|
||||
automation.condition = [];
|
||||
return (dispatch) => {
|
||||
const urlAutomation = '/automation';
|
||||
const urlBooleanTrigger = '/booleanTrigger';
|
||||
const urlRangeTrigger = '/rangeTrigger';
|
||||
const urlScenePriority = '/scenePriority';
|
||||
// conditions
|
||||
const urlRangeCondition = '/rangeCondition';
|
||||
const urlBooleanCondition = '/booleanCondition';
|
||||
const urlThermostatCondition = '/thermostatCondition';
|
||||
|
||||
const rangeTriggerList = triggerList.filter((trigger) => 'operand' in trigger);
|
||||
const booleanTriggerList = triggerList.filter(
|
||||
(trigger) => !('operand' in trigger),
|
||||
);
|
||||
const rangeConditionList = conditionList.filter((condition) => 'operand' in condition && 'value' in condition);
|
||||
const booleanConditionList = conditionList.filter((condition) => 'on' in condition);
|
||||
const thermostatConditionList = conditionList.filter((condition) => 'operand' in condition && 'mode' in condition);
|
||||
|
||||
|
||||
return Endpoint.post(urlAutomation, {}, automation).then(
|
||||
async (automationRes) => {
|
||||
|
@ -591,7 +602,7 @@ export const RemoteService = {
|
|||
const trigger = {
|
||||
automationId: id,
|
||||
deviceId: t.device,
|
||||
on: t.value,
|
||||
on: t.on,
|
||||
};
|
||||
resBoolTriggers.push(Endpoint.post(
|
||||
urlBooleanTrigger,
|
||||
|
@ -601,6 +612,51 @@ export const RemoteService = {
|
|||
}
|
||||
automation.triggers.push(...((await Promise.all(resBoolTriggers)).map((v) => v.data)));
|
||||
|
||||
// Conditions
|
||||
const resRangeConditions = [];
|
||||
for (const t of rangeConditionList) {
|
||||
const condition = {
|
||||
automationId: id,
|
||||
deviceId: t.device,
|
||||
operator: t.operand,
|
||||
range: t.value,
|
||||
};
|
||||
resRangeConditions.push(Endpoint.post(urlRangeCondition, {}, condition));
|
||||
}
|
||||
automation.conditions = (await Promise.all(resRangeConditions)).map((v) => v.data);
|
||||
|
||||
const resBoolConditions = [];
|
||||
for (const t of booleanConditionList) {
|
||||
console.log('HERE', t);
|
||||
const condition = {
|
||||
automationId: id,
|
||||
deviceId: t.device,
|
||||
on: t.on,
|
||||
};
|
||||
resBoolConditions.push(Endpoint.post(
|
||||
urlBooleanCondition,
|
||||
{},
|
||||
condition,
|
||||
));
|
||||
}
|
||||
automation.conditions.push(...((await Promise.all(resBoolConditions)).map((v) => v.data)));
|
||||
|
||||
const resThermoConditions = [];
|
||||
for (const t of thermostatConditionList) {
|
||||
const condition = {
|
||||
automationId: id,
|
||||
deviceId: t.device,
|
||||
mode: t.mode,
|
||||
operator: t.operand,
|
||||
};
|
||||
resThermoConditions.push(Endpoint.post(
|
||||
urlThermostatCondition,
|
||||
{},
|
||||
condition,
|
||||
));
|
||||
}
|
||||
automation.conditions.push(...((await Promise.all(resThermoConditions)).map((v) => v.data)));
|
||||
|
||||
const resScenePriorities = [];
|
||||
for (const [priority, sceneId] of order.entries()) {
|
||||
const scenePriority = {
|
||||
|
|
Loading…
Reference in a new issue