Skip to content

Commit

Permalink
LAN Manager: Should save a 0 once if a device is not discovered (#2164)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles authored Nov 22, 2024
1 parent 7a24976 commit 4f31ae6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
8 changes: 8 additions & 0 deletions server/services/lan-manager/lib/lan-manager.scanPresence.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ async function scanPresence() {
device_feature_external_id: externalId,
state: 1,
});
} else {
const deviceFeatureInCache = this.gladys.stateManager.get('deviceFeatureByExternalId', externalId);
if (deviceFeatureInCache.last_value !== 0) {
this.gladys.event.emit(EVENTS.DEVICE.NEW_STATE, {
device_feature_external_id: externalId,
state: 0,
});
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const scanMock = stub();
const LANManager = proxyquire('../../../../services/lan-manager/lib', {
'./lan-manager.scan': { scan: scanMock },
});
const { DEVICE_FEATURE_CATEGORIES } = require('../../../../utils/constants');
const { EVENTS, DEVICE_FEATURE_CATEGORIES } = require('../../../../utils/constants');

const gladys = {
event: {
Expand All @@ -30,6 +30,10 @@ describe('LANManager scanPresence', () => {
},
];

gladys.stateManager = {
get: fake.returns({ last_value: 0 }),
};

gladys.event = {
emit: fake.returns(true),
};
Expand Down Expand Up @@ -66,7 +70,7 @@ describe('LANManager scanPresence', () => {
assert.notCalled(gladys.event.emit);
});

it('scanPresence presence device not discovered', async () => {
it('scanPresence presence device not discovered, should save value 0', async () => {
gladys.device.get = fake.resolves([
{
features: [
Expand All @@ -78,6 +82,43 @@ describe('LANManager scanPresence', () => {
},
]);

gladys.stateManager = {
get: fake.returns({
last_value: 1,
}),
};

scanMock.resolves([]);
await manager.scanPresence();

assert.calledOnceWithExactly(gladys.device.get, {
service: 'lan-manager',
device_feature_category: DEVICE_FEATURE_CATEGORIES.PRESENCE_SENSOR,
});
assert.calledWith(gladys.event.emit, EVENTS.DEVICE.NEW_STATE, {
device_feature_external_id: 'lan-manager:unknown-uuid',
state: 0,
});
});

it('scanPresence presence device not discovered, should not save value 0 as last value is 0', async () => {
gladys.device.get = fake.resolves([
{
features: [
{
external_id: 'lan-manager:unknown-uuid',
category: DEVICE_FEATURE_CATEGORIES.PRESENCE_SENSOR,
},
],
},
]);

gladys.stateManager = {
get: fake.returns({
last_value: 0,
}),
};

scanMock.resolves([]);
await manager.scanPresence();

Expand Down

0 comments on commit 4f31ae6

Please sign in to comment.