Skip to content

Commit

Permalink
feat: add support for newer magnet and motion devices and add support…
Browse files Browse the repository at this point in the history
… for cube (#9)

* new motion sensor with light intensity measurements

* added support for newer magnet sensors

* added support for the cube
  • Loading branch information
Raidok authored and marvinroger committed Oct 5, 2017
1 parent 1a0d3f4 commit 03d2020
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
10 changes: 8 additions & 2 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ aqara.on('gateway', (gateway) => {
case 'motion':
console.log(` Motion (${device.hasMotion() ? 'motion' : 'no motion'})`)
device.on('motion', () => {
console.log(`${device.getSid()} has motion`)
console.log(`${device.getSid()} has motion${device.getLux() !== null ? ' (lux:' + device.getLux() + ')' : ''}`)
})
device.on('noMotion', () => {
console.log(`${device.getSid()} has no motion (${device.getSecondsSinceMotion()})`)
console.log(`${device.getSid()} has no motion (inactive:${device.getSecondsSinceMotion()}${device.getLux() !== null ? ' lux:' + device.getLux() : ''})`)
})
break
case 'sensor':
Expand All @@ -66,6 +66,12 @@ aqara.on('gateway', (gateway) => {
console.log(`${device.getSid()}${device.isLeaking() ? '' : ' not'} leaking`)
})
break
case 'cube':
console.log(` Cube`)
device.on('update', () => {
console.log(`${device.getSid()} ${device.getStatus()}${device.getRotateDegrees() !== null ? ' ' + device.getRotateDegrees() : ''}`)
})
break
}
})

Expand Down
34 changes: 34 additions & 0 deletions src/lib/cube.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const Subdevice = require('./subdevice')

class Cube extends Subdevice {
constructor (opts) {
super({ sid: opts.sid, type: 'cube' })

this._status = null
this._rotateDegrees = null
}

_handleState (state) {
super._handleState(state)

if ('rotate' in state) {
this._status = 'rotate'
this._rotateDegrees = state.rotate
 } else {
this._status = state.status
this._rotateDegrees = null
}

this.emit('update')

}

getStatus () {
return this._status
}
getRotateDegrees () {
return this._rotateDegrees
}
}

module.exports = Cube
6 changes: 6 additions & 0 deletions src/lib/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Switch = require('./switch')
const Motion = require('./motion')
const Sensor = require('./sensor')
const Leak = require('./leak')
const Cube = require('./cube')

class Gateway extends events.EventEmitter {
constructor (opts) {
Expand Down Expand Up @@ -64,12 +65,14 @@ class Gateway extends events.EventEmitter {
let subdevice
switch (type) {
case 'magnet':
case 'sensor_magnet.aq2':
subdevice = new Magnet({ sid })
break
case 'switch':
subdevice = new Switch({ sid })
break
case 'motion':
case 'sensor_motion.aq2':
subdevice = new Motion({ sid })
break
case 'sensor_ht':
Expand All @@ -79,6 +82,9 @@ class Gateway extends events.EventEmitter {
case 'sensor_wleak.aq1':
subdevice = new Leak({ sid })
break
case 'cube':
subdevice = new Cube({ sid })
break
default:
return false
}
Expand Down
22 changes: 15 additions & 7 deletions src/lib/motion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,34 @@ class Motion extends Subdevice {
super({ sid: opts.sid, type: 'motion' })

this._motion = null
this._lux = null
this._seconds = null
}

_handleState (state) {
super._handleState(state)

// when motion is detected then json contains only 'status' field with this specific value
this._motion = state.status === 'motion'
// in case of inactivity, json contains only 'no_motion' field
// with seconds from last motion as the value (reports '120', '180', '300', '600', '1200' and finally '1800')
this._seconds = state.no_motion
// message with lux value comes separately and seems to arrive before motion messages
if (state.lux) this._lux = state.lux

if (this._motion) this.emit('motion')
else if (state.no_motion) this.emit('noMotion')
if ('status' in state || 'no_motion' in state) {
// when motion is detected then json contains only 'status' field with this specific value
this._motion = state.status === 'motion'
// in case of inactivity, json contains only 'no_motion' field
// with seconds from last motion as the value (reports '120', '180', '300', '600', '1200' and finally '1800')
this._seconds = state.no_motion

if (this._motion) this.emit('motion')
else if (state.no_motion) this.emit('noMotion')
}
}

hasMotion () {
return this._motion
}
getLux () {
return this._lux
}
getSecondsSinceMotion () {
return this._seconds
}
Expand Down

0 comments on commit 03d2020

Please sign in to comment.