-
-
Notifications
You must be signed in to change notification settings - Fork 317
Add "Enable a scene" and "Disable a scene" scene actions #2928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Pierre-Gilles
wants to merge
2
commits into
master
Choose a base branch
from
claude/scene-enable-disable-action
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
front/src/routes/scene/edit-scene/actions/EnableDisableSceneParams.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { Component } from 'preact'; | ||
| import { connect } from 'unistore/preact'; | ||
| import { Text } from 'preact-i18n'; | ||
| import Select from 'react-select'; | ||
|
|
||
| import { ACTIONS } from '../../../../../../server/utils/constants'; | ||
| import actions from '../../../../actions/scene'; | ||
|
|
||
| class EnableDisableSceneParams extends Component { | ||
| handleChange = selectedOption => { | ||
| if (selectedOption) { | ||
| this.props.updateActionProperty(this.props.path, 'scene', selectedOption.value); | ||
| } else { | ||
| this.props.updateActionProperty(this.props.path, 'scene', null); | ||
| } | ||
| }; | ||
|
|
||
| refreshSelectedOptions = nextProps => { | ||
| let selectedOption = null; | ||
| let scenes = this.state.scenes || []; | ||
|
|
||
| // Contrary to the "start a scene" action, the current scene is part of the list: | ||
| // a scene disabling itself is the way to build a "run once, then disarm" scene. | ||
| if (scenes.length === 0 && nextProps.scenes) { | ||
| scenes = nextProps.scenes.map(scene => ({ | ||
| value: scene.selector, | ||
| label: scene.name | ||
| })); | ||
| } | ||
|
|
||
| if (nextProps.action.scene && scenes.length > 0) { | ||
| selectedOption = scenes.find(scene => scene.value === nextProps.action.scene) || null; | ||
| } | ||
|
|
||
| this.setState({ selectedOption, scenes }); | ||
| }; | ||
|
|
||
| constructor(props) { | ||
| super(props); | ||
| this.state = { | ||
| selectedOption: null | ||
| }; | ||
| } | ||
|
|
||
| async componentDidMount() { | ||
| await this.props.getScenes(); | ||
| } | ||
|
|
||
| componentWillReceiveProps(nextProps) { | ||
| this.refreshSelectedOptions(nextProps); | ||
| } | ||
|
|
||
| render(props, { selectedOption, scenes }) { | ||
| const enabling = props.action.type === ACTIONS.SCENE.ENABLE; | ||
| return ( | ||
| <div class="form-group"> | ||
| <div class="alert alert-info"> | ||
| {enabling && <Text id="editScene.actionsCard.setSceneActive.enableNotice" />} | ||
| {!enabling && <Text id="editScene.actionsCard.setSceneActive.disableNotice" />} | ||
| </div> | ||
| <label class="form-label"> | ||
| <Text id="editScene.actionsCard.setSceneActive.label" /> | ||
| </label> | ||
| <Select | ||
| value={selectedOption} | ||
| onChange={this.handleChange} | ||
| options={scenes} | ||
| className="react-select-container" | ||
| classNamePrefix="react-select" | ||
| /> | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export default connect('scenes', actions)(EnableDisableSceneParams); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
server/test/lib/scene/actions/scene.action.enableDisableScene.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| const sinon = require('sinon').createSandbox(); | ||
|
|
||
| const { fake, assert } = sinon; | ||
| const EventEmitter = require('events'); | ||
| const chai = require('chai'); | ||
| const chaiAsPromised = require('chai-as-promised'); | ||
|
|
||
| const { ACTIONS } = require('../../../../utils/constants'); | ||
| const executeActionsFactory = require('../../../../lib/scene/scene.executeActions'); | ||
| const actionsFunc = require('../../../../lib/scene/scene.actions'); | ||
| const { AbortScene, NotFoundError } = require('../../../../utils/coreErrors'); | ||
|
|
||
| const StateManager = require('../../../../lib/state'); | ||
| const SceneManager = require('../../../../lib/scene'); | ||
| const db = require('../../../../models'); | ||
|
|
||
| chai.use(chaiAsPromised); | ||
|
|
||
| const { expect } = chai; | ||
|
|
||
| describe('scene.enable / scene.disable', () => { | ||
| const { executeActions } = executeActionsFactory(actionsFunc); | ||
| let event; | ||
| let stateManager; | ||
|
|
||
| beforeEach(() => { | ||
| event = new EventEmitter(); | ||
| stateManager = new StateManager(event); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| sinon.reset(); | ||
| }); | ||
|
|
||
| it('should enable another scene', async () => { | ||
| const update = fake.resolves({ selector: 'my-other-scene', active: true }); | ||
| const scope = {}; | ||
| await executeActions( | ||
| { stateManager, event, update }, | ||
| [ | ||
| [ | ||
| { | ||
| type: ACTIONS.SCENE.ENABLE, | ||
| scene: 'my-other-scene', | ||
| }, | ||
| ], | ||
| ], | ||
| scope, | ||
| ); | ||
| assert.calledOnceWithExactly(update, 'my-other-scene', { active: true }); | ||
| }); | ||
|
|
||
| it('should disable another scene', async () => { | ||
| const update = fake.resolves({ selector: 'my-other-scene', active: false }); | ||
| const scope = {}; | ||
| await executeActions( | ||
| { stateManager, event, update }, | ||
| [ | ||
| [ | ||
| { | ||
| type: ACTIONS.SCENE.DISABLE, | ||
| scene: 'my-other-scene', | ||
| }, | ||
| ], | ||
| ], | ||
| scope, | ||
| ); | ||
| assert.calledOnceWithExactly(update, 'my-other-scene', { active: false }); | ||
| }); | ||
|
|
||
| it('should allow a scene to disable itself', async () => { | ||
| const update = fake.resolves({ selector: 'my-scene', active: false }); | ||
| const scope = {}; | ||
| await executeActions( | ||
| { stateManager, event, update }, | ||
| [ | ||
| [ | ||
| { | ||
| type: ACTIONS.SCENE.DISABLE, | ||
| scene: 'my-scene', | ||
| }, | ||
| ], | ||
| ], | ||
| scope, | ||
| ); | ||
| assert.calledOnceWithExactly(update, 'my-scene', { active: false }); | ||
| }); | ||
|
|
||
| it('should abort the scene when no scene is selected', async () => { | ||
| const update = fake.resolves(null); | ||
| const scope = {}; | ||
| const promise = executeActions( | ||
| { stateManager, event, update }, | ||
| [ | ||
| [ | ||
| { | ||
| type: ACTIONS.SCENE.ENABLE, | ||
| }, | ||
| ], | ||
| ], | ||
| scope, | ||
| ); | ||
| await expect(promise).to.be.rejectedWith(AbortScene, 'SCENE_NOT_FOUND'); | ||
| assert.notCalled(update); | ||
| }); | ||
|
|
||
| it('should abort the scene when the selected scene does not exist anymore', async () => { | ||
| const update = fake.rejects(new NotFoundError('Scene not found')); | ||
| const scope = {}; | ||
| const promise = executeActions( | ||
| { stateManager, event, update }, | ||
| [ | ||
| [ | ||
| { | ||
| type: ACTIONS.SCENE.DISABLE, | ||
| scene: 'deleted-scene', | ||
| }, | ||
| ], | ||
| ], | ||
| scope, | ||
| ); | ||
| await expect(promise).to.be.rejectedWith(AbortScene, 'SCENE_NOT_FOUND'); | ||
| assert.calledOnceWithExactly(update, 'deleted-scene', { active: false }); | ||
| }); | ||
|
|
||
| it('should persist the active flag of the target scene in database', async () => { | ||
| const brain = { addNamedEntity: fake.returns(null), removeNamedEntity: fake.returns(null) }; | ||
| const sceneManager = new SceneManager({}, event, {}, {}, {}, {}, {}, {}, {}, {}, brain); | ||
| await executeActions(sceneManager, [[{ type: ACTIONS.SCENE.DISABLE, scene: 'test-scene' }]], {}); | ||
| let sceneInDb = await db.Scene.findOne({ where: { selector: 'test-scene' } }); | ||
| expect(sceneInDb.active).to.equal(false); | ||
| await executeActions(sceneManager, [[{ type: ACTIONS.SCENE.ENABLE, scene: 'test-scene' }]], {}); | ||
| sceneInDb = await db.Scene.findOne({ where: { selector: 'test-scene' } }); | ||
| expect(sceneInDb.active).to.equal(true); | ||
| }); | ||
|
|
||
| it('should propagate an unexpected error coming from the update', async () => { | ||
| const update = fake.rejects(new Error('DATABASE_ERROR')); | ||
| const promise = actionsFunc[ACTIONS.SCENE.ENABLE]( | ||
| { stateManager, event, update }, | ||
| { type: ACTIONS.SCENE.ENABLE, scene: 'my-other-scene' }, | ||
| {}, | ||
| ); | ||
| await expect(promise).to.be.rejectedWith(Error, 'DATABASE_ERROR'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.