Skip to content

Commit

Permalink
Scene: Be able to blink lights from switch devices (#2064)
Browse files Browse the repository at this point in the history
  • Loading branch information
cicoub13 authored May 3, 2024
1 parent f90b143 commit 6f0bc73
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
25 changes: 16 additions & 9 deletions front/src/routes/scene/edit-scene/actions/BlinkLightParams.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ import Select from 'react-select';
class BlinkLight extends Component {
getOptions = async () => {
try {
const devices = await this.props.httpClient.get('/api/v1/device', {
const lightDevices = await this.props.httpClient.get('/api/v1/device', {
device_feature_category: 'light',
device_feature_type: 'binary'
});
const deviceOptions = devices.map(device => ({
value: device.selector,
label: device.name
}));
const switchDevices = await this.props.httpClient.get('/api/v1/device', {
device_feature_category: 'switch',
device_feature_type: 'binary'
});
const deviceOptions = [...lightDevices, ...switchDevices]
.map(device => ({
value: device.selector,
label: device.name
}))
.sort((d1, d2) => d1.label.localeCompare(d2.label));
await this.setState({ deviceOptions });
this.refreshSelectedOptions(this.props);
return deviceOptions;
Expand All @@ -23,8 +29,8 @@ class BlinkLight extends Component {
};
handleChange = selectedOptions => {
if (selectedOptions) {
const lights = selectedOptions.map(selectedOption => selectedOption.value);
this.props.updateActionProperty(this.props.columnIndex, this.props.index, 'devices', lights);
const devices = selectedOptions.map(selectedOption => selectedOption.value);
this.props.updateActionProperty(this.props.columnIndex, this.props.index, 'devices', devices);
} else {
this.props.updateActionProperty(this.props.columnIndex, this.props.index, 'devices', []);
}
Expand All @@ -39,13 +45,14 @@ class BlinkLight extends Component {
refreshSelectedOptions = nextProps => {
const selectedOptions = [];
if (nextProps.action.devices && this.state.deviceOptions) {
nextProps.action.devices.forEach(light => {
const deviceOption = this.state.deviceOptions.find(deviceOption => deviceOption.value === light);
nextProps.action.devices.forEach(device => {
const deviceOption = this.state.deviceOptions.find(deviceOption => deviceOption.value === device);
if (deviceOption) {
selectedOptions.push(deviceOption);
}
});
}
selectedOptions.sort((d1, d2) => d1.label.localeCompare(d2.label));
this.setState({ selectedOptions });
};
constructor(props) {
Expand Down
9 changes: 8 additions & 1 deletion server/lib/scene/scene.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,18 @@ const actionsFunc = {
await Promise.map(action.devices, async (deviceSelector) => {
try {
const device = self.stateManager.get('device', deviceSelector);
const deviceFeature = getDeviceFeature(
let deviceFeature = getDeviceFeature(
device,
DEVICE_FEATURE_CATEGORIES.LIGHT,
DEVICE_FEATURE_TYPES.LIGHT.BINARY,
);
if (!deviceFeature) {
deviceFeature = getDeviceFeature(
device,
DEVICE_FEATURE_CATEGORIES.SWITCH,
DEVICE_FEATURE_TYPES.SWITCH.BINARY,
);
}
const oldValue = deviceFeature.last_value;
let newValue = 0;
/* eslint-disable no-await-in-loop */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,45 @@ describe('scene.blink-lights', () => {
assert.callCount(device.setValue, 12);
});

it('should blink switch in fast mode', async () => {
const stateManager = new StateManager(event);
const deviceFeature = {
category: DEVICE_FEATURE_CATEGORIES.SWITCH,
type: DEVICE_FEATURE_TYPES.SWITCH.BINARY,
last_value: 0,
};
const device = {
setValue: fake.resolves(null),
features: {
category: DEVICE_FEATURE_CATEGORIES.SWITCH,
type: DEVICE_FEATURE_TYPES.SWITCH.BINARY,
find: fake.returns(deviceFeature),
},
};

stateManager.setState('device', 'switch-1', device);

const scope = {};
executeActions(
{ stateManager, event, device },
[
[
{
type: ACTIONS.LIGHT.BLINK,
devices: ['switch-1'],
blinking_speed: 'fast',
blinking_time: 2,
},
],
],
scope,
);
await clock.tickAsync(3000);
assert.calledWithExactly(device.setValue, device, deviceFeature, 0);
assert.calledWithExactly(device.setValue, device, deviceFeature, 1);
assert.callCount(device.setValue, 12);
});

it('should blink light in unknown mode', async () => {
const stateManager = new StateManager(event);
const deviceFeature = {
Expand Down

0 comments on commit 6f0bc73

Please sign in to comment.