Skip to content

Commit

Permalink
feat(scenes): Allow blinking switchs
Browse files Browse the repository at this point in the history
  • Loading branch information
cicoub13 committed Apr 25, 2024
1 parent 0ebc8c2 commit f09c501
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 43 deletions.
6 changes: 3 additions & 3 deletions front/src/config/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -1684,8 +1684,8 @@
"label": "Wähle die Lichter aus, die umgeschaltet werden sollen",
"description": "Diese Aktion schaltet das Licht ein, wenn es ausgeschaltet ist bzw. schaltet es aus, wenn es eingeschaltet ist"
},
"blinkLights": {
"label": "Wählen Sie die Lichter aus, die Sie zum Blinken bringen möchten",
"blinkDevices": {
"label": "Wählen Sie die Lichter/Schalter aus, die Sie blinken lassen möchten",
"description": "Stellen Sie die Dauer und die Geschwindigkeit des Blinkens ein",
"blinkingTime": {
"label": "Dauer (in Sekunden)",
Expand Down Expand Up @@ -1840,7 +1840,7 @@
"turn-on": "Licht einschalten",
"turn-off": "Licht ausschalten",
"toggle": "Lichter umschalten",
"blink": "Lichter blinken lassen"
"blink": "Lichter/Schalter blinken lassen"
},
"switch": {
"turn-on": "Schalter einschalten",
Expand Down
10 changes: 5 additions & 5 deletions front/src/config/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1686,8 +1686,8 @@
"label": "Select the lights you want to toggle",
"description": "This action turn on the light if state is off or turn off the light if state is on"
},
"blinkLights": {
"label": "Select the lights you want to make blink",
"blinkDevices": {
"label": "Select the lights/switches you want to make blink",
"description": "Configure blinking duration and speed",
"blinkingTime": {
"label": "Duration (in seconds)",
Expand Down Expand Up @@ -1831,7 +1831,8 @@
},
"device": {
"get-value": "Get device value",
"set-value": "Set device value"
"set-value": "Set device value",
"blink": "Make the Lights/Switches blink"
},
"message": {
"send": "Send Message",
Expand All @@ -1841,8 +1842,7 @@
"light": {
"turn-on": "Turn On the Lights",
"turn-off": "Turn Off the Lights",
"toggle": "Toggle the Lights",
"blink": "Make the Lights blink"
"toggle": "Toggle the Lights"
},
"switch": {
"turn-on": "Turn On the Switches",
Expand Down
10 changes: 5 additions & 5 deletions front/src/config/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -1688,8 +1688,8 @@
"label": "Sélectionnez les lumières que vous souhaitez inverser",
"description": "Cette action allume la lumière si éteinte, sinon éteins la lumière"
},
"blinkLights": {
"label": "Sélectionnez les lumières que vous souhaitez faire clignoter",
"blinkDevices": {
"label": "Sélectionnez les lumières/prises que vous souhaitez faire clignoter",
"description": "Réglez la durée et la vitesse de clignotement",
"blinkingTime": {
"label": "Durée (en secondes)",
Expand Down Expand Up @@ -1833,7 +1833,8 @@
},
"device": {
"get-value": "Récupérer le dernier état",
"set-value": "Contrôler un appareil"
"set-value": "Contrôler un appareil",
"blink": "Faire clignoter les lumières/prises"
},
"message": {
"send": "Envoyer un message",
Expand All @@ -1843,8 +1844,7 @@
"light": {
"turn-on": "Allumer les lumières",
"turn-off": "Eteindre les lumières",
"toggle": "Inverser les lumières",
"blink": "Faire clignoter les lumières"
"toggle": "Inverser les lumières"
},
"switch": {
"turn-on": "Allumer les prises",
Expand Down
10 changes: 5 additions & 5 deletions front/src/routes/scene/edit-scene/ActionCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import DeviceSetValue from './actions/DeviceSetValue';
import SendMessageParams from './actions/SendMessageParams';
import OnlyContinueIfParams from './actions/only-continue-if/OnlyContinueIfParams';
import TurnOnOffLightParams from './actions/TurnOnOffLightParams';
import BlinkLightParams from './actions/BlinkLightParams';
import BlinkDeviceParams from './actions/BlinkDeviceParams';
import TurnOnOffSwitchParams from './actions/TurnOnOffSwitchParams';
import StartSceneParams from './actions/StartSceneParams';
import UserPresence from './actions/UserPresence';
Expand All @@ -39,7 +39,7 @@ const ACTION_ICON = {
[ACTIONS.LIGHT.TURN_ON]: 'fe fe-toggle-right',
[ACTIONS.LIGHT.TURN_OFF]: 'fe fe-toggle-left',
[ACTIONS.LIGHT.TOGGLE]: 'fe fe-shuffle',
[ACTIONS.LIGHT.BLINK]: 'fe fe-star',
[ACTIONS.DEVICE.BLINK]: 'fe fe-star',
[ACTIONS.SWITCH.TURN_ON]: 'fe fe-toggle-right',
[ACTIONS.SWITCH.TURN_OFF]: 'fe fe-toggle-left',
[ACTIONS.SWITCH.TOGGLE]: 'fe fe-shuffle',
Expand Down Expand Up @@ -100,7 +100,7 @@ const ActionCard = ({ children, ...props }) => {
props.action.type === ACTIONS.MESSAGE.SEND ||
props.action.type === ACTIONS.CALENDAR.IS_EVENT_RUNNING ||
props.action.type === ACTIONS.MQTT.SEND ||
props.action.type === ACTIONS.LIGHT.BLINK,
props.action.type === ACTIONS.DEVICE.BLINK,
'col-lg-4':
props.action.type !== ACTIONS.CONDITION.ONLY_CONTINUE_IF &&
props.action.type !== ACTIONS.MESSAGE.SEND &&
Expand Down Expand Up @@ -176,8 +176,8 @@ const ActionCard = ({ children, ...props }) => {
updateActionProperty={props.updateActionProperty}
/>
)}
{props.action.type === ACTIONS.LIGHT.BLINK && (
<BlinkLightParams
{props.action.type === ACTIONS.DEVICE.BLINK && (
<BlinkDeviceParams
action={props.action}
columnIndex={props.columnIndex}
index={props.index}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import { connect } from 'unistore/preact';
import { Text, Localizer } from 'preact-i18n';
import Select from 'react-select';

class BlinkLight extends Component {
class BlinkDeviceParams 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 => ({
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
}));
Expand All @@ -23,8 +27,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,8 +43,8 @@ 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);
}
Expand Down Expand Up @@ -73,7 +77,7 @@ class BlinkLight extends Component {
<div class="col-sm-12">
<div class="form-group">
<div class="form-label">
<Text id="editScene.actionsCard.blinkLigths.label" />
<Text id="editScene.actionsCard.blinkDevices.label" />
</div>
<Select
defaultValue={[]}
Expand All @@ -89,37 +93,37 @@ class BlinkLight extends Component {
<div class="col-sm-6">
<div class="form-group">
<div class="form-label">
<Text id="editScene.actionsCard.blinkLights.blinkingTime.label" />
<Text id="editScene.actionsCard.blinkDevices.blinkingTime.label" />
</div>
<Localizer>
<input
type="text"
class="form-control"
value={props.action.blinking_time}
onChange={this.handleChangeBlinkingTime}
placeholder={<Text id="editScene.actionsCard.blinkLights.blinkingTime.placeholder" />}
placeholder={<Text id="editScene.actionsCard.blinkDevices.blinkingTime.placeholder" />}
/>
</Localizer>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<div class="form-label">
<Text id="editScene.actionsCard.blinkLights.blinkingSpeed.label" />
<Text id="editScene.actionsCard.blinkDevices.blinkingSpeed.label" />
</div>
<select
class="custom-select"
value={props.action.blinking_speed}
onChange={this.handleChangeBlinkingSpeed}
>
<option value="slow">
<Text id="editScene.actionsCard.blinkLights.blinkingSpeed.slow" />
<Text id="editScene.actionsCard.blinkDevices.blinkingSpeed.slow" />
</option>
<option value="medium">
<Text id="editScene.actionsCard.blinkLights.blinkingSpeed.medium" />
<Text id="editScene.actionsCard.blinkDevices.blinkingSpeed.medium" />
</option>
<option value="fast">
<Text id="editScene.actionsCard.blinkLights.blinkingSpeed.fast" />
<Text id="editScene.actionsCard.blinkDevices.blinkingSpeed.fast" />
</option>
</select>
</div>
Expand All @@ -130,4 +134,4 @@ class BlinkLight extends Component {
}
}

export default connect('httpClient', {})(BlinkLight);
export default connect('httpClient', {})(BlinkDeviceParams);
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const ACTION_LIST = [
ACTIONS.LIGHT.TURN_ON,
ACTIONS.LIGHT.TURN_OFF,
ACTIONS.LIGHT.TOGGLE,
ACTIONS.LIGHT.BLINK,
ACTIONS.DEVICE.BLINK,
ACTIONS.SWITCH.TURN_ON,
ACTIONS.SWITCH.TURN_OFF,
ACTIONS.SWITCH.TOGGLE,
Expand Down
11 changes: 9 additions & 2 deletions server/lib/scene/scene.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const actionsFunc = {
}
});
},
[ACTIONS.LIGHT.BLINK]: async (self, action, scope) => {
[ACTIONS.DEVICE.BLINK]: async (self, action, scope) => {
const blinkingSpeed = action.blinking_speed;
const blinkingTime = action.blinking_time * 1000 + 1;
let blinkingInterval;
Expand All @@ -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 @@ -8,7 +8,7 @@ const StateManager = require('../../../../lib/state');

const event = new EventEmitter();

describe('scene.blink-lights', () => {
describe('scene.blink-devices', () => {
let clock;

beforeEach(() => {
Expand Down Expand Up @@ -43,7 +43,7 @@ describe('scene.blink-lights', () => {
[
[
{
type: ACTIONS.LIGHT.BLINK,
type: ACTIONS.DEVICE.BLINK,
devices: ['light-1'],
blinking_speed: 'slow',
blinking_time: 2,
Expand Down Expand Up @@ -82,7 +82,7 @@ describe('scene.blink-lights', () => {
[
[
{
type: ACTIONS.LIGHT.BLINK,
type: ACTIONS.DEVICE.BLINK,
devices: ['light-1'],
blinking_speed: 'medium',
blinking_time: 2,
Expand Down Expand Up @@ -121,7 +121,7 @@ describe('scene.blink-lights', () => {
[
[
{
type: ACTIONS.LIGHT.BLINK,
type: ACTIONS.DEVICE.BLINK,
devices: ['light-1'],
blinking_speed: 'fast',
blinking_time: 2,
Expand All @@ -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.DEVICE.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 All @@ -160,7 +199,7 @@ describe('scene.blink-lights', () => {
[
[
{
type: ACTIONS.LIGHT.BLINK,
type: ACTIONS.DEVICE.BLINK,
devices: ['light-1'],
blinking_speed: 'unknown',
blinking_time: 2,
Expand Down Expand Up @@ -192,7 +231,7 @@ describe('scene.blink-lights', () => {
[
[
{
type: ACTIONS.LIGHT.BLINK,
type: ACTIONS.DEVICE.BLINK,
devices: ['my-device'],
blinking_speed: 'slow',
blinking_time: 1,
Expand Down
2 changes: 1 addition & 1 deletion server/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,12 @@ const ACTIONS = {
DEVICE: {
SET_VALUE: 'device.set-value',
GET_VALUE: 'device.get-value',
BLINK: 'device.blink',
},
LIGHT: {
TURN_ON: 'light.turn-on',
TURN_OFF: 'light.turn-off',
TOGGLE: 'light.toggle',
BLINK: 'light.blink',
},
SWITCH: {
TURN_ON: 'switch.turn-on',
Expand Down

0 comments on commit f09c501

Please sign in to comment.