fixed condition autmations

This commit is contained in:
britea 2020-05-21 16:13:47 +02:00
parent 3830c0004f
commit 727a5c9081
3 changed files with 88 additions and 36 deletions

View file

@ -49,8 +49,21 @@ const deviceStateOptions = [
{ key: 'on', text: 'on', value: true }, { key: 'on', text: 'on', value: true },
]; ];
const thermostatOptions = [
{ key: 'HEATING', text: 'HEATING', value: 'HEATING' },
{ key: 'COOLING', text: 'COOLING', value: 'COOLING' },
{ key: 'IDLE', text: 'IDLE', value: 'IDLE' },
{ key: 'OFF', text: 'OFF', value: 'OFF' },
];
const thermostatOperands = [
{ key: 'EQUAL', text: '=', value: 'EQUAL' },
{ key: 'NOTEQUAL', text: '\u2260', value: 'NOTEQUAL' },
];
const CreateTrigger = (props) => { const CreateTrigger = (props) => {
const [activeOperand, setActiveOperand] = useState(true); const [activeOperand, setActiveOperand] = useState(true);
const [activeThermostat, setActiveThermostat] = useState(false);
const operandsRef = useRef(null); const operandsRef = useRef(null);
const valuesRef = useRef(null); const valuesRef = useRef(null);
const notAdmitedDevices = ['buttonDimmer']; const notAdmitedDevices = ['buttonDimmer'];
@ -72,7 +85,7 @@ const CreateTrigger = (props) => {
const onChange = (e, val) => { const onChange = (e, val) => {
props.inputChange(val); props.inputChange(val);
setActiveOperand(hasOperand.has(props.devices[val.value].kind)); setActiveOperand(hasOperand.has(props.devices[val.value].kind));
setActiveThermostat(props.devices[val.value].kind === 'thermostat');
if (operandsRef.current) operandsRef.current.setValue(''); if (operandsRef.current) operandsRef.current.setValue('');
if (valuesRef.current) valuesRef.current.inputRef.current.valueAsNumber = undefined; if (valuesRef.current) valuesRef.current.inputRef.current.valueAsNumber = undefined;
}; };
@ -92,30 +105,55 @@ const CreateTrigger = (props) => {
placeholder="Device" placeholder="Device"
/> />
</Form.Field> </Form.Field>
{activeOperand ? ( {
<> activeThermostat ? (
<Form.Field inline width={2}> <>
<Dropdown <Form.Field inline width={2}>
onChange={(e, val) => props.inputChange(val)} <Dropdown
ref={operandsRef} onChange={(e, val) => props.inputChange(val)}
name="operand" ref={operandsRef}
compact name="operand"
selection compact
options={operands} selection
/> options={thermostatOperands}
</Form.Field> />
<Form.Field inline width={7}> </Form.Field>
<Input <Form.Field inline width={7}>
onChange={(e, val) => { <Dropdown
onChange={(e, val) => props.inputChange(val)}
placeholder="State"
name="mode"
compact
selection
options={thermostatOptions}
/>
</Form.Field>
</>
)
: activeOperand ? (
<>
<Form.Field inline width={2}>
<Dropdown
onChange={(e, val) => props.inputChange(val)}
ref={operandsRef}
name="operand"
compact
selection
options={operands}
/>
</Form.Field>
<Form.Field inline width={7}>
<Input
onChange={(e, val) => {
props.inputChange(val); props.inputChange(val);
}} }}
ref={valuesRef} ref={valuesRef}
name="value" name="value"
type="number" type="number"
placeholder="Value" placeholder="Value"
/> />
</Form.Field> </Form.Field>
</> </>
) : ( ) : (
<Form.Field inline width={7}> <Form.Field inline width={7}>
<Dropdown <Dropdown
@ -127,7 +165,8 @@ const CreateTrigger = (props) => {
options={deviceStateOptions} options={deviceStateOptions}
/> />
</Form.Field> </Form.Field>
)} )
}
</Form.Group> </Form.Group>
</Form> </Form>
</List.Content> </List.Content>
@ -165,7 +204,9 @@ const SceneItem = (props) => {
const Trigger = ({ const Trigger = ({
deviceName, trigger, onRemove, index, deviceName, trigger, onRemove, index,
}) => { }) => {
const { operand, value, on } = trigger; const {
operand, value, on, mode,
} = trigger;
let symbol; let symbol;
if (operand) { if (operand) {
symbol = operands.filter((opt) => opt.key === operand)[0].text; symbol = operands.filter((opt) => opt.key === operand)[0].text;
@ -175,7 +216,7 @@ const Trigger = ({
<Menu compact> <Menu compact>
<Menu.Item as="span">{deviceName}</Menu.Item> <Menu.Item as="span">{deviceName}</Menu.Item>
{operand ? <Menu.Item as="span">{symbol}</Menu.Item> : ''} {operand ? <Menu.Item as="span">{symbol}</Menu.Item> : ''}
<Menu.Item as="span">{operand ? value : on ? 'on' : 'off'}</Menu.Item> <Menu.Item as="span">{mode || (operand ? value : on ? 'on' : 'off')}</Menu.Item>
</Menu> </Menu>
<Icon <Icon
as="i" as="i"
@ -292,19 +333,24 @@ class AutomationSaveModal extends Component {
)[0]; )[0];
const triggerKind = this.triggerKind(trigger); const triggerKind = this.triggerKind(trigger);
const conditionKind = this.conditionKind(trigger);
if (!isCondition && (!device || !triggerKind)) {
error.message = 'There are missing fields';
return error;
}
if (!device || !triggerKind) { if (isCondition && !conditionKind) {
error.message = 'There are missing fields'; error.message = 'There are missing fields';
return error; return error;
} }
const deviceKind = device.kind; const deviceKind = device.kind;
const devicesWithPercentage = ['dimmableLight', 'curtains', 'knobDimmer']; const devicesWithPercentage = ['dimmableLight', 'curtains', 'knobDimmer'];
switch (triggerKind) { switch (isCondition ? conditionKind : triggerKind) {
case 'booleanTrigger': case 'booleanTrigger' || 'booleanCondition':
if (!trigger.device || trigger.on === null || trigger.on === undefined) return error; if (!trigger.device || trigger.on === null || trigger.on === undefined) return error;
break; break;
case 'rangeTrigger': case 'rangeTrigger' || 'rangeCondition':
if (!trigger.device || !trigger.operand || !trigger.value) { if (!trigger.device || !trigger.operand || !trigger.value) {
return error; return error;
} }
@ -329,6 +375,9 @@ class AutomationSaveModal extends Component {
return error; return error;
} }
break; break;
case 'thermostatCondition':
if (!trigger.device || trigger.mode === null || trigger.mode === undefined || !trigger.operand) return error;
break;
default: default:
throw new Error('theoretically unreachable statement'); throw new Error('theoretically unreachable statement');
} }
@ -413,12 +462,14 @@ class AutomationSaveModal extends Component {
return this.props.scenes.filter((e) => e.name.includes(this.scenesFilter)); return this.props.scenes.filter((e) => e.name.includes(this.scenesFilter));
} }
_generateKey = (trigger) => { _generateKey = (trigger, isCondition = false) => {
switch (this.triggerKind(trigger)) { switch (isCondition ? this.conditionKind(trigger) : this.triggerKind(trigger)) {
case 'booleanTrigger': case 'booleanTrigger' || 'booleanCondition':
return `${trigger.device}${trigger.on}`; return `${trigger.device}${trigger.on}`;
case 'rangeTrigger': case 'rangeTrigger' || 'rangeCondition':
return `${trigger.device}${trigger.operand}${trigger.value}`; return `${trigger.device}${trigger.operand}${trigger.value}`;
case 'thermostatCondition':
return `${trigger.device}${trigger.operand}${trigger.mode}`;
default: default:
throw new Error('theoretically unreachable statement'); throw new Error('theoretically unreachable statement');
} }
@ -670,7 +721,7 @@ class AutomationSaveModal extends Component {
const deviceName = this.deviceList.filter( const deviceName = this.deviceList.filter(
(d) => d.id === condition.device, (d) => d.id === condition.device,
)[0].name; )[0].name;
const key = this._generateKey(condition); const key = this._generateKey(condition, true);
return ( return (
<Trigger <Trigger
key={key} key={key}

View file

@ -103,7 +103,7 @@ const Automation = ({
{condition.operator {condition.operator
? `${getOperator(condition.operator) ? `${getOperator(condition.operator)
} ${ } ${
condition.range}` condition.mode ? condition.mode : condition.range}`
: condition.on : condition.on
? ' - on' ? ' - on'
: ' - off'} : ' - off'}

View file

@ -578,6 +578,7 @@ export const RemoteService = {
); );
const rangeConditionList = conditionList.filter((condition) => 'operand' in condition && 'value' in condition); const rangeConditionList = conditionList.filter((condition) => 'operand' in condition && 'value' in condition);
const booleanConditionList = conditionList.filter((condition) => 'on' in condition); const booleanConditionList = conditionList.filter((condition) => 'on' in condition);
debugger;
const thermostatConditionList = conditionList.filter((condition) => 'operand' in condition && 'mode' in condition); const thermostatConditionList = conditionList.filter((condition) => 'operand' in condition && 'mode' in condition);