diff --git a/server/services/lan-manager/lib/lan-manager.scanPresence.js b/server/services/lan-manager/lib/lan-manager.scanPresence.js index 5a401528fd..9aea8c7e78 100644 --- a/server/services/lan-manager/lib/lan-manager.scanPresence.js +++ b/server/services/lan-manager/lib/lan-manager.scanPresence.js @@ -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, + }); + } } }); } diff --git a/server/test/services/lan-manager/lib/lan-manager.scanPresence.test.js b/server/test/services/lan-manager/lib/lan-manager.scanPresence.test.js index 9ad91815ff..7854075a51 100644 --- a/server/test/services/lan-manager/lib/lan-manager.scanPresence.test.js +++ b/server/test/services/lan-manager/lib/lan-manager.scanPresence.test.js @@ -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: { @@ -30,6 +30,10 @@ describe('LANManager scanPresence', () => { }, ]; + gladys.stateManager = { + get: fake.returns({ last_value: 0 }), + }; + gladys.event = { emit: fake.returns(true), }; @@ -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: [ @@ -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();