Skip to content
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

Scene check user presence action: add userSeen #1895

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions server/lib/scene/scene.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,19 @@ const actionsFunc = {
deviceSeenRecently = true;
}
});
// if no device was seen, the user has left home
if (deviceSeenRecently === false) {

const user = self.stateManager.get('user', action.user);
if (deviceSeenRecently) {
if (user.current_house_id === null) {
// if at least one device was seen, the user is at home
logger.info(
`CheckUserPresence action: At least one device of the user "${action.user}" were seen in the last ${action.minutes} minutes.`,
);
logger.info(`CheckUserPresence action: Set "${action.user}" to seen in house "${action.house}"`);
await self.house.userSeen(action.house, action.user);
}
} else if (user.current_house_id !== null) {
// if no device was seen, the user has left home
logger.info(
`CheckUserPresence action: No devices of the user "${action.user}" were seen in the last ${action.minutes} minutes.`,
);
Expand Down
77 changes: 73 additions & 4 deletions server/test/lib/scene/scene.executeActions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,10 +685,74 @@ describe('scene.executeActions', () => {
);
assert.calledWith(house.userLeft, 'my-house', 'john');
});
it('should execute action user.checkPresence and not call userLeft because user was seen', async () => {

it('should execute action user.checkPresence and user left home', async () => {
stateManager.setState('deviceFeature', 'my-device', {
last_value_changed: Date.now() - 15 * 60 * 1000,
});
stateManager.setState('user', 'john', {
current_house_id: 'house-id',
});
const house = {
userSeen: fake.resolves(null),
userLeft: fake.resolves(null),
};
const scope = {};
await executeActions(
{ stateManager, event, house },
[
[
{
type: ACTIONS.USER.CHECK_PRESENCE,
user: 'john',
house: 'my-house',
minutes: 10,
device_features: ['my-device'],
},
],
],
scope,
);
assert.notCalled(house.userSeen);
assert.calledWith(house.userLeft, 'my-house', 'john');
});
it('should execute action user.checkPresence and user still out home', async () => {
stateManager.setState('deviceFeature', 'my-device', {
last_value_changed: Date.now() - 15 * 60 * 1000,
});
stateManager.setState('user', 'john', {
current_house_id: null,
});
const house = {
userSeen: fake.resolves(null),
userLeft: fake.resolves(null),
};
const scope = {};
await executeActions(
{ stateManager, event, house },
[
[
{
type: ACTIONS.USER.CHECK_PRESENCE,
user: 'john',
house: 'my-house',
minutes: 10,
device_features: ['my-device'],
},
],
],
scope,
);
assert.notCalled(house.userSeen);
assert.notCalled(house.userLeft);
});
it('should execute action user.checkPresence and user back home', async () => {
stateManager.setState('deviceFeature', 'my-device', {
last_value_changed: Date.now(),
});
stateManager.setState('user', 'john', {
current_house_id: null,
});
const house = {
userSeen: fake.resolves(null),
userLeft: fake.resolves(null),
Expand All @@ -709,11 +773,15 @@ describe('scene.executeActions', () => {
],
scope,
);
assert.calledWith(house.userSeen, 'my-house', 'john');
assert.notCalled(house.userLeft);
});
it('should execute action user.checkPresence and call userLeft because user was not seen', async () => {
it('should execute action user.checkPresence and user still at home', async () => {
stateManager.setState('deviceFeature', 'my-device', {
last_value_changed: Date.now() - 15 * 60 * 1000,
last_value_changed: Date.now(),
});
stateManager.setState('user', 'john', {
current_house_id: 'house-id',
});
const house = {
userSeen: fake.resolves(null),
Expand All @@ -735,7 +803,8 @@ describe('scene.executeActions', () => {
],
scope,
);
assert.calledWith(house.userLeft, 'my-house', 'john');
assert.notCalled(house.userSeen);
assert.notCalled(house.userLeft);
});

it('should abort scene, house empty is not verified', async () => {
Expand Down