connected automation conditions to the backend

This commit is contained in:
britea 2020-05-19 16:23:30 +02:00
parent fa4bae7c10
commit 0d2a2e92ba
3 changed files with 367 additions and 248 deletions

View file

@ -236,10 +236,9 @@ class AutomationSaveModal extends Component {
this.onChangeName = this.onChangeName.bind(this); this.onChangeName = this.onChangeName.bind(this);
// Conditions // Conditions
this.setNewCondition = this._setter("newCondition"); this.setNewCondition = this._setter('newCondition');
this.addCondition = this.addCondition.bind(this); this.addCondition = this.addCondition.bind(this);
this.removeCondition = this.removeCondition.bind(this); this.removeCondition = this.removeCondition.bind(this);
} }
openModal = (e) => { openModal = (e) => {
@ -269,6 +268,20 @@ class AutomationSaveModal extends Component {
// throw new Error("Trigger kind not handled"); // 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) { _checkNewTrigger(trigger, isCondition = false) {
const error = { const error = {
result: false, result: false,
@ -331,13 +344,13 @@ class AutomationSaveModal extends Component {
(t) => t.device === trigger.device && t.operand === trigger.operand, (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`; const duplicationMessage = `You have already created a ${type} for this device with the same conditions`;
return { return {
result: isNotDuplicate, result: isNotDuplicate,
message: isNotDuplicate message: isNotDuplicate
? null ? 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 this.props
.fastUpdateAutomation(automation) .fastUpdateAutomation(automation)
.then(this.closeModal) .then(this.closeModal)
@ -471,6 +503,7 @@ class AutomationSaveModal extends Component {
automation, automation,
triggerList: this.state.triggerList, triggerList: this.state.triggerList,
order: this.state.order, order: this.state.order,
conditionList: this.state.conditionsList,
}) })
.then(this.closeModal) .then(this.closeModal)
.catch(console.error); .catch(console.error);

View file

@ -17,7 +17,7 @@ import CreateAutomation, { operands } from './AutomationCreationModal';
const Automation = ({ const Automation = ({
automation, devices, scenes, removeAutomation, automation, devices, scenes, removeAutomation,
}) => { }) => {
const { triggers } = automation; const { triggers, conditions } = automation;
const scenePriorities = automation.scenes; const scenePriorities = automation.scenes;
const getOperator = (operand) => operands.filter((o) => o.key === operand)[0].text; const getOperator = (operand) => operands.filter((o) => o.key === operand)[0].text;
@ -85,6 +85,36 @@ const Automation = ({
</Grid.Row> </Grid.Row>
</Grid> </Grid>
</Segment> </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>
</> </>
); );
}; };

View file

@ -556,19 +556,30 @@ export const RemoteService = {
* with user-fiendly errors as a RemoteError * with user-fiendly errors as a RemoteError
*/ */
saveAutomation: (data) => { saveAutomation: (data) => {
const { automation, triggerList, order } = data; const {
automation, triggerList, order, conditionList,
} = data;
automation.triggers = []; automation.triggers = [];
automation.scenes = []; automation.scenes = [];
automation.condition = [];
return (dispatch) => { return (dispatch) => {
const urlAutomation = '/automation'; const urlAutomation = '/automation';
const urlBooleanTrigger = '/booleanTrigger'; const urlBooleanTrigger = '/booleanTrigger';
const urlRangeTrigger = '/rangeTrigger'; const urlRangeTrigger = '/rangeTrigger';
const urlScenePriority = '/scenePriority'; const urlScenePriority = '/scenePriority';
// conditions
const urlRangeCondition = '/rangeCondition';
const urlBooleanCondition = '/booleanCondition';
const urlThermostatCondition = '/thermostatCondition';
const rangeTriggerList = triggerList.filter((trigger) => 'operand' in trigger); const rangeTriggerList = triggerList.filter((trigger) => 'operand' in trigger);
const booleanTriggerList = triggerList.filter( const booleanTriggerList = triggerList.filter(
(trigger) => !('operand' in trigger), (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( return Endpoint.post(urlAutomation, {}, automation).then(
async (automationRes) => { async (automationRes) => {
@ -591,7 +602,7 @@ export const RemoteService = {
const trigger = { const trigger = {
automationId: id, automationId: id,
deviceId: t.device, deviceId: t.device,
on: t.value, on: t.on,
}; };
resBoolTriggers.push(Endpoint.post( resBoolTriggers.push(Endpoint.post(
urlBooleanTrigger, urlBooleanTrigger,
@ -601,6 +612,51 @@ export const RemoteService = {
} }
automation.triggers.push(...((await Promise.all(resBoolTriggers)).map((v) => v.data))); 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 = []; const resScenePriorities = [];
for (const [priority, sceneId] of order.entries()) { for (const [priority, sceneId] of order.entries()) {
const scenePriority = { const scenePriority = {