Skip to content

Commit

Permalink
allow finding devices by hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
alexh-name committed Jan 26, 2021
1 parent 8e6b15d commit a0465aa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
10 changes: 10 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
"title": "Device IP Address",
"type": "string"
},
"hostname": {
"title": "Device Hostname",
"type": "string"
},
"threshold": {
"title": "Disconnect Threshold (minutes)",
"description": "Time in minutes to wait before updating 'disconnected' status",
Expand All @@ -78,6 +82,11 @@
"required": [
"ip"
]
},
{
"required": [
"hostname"
]
}
]
}
Expand All @@ -104,6 +113,7 @@
"devices[].name",
"devices[].mac",
"devices[].ip",
"devices[].hostname",
"devices[].threshold"
]
},
Expand Down
15 changes: 8 additions & 7 deletions lib/accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@ class OccupancySensor {
this.threshold = !config.threshold && config.threshold !== 0 ? platform.threshold : config.threshold
this.mac = config.mac ? config.mac.toLowerCase() : null
this.ip = config.ip
this.hostname = config.hostname ? config.hostname.toLowerCase() : null
this.name = config.name
this.model = 'ARP-network-scanner'
this.serial = this.mac || this.ip
this.serial = this.mac || this.ip || this.hostname
this.manufacturer = '@nitaybz'
this.displayName = this.name

this.thresholdTimer = null

if (!this.serial) {
this.log(`Can't initiate ${this.name} device without mac address or ip address`)
this.log(`Can't initiate ${this.name} device without mac address, ip address or hostname`)
this.log(`Please change your config`)
return
}

if (typeof this.serial !== 'string') {
this.log(`Wrong mac/ip address format`)
this.log(`Please adjust your config to include proper ip address (10.0.0.x) or mac address (3e:34:ae:87:f1:cc)`)
this.log(`Wrong mac/ip address/hostname format`)
this.log(`Please adjust your config to include proper ip address (10.0.0.x), mac address (3e:34:ae:87:f1:cc) or hostname (joes-iphone.local)`)
return
}

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

const listenTo = this.mac ? `mac:${this.mac}` : `ip:${this.ip}`
const listenTo = this.mac ? `mac:${this.mac}` : this.ip ? `ip:${this.ip}` : `hostname:${this.hostname}`
this.log.easyDebug(`[${this.name}] - Listening to ${listenTo}`)

this.network.on(`net-connected:${listenTo}`, (device) =>
Expand All @@ -91,7 +92,7 @@ class OccupancySensor {
setDetected(isDetected, device) {
clearTimeout(this.thresholdTimer)
if (isDetected && !this.isDetected) {
this.log(`[${this.name}] - connected to the network (mac: ${device.mac} | ip:${device.ip})`)
this.log(`[${this.name}] - connected to the network (mac: ${device.mac} | ip:${device.ip} | hostname:${device.name})`)
this.isDetected = 1
this.OccupancySensorService
.getCharacteristic(Characteristic.OccupancyDetected)
Expand All @@ -100,7 +101,7 @@ class OccupancySensor {
}

this.thresholdTimer = setTimeout(() => {
this.log(`[${this.name}] - disconnected from the network (mac: ${device.mac} | ip:${device.ip})`)
this.log(`[${this.name}] - disconnected from the network (mac: ${device.mac} | ip:${device.ip} | hostname:${device.name})`)
this.isDetected = 0
this.OccupancySensorService
.getCharacteristic(Characteristic.OccupancyDetected)
Expand Down
5 changes: 4 additions & 1 deletion lib/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,20 @@ class NetworkObserver extends EventEmitter {
}
this.emit(`net-device:mac:${device.mac}`, device);
this.emit(`net-device:ip:${device.ip}`, device);
this.emit(`net-device:hostname:${device.name}`, device);
});
// Check if device disconnected
const removedDevices = previousDeviceKeys.map(key => cacheMap[key]);
// Emit events
newDevices.forEach(device => {
this.emit(`net-connected:mac:${device.mac}`, device);
this.emit(`net-connected:ip:${device.ip}`, device);
this.emit(`net-connected:hostname:${device.name}`, device);
});
removedDevices.forEach(device => {
this.emit(`net-disconnected:mac:${device.mac}`, device);
this.emit(`net-disconnected:ip:${device.ip}`, device);
this.emit(`net-disconnected:hostname:${device.name}`, device);
});
// Save cache
this.cachedDevices = devices;
Expand All @@ -110,7 +113,7 @@ const removeCachedDevices = function() {
if (accessory.context.serial === '12:34:56:78:9a:bc' && this.anyoneSensor)
return

const deviceInConfig = this.devicesConfig.find(device => (device.mac && device.mac.toLowerCase() === accessory.context.serial) || device.ip === accessory.context.serial)
const deviceInConfig = this.devicesConfig.find(device => (device.mac && device.mac.toLowerCase() === accessory.context.serial) || device.ip === accessory.context.serial || device.hostname && device.hostname.toLowerCase() === accessory.context.serial)
if (deviceInConfig)
return
else {
Expand Down

0 comments on commit a0465aa

Please sign in to comment.