Skip to content

Commit

Permalink
fixes for anyone sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
nitaybz committed Dec 20, 2020
1 parent 6feaa12 commit f70624c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
14 changes: 7 additions & 7 deletions lib/accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class OccupancySensor {
this.network = network
this.log = platform.log
this.api = platform.api
this.platformAccessories = platform.accessories
this.platformDevices = platform.devices
this.interval = platform.interval
this.threshold = !config.threshold && config.threshold !== 0 ? platform.threshold : config.threshold
this.mac = config.mac ? config.mac.toLowerCase() : null
Expand All @@ -34,7 +34,7 @@ class OccupancySensor {
}

this.UUID = this.api.hap.uuid.generate(this.serial)
this.accessory = this.platformAccessories.find(accessory => accessory.UUID === this.UUID)
this.accessory = platform.accessories.find(accessory => accessory.UUID === this.UUID)

if (!this.accessory) {
this.log(`Creating New ${platform.PLATFORM_NAME} Accessory for ${this.name}`)
Expand All @@ -60,7 +60,7 @@ class OccupancySensor {
.setCharacteristic(Characteristic.SerialNumber, this.serial)

if (config.anyone)
this.addAnyoneSensor
this.addAnyoneSensor()
else
this.addOccupancySensor()

Expand Down Expand Up @@ -122,15 +122,15 @@ class OccupancySensor {
.on('get', (callback) => callback(null, this.isDetected))
.updateValue(this.isDetected)

setInterval(() => {
this.updateAnyone()
}, this.interval)
setInterval(() => {
this.updateAnyone()
}, this.interval)
}

updateAnyone() {

// check if any accessory isDetected ( === 1 )
const isDetected = this.platformAccessories.find(pAccessory => pAccessory.isDetected)
const isDetected = this.platformDevices.find(pDevice => pDevice.isDetected)
if (isDetected && !this.isDetected) {
this.log(`[${this.name}] - Someone connected to the network`)
this.isDetected = 1
Expand Down
8 changes: 6 additions & 2 deletions lib/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const init = function() {

this.devicesConfig.forEach(device => this.devices.push(new Accessory(net, device, this)))
if (this.anyoneSensor)
this.devices.push(new Accessory(net, {anyone: true, mac: '12:34:56:78:9a:bc', name: 'Anyone'}, this))
new Accessory(net, {anyone: true, mac: '12:34:56:78:9a:bc', name: 'Anyone'}, this)

}

Expand Down Expand Up @@ -141,12 +141,16 @@ module.exports = {

const removeCachedDevices = function() {
this.accessories.forEach(accessory => {
// if anyone sensor
if (accessory.context.serial === '12:34:56:78:9a:bc' && this.anyoneSensor)
return

const deviceInConfig = this.devicesConfig.find(device => device.mac.toLowerCase() === accessory.context.serial || device.ip === accessory.context.serial)
if (deviceInConfig)
return
else {
// unregistering accessory
this.log(`Unregistering disconnected device: "${accessory.displayName}" (${accessory.context.serial}`)
this.log(`Unregistering disconnected device: "${accessory.displayName}" (${accessory.context.serial})`)
this.api.unregisterPlatformAccessories(this.PLUGIN_NAME, this.PLATFORM_NAME, [accessory])
}

Expand Down

0 comments on commit f70624c

Please sign in to comment.