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

View File

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

View File

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