Skip to content

Commit

Permalink
Update HueSensor.js
Browse files Browse the repository at this point in the history
- Remove _Configured Name_ characteristic, see #861;
- Add support for IKEA SHORTCUT Button, see #877;
- Add support for LIDL Livarno Lux remote;
- Support `state.test` for IAS devices;
- Code cleanup.
  • Loading branch information
ebaauw committed Jan 8, 2021
1 parent 931ee34 commit 8171c92
Showing 1 changed file with 97 additions and 54 deletions.
151 changes: 97 additions & 54 deletions lib/HueSensor.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// homebridge-hue/lib/HueSensor.js
// Copyright © 2016-2020 Erik Baauw. All rights reserved.
// Copyright © 2016-2021 Erik Baauw. All rights reserved.
//
// Homebridge plugin for Philips Hue and/or deCONZ.
//
// HueSensor provides support for Philips Hue sensors.

'use strict'

Expand Down Expand Up @@ -362,6 +360,18 @@ function HueSensor (accessory, id, obj) {
homekitValue: function (v) { return Math.floor(v / 1000) },
homekitAction: hkZLLSwitchAction
}
} else if (
this.obj.manufacturername === 'IKEA of Sweden' &&
this.obj.modelid === 'TRADFRI SHORTCUT Button'
) {
// Ikea Trådfri shortcut button
this.createLabel(Characteristic.ServiceLabelNamespace.ARABIC_NUMERALS)
this.createButton(1, 'Button', SINGLE_LONG)
this.type = {
key: 'buttonevent',
homekitValue: function (v) { return Math.floor(v / 1000) },
homekitAction: hkZLLSwitchAction
}
} else if (
this.obj.manufacturername === 'IKEA of Sweden' &&
this.obj.modelid === 'TRADFRI open/close remote'
Expand Down Expand Up @@ -891,6 +901,21 @@ function HueSensor (accessory, id, obj) {
homekitValue: function (v) { return Math.floor(v / 1000) },
homekitAction: hkZLLSwitchAction
}
} else if (
this.obj.manufacturername === 'LIDL Livarno Lux' &&
this.obj.modelid === 'HG06323'
) {
// LIDL Livarno Lux remote, see deconz-rest-plugin#3829
this.createLabel(Characteristic.ServiceLabelNamespace.ARABIC_NUMERALS)
this.createButton(1, 'On', SINGLE_DOUBLE_LONG)
this.createButton(2, 'DimUp', SINGLE_LONG)
this.createButton(3, 'DimDown', SINGLE_LONG)
this.createButton(4, 'Off', SINGLE)
this.type = {
key: 'buttonevent',
homekitValue: function (v) { return Math.floor(v / 1000) },
homekitAction: hkZLLSwitchAction
}
} else {
this.log.warn(
'%s: %s: warning: ignoring unknown %s sensor %j',
Expand Down Expand Up @@ -1336,7 +1361,7 @@ function HueSensor (accessory, id, obj) {
this.service = new my.Services.Resource(this.name, this.subtype)
} else {
this.service = this.accessory.lightService
this.noSetNameCallback = true
// this.noSetNameCallback = true
}
this.serviceList.push(this.service)
this.service
Expand All @@ -1357,7 +1382,7 @@ function HueSensor (accessory, id, obj) {
this.service = new my.Services.Resource(this.name, this.subtype)
} else {
this.service = this.accessory.lightService
this.noSetNameCallback = true
// this.noSetNameCallback = true
}
this.serviceList.push(this.service)
this.service
Expand Down Expand Up @@ -1741,15 +1766,15 @@ function HueSensor (accessory, id, obj) {
this.service.getCharacteristic(my.Characteristics.Enabled)
.on('set', this.setEnabled.bind(this))
if (
this.bridge.platform.config.resource && !this.noSetNameCallback
this.bridge.platform.config.resource // && !this.noSetNameCallback
) {
this.service.addOptionalCharacteristic(my.Characteristics.Resource)
this.service.getCharacteristic(my.Characteristics.Resource)
.updateValue(this.resource)
}
this.service.addOptionalCharacteristic(Characteristic.ConfiguredName)
this.service.getCharacteristic(Characteristic.ConfiguredName)
.on('set', this.setName.bind(this))
// this.service.addOptionalCharacteristic(Characteristic.ConfiguredName)
// this.service.getCharacteristic(Characteristic.ConfiguredName)
// .on('set', this.setName.bind(this))
}
if (this.obj.config.battery !== undefined) {
this.batteryService = this.accessory.getBatteryService(
Expand All @@ -1766,7 +1791,7 @@ HueSensor.prototype.createLabel = function (labelNamespace) {
this.accessory.labelService = this.service
} else {
this.service = this.accessory.labelService
this.noSetNameCallback = true
// this.noSetNameCallback = true
}
this.buttonMap = {}
}
Expand All @@ -1786,7 +1811,7 @@ HueSensor.prototype.createButton = function (buttonIndex, buttonName, props) {
// ===== Bridge Events =========================================================

HueSensor.prototype.heartbeat = function (beat, obj) {
this.checkName(obj.name)
// this.checkName(obj.name)
if (
obj.state.daylight != null &&
obj.state.lightlevel == null && obj.state.status == null
Expand All @@ -1809,9 +1834,9 @@ HueSensor.prototype.checkAttr = function (attr, event) {
break
case 'lastseen':
break
case 'name':
this.checkName(attr.name)
break
// case 'name':
// this.checkName(attr.name)
// break
default:
break
}
Expand Down Expand Up @@ -1869,6 +1894,11 @@ HueSensor.prototype.checkState = function (state, event) {
case 'tampered':
this.checkTampered(state.tampered)
break
case 'test':
if (state.test) {
state[this.type.key] = true
}
break
case 'tiltangle':
break
case 'valve':
Expand Down Expand Up @@ -2593,26 +2623,26 @@ HueSensor.prototype.checkMode = function (mode) {
}
}

HueSensor.prototype.checkName = function (name) {
if (this.obj.name !== name && name.trim() !== '') {
this.log.debug(
'%s: name changed from %s to %s', this.name, this.obj.name, name
)
this.obj.name = name
}
const hkName = this.obj.name
if (this.hk.name !== hkName) {
if (this.hk.name !== undefined) {
this.log.info(
'%s: set homekit name from %s to %s', this.name, this.hk.name, hkName
)
}
this.hk.name = hkName
this.service.getCharacteristic(Characteristic.ConfiguredName)
.updateValue(hkName)
this.name = this.hk.name
}
}
// HueSensor.prototype.checkName = function (name) {
// if (this.obj.name !== name) {
// this.log.debug(
// '%s: name changed from %j to %j', this.name, this.obj.name, name
// )
// this.obj.name = name
// }
// const hkName = this.obj.name
// if (this.hk.name !== hkName) {
// if (this.hk.name !== undefined) {
// this.log.info(
// '%s: set homekit name from %j to %j', this.name, this.hk.name, hkName
// )
// }
// this.hk.name = hkName
// this.service.getCharacteristic(Characteristic.ConfiguredName)
// .updateValue(hkName)
// this.name = this.hk.name
// }
// }

HueSensor.prototype.checkOffset = function (offset) {
if (this.obj.config.offset !== offset) {
Expand Down Expand Up @@ -2838,25 +2868,38 @@ HueSensor.prototype.setMirroring = function (mirroring, callback) {
})
}

HueSensor.prototype.setName = function (name, callback) {
if (this.noSetNameCallback) {
callback = () => {}
}
if (name === this.hk.name) {
return callback()
}
this.log.info(
'%s: homekit name changed from %s to %s', this.name, this.hk.name, name
)
this.hk.name = name
this.put('', { name: name }).then((obj) => {
this.obj.name = name
this.name = name
return callback()
}).catch((error) => {
return callback(error)
})
}
// HueSensor.prototype.setName = function (name, callback) {
// if (this.noSetNameCallback) {
// callback = () => {}
// }
// if (name === this.hk.name) {
// return callback()
// }
// name = name.trim() // .slice(0, 32).trim()
// if (name === '') {
// return callback(new Error())
// }
// this.log.info(
// '%s: homekit name changed from %j to %j', this.name, this.hk.name, name
// )
// this.put('', { name: name }).then((obj) => {
// if (obj.name == null) {
// this.obj.name = name
// this.hk.name = name
// return callback(new Error())
// }
// this.obj.name = obj.name
// this.name = obj.name
// setImmediate(() => {
// this.hk.name = name
// this.service.getCharacteristic(Characteristic.ConfiguredName)
// .updateValue(this.hk.name)
// })
// return callback()
// }).catch((error) => {
// return callback(error)
// })
// }

HueSensor.prototype.setOffset = function (offset, callback) {
if (offset === this.hk.offset) {
Expand Down Expand Up @@ -3127,5 +3170,5 @@ HueSensor.prototype.setSensitivity = function (sensitivity, callback) {
}

HueSensor.prototype.put = function (resource, body) {
return this.bridge.request('put', this.resource + resource, body)
return this.bridge.put(this.resource + resource, body)
}

0 comments on commit 8171c92

Please sign in to comment.