Skip to content

Commit

Permalink
added onTime to deal with #193
Browse files Browse the repository at this point in the history
  • Loading branch information
thkl committed Oct 5, 2020
1 parent a0542e1 commit 26d9dfd
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 16 deletions.
5 changes: 4 additions & 1 deletion lib/configurationsrv/html/assets/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,10 @@
"Show measured values as graph on the frontpage":"Zeigt einen Graph des Messwertes auf der Startseite an",
"Unlock mode":"Unlock Modus",
"What to do when HomeKit will unlock the door":"Was soll getan werden, wenn Homekit die Tür aufschliesst",
"This service provides a filling sensor":"Dieser Service stellt einen Füllstandssensor bereit."
"This service provides a filling sensor":"Dieser Service stellt einen Füllstandssensor bereit.",
"On Time":"Einschaltzeit",
"HAP will switch off this device automatically after the given seconds. Set this to 0 to turn off this feature.":
"HAP wird das Gerät automatisch nach der angegebenen Anzahl der Sekunden ausschalten. Stelle den Wert auf 0 um die Funktion zu deaktivieren."
}


25 changes: 24 additions & 1 deletion lib/services/HomeMaticDimmerAccessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class HomeMaticDimmerAccessory extends HomeMaticAccessory {
let settings = this.getDeviceSettings()
this.useRampTime = settings.useRampTime || false
this.rampTime = settings.rampTime || 500
this.onTime = this.getDeviceSettings().OnTime

self.debugLog('Init light RampTime %s onTime %s', this.rampTime, this.onTime)

this.lightBulbService = this.getService(Service.Lightbulb)

Expand All @@ -55,9 +58,15 @@ class HomeMaticDimmerAccessory extends HomeMaticAccessory {
.on('set', (value, callback) => {
self.debugLog('set ON %s', value)
self.isWorking = true

self.lightWasTurnedOn = value
clearTimeout(self.timer)
self.timer = setTimeout(async () => {
self.debugLog('OnTime is %s LWTO is %s', self.onTime, self.lightWasTurnedOn)
if ((self.lightWasTurnedOn === true) && (self.onTime !== undefined) && (parseInt(self.onTime) > 0)) {
self.debugLog('set OnTime to %s', self.onTime)
await self.setValue('ON_TIME', self.onTime)
}

if (self.useRampTime === true) {
await self.setValueForDataPointNameWithSettingsKey('ramp', null, parseFloat(self.rampTime) / 1000)
}
Expand All @@ -71,6 +80,7 @@ class HomeMaticDimmerAccessory extends HomeMaticAccessory {
self.setValueForDataPointNameWithSettingsKey('level', null, self.oldLevel)
}
}, self.delayOnSet)
self.debugLog('LWTO is %s', self.lightWasTurnedOn)
if (callback) {
callback()
}
Expand All @@ -93,6 +103,13 @@ class HomeMaticDimmerAccessory extends HomeMaticAccessory {
clearTimeout(self.timer)
self.timer = setTimeout(async () => {
self.debugLog('set bn %s', hkvalue)
// do this just once
self.debugLog('OnTime is %s LWTO is %s', self.onTime, self.lightWasTurnedOn)
if ((self.lightWasTurnedOn === true) && (self.onTime !== undefined) && (parseInt(self.onTime) > 0)) {
self.debugLog('set OnTime to %s', self.onTime)
await self.setValue('ON_TIME', self.onTime)
}
self.lightWasTurnedOn = false
self.isWorking = true
if (self.useRampTime === true) {
await self.setValueForDataPointNameWithSettingsKey('ramp', null, parseFloat(self.rampTime) / 1000)
Expand Down Expand Up @@ -165,6 +182,12 @@ class HomeMaticDimmerAccessory extends HomeMaticAccessory {
default: 500,
label: 'Ramp time in ms',
hint: 'uses a dimmer ramp time to slowly set the new level'
},
'OnTime': {
type: 'number',
default: 0,
label: 'On Time',
hint: 'HAP will switch off this device automatically after the given seconds. Set this to 0 to turn off this feature.'
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/services/HomeMaticIPPowerMeterSwitchAccessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class HomeMaticIPPowerMeterSwitchAccessory extends HomeMaticPowerMeterSwitchAcce
'HmIP-BSM': {
roChannel: 4,
switch: '4.STATE',
ontime: '4.ON_TIME',
power: 'POWER',
current: 'CURRENT',
voltage: 'VOLTAGE',
Expand All @@ -46,6 +47,7 @@ class HomeMaticIPPowerMeterSwitchAccessory extends HomeMaticPowerMeterSwitchAcce
'*': {
roChannel: 3,
switch: '3.STATE',
ontime: '3.ON_TIME',
power: 'POWER',
current: 'CURRENT',
voltage: 'VOLTAGE',
Expand Down
27 changes: 20 additions & 7 deletions lib/services/HomeMaticPowerMeterSwitchAccessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const HomeMaticPowerMeterAccessory = require(path.join(__dirname, 'HomeMaticPowe

class HomeMaticPowerMeterSwitchAccessory extends HomeMaticPowerMeterAccessory {
initAccessoryService (Service) {
this.log.debug('[PMSWITCH] initAccessoryService')
this.debugLog('initAccessoryService')
this.service = this.getService(new Service.Outlet(this._name))
this.service.addOptionalCharacteristic(this.eve.Characteristic.TotalConsumption)
this.service.addOptionalCharacteristic(this.eve.Characteristic.ElectricCurrent)
Expand All @@ -48,22 +48,28 @@ class HomeMaticPowerMeterSwitchAccessory extends HomeMaticPowerMeterAccessory {
super.publishServices(Service, Characteristic)
let self = this
let readOnly = this.isReadOnly()
let onTime = this.getDeviceSettings().OnTime

if (this.getDataPointNameFromSettings('switch', null)) {
this.log.debug('[PMSWITCH] adding isOn isOnCharacteristic')
this.debugLog('adding isOn isOnCharacteristic')
this.isOnCharacteristic = this.service.getCharacteristic(Characteristic.On)
this.isOnCharacteristic.on('get', async (callback) => {
self.log.debug('[PMSWITCH] get state fetch CCU State')
self.debugLog('get state fetch CCU State')

let state = await self.getValueForDataPointNameWithSettingsKey('switch', null, true)
self.log.debug('[PMSWITCH] get state %s', state)
self.debugLog('get state %s', state)
self.currentStatus = self.isTrue(state)
callback(null, self.currentStatus)
})

this.isOnCharacteristic.on('set', (value, callback) => {
self.log.debug('[PMSWITCH] set %s', value)
this.isOnCharacteristic.on('set', async (value, callback) => {
self.debugLog('set %s', value)
if (!readOnly) {
if ((self.isTrue(value)) && (onTime) && (parseInt(onTime) > 0)) {
self.debugLog('set onTime %s seconds', onTime)
await self.setValueForDataPointNameWithSettingsKey('ontime', null, onTime)
}

self.setValueForDataPointNameWithSettingsKey('switch', null, value)
} else {
// check the state 1 sec later to reset the homekit state
Expand All @@ -81,7 +87,7 @@ class HomeMaticPowerMeterSwitchAccessory extends HomeMaticPowerMeterAccessory {
// }

this.registerAddressWithSettingsKeyForEventProcessingAtAccessory('switch', null, (newValue) => {
self.log.debug('[PMSWITCH] event state %s', newValue)
self.debugLog('event state %s', newValue)
self.currentStatus = self.isTrue(newValue)
if (self.isTrue(newValue)) {
self.updateLastActivation()
Expand Down Expand Up @@ -115,6 +121,7 @@ class HomeMaticPowerMeterSwitchAccessory extends HomeMaticPowerMeterAccessory {
'*': {
roChannel: 1,
switch: '1.STATE',
ontime: '1.ON_TIME',
power: 'POWER',
current: 'CURRENT',
voltage: 'VOLTAGE',
Expand All @@ -132,6 +139,12 @@ class HomeMaticPowerMeterSwitchAccessory extends HomeMaticPowerMeterAccessory {
default: 'Power',
label: 'Logging for',
hint: 'Eve is only able to log on option.'
},
'OnTime': {
type: 'number',
default: 0,
label: 'On Time',
hint: 'HAP will switch off this device automatically after the given seconds. Set this to 0 to turn off this feature.'
}
}
}
Expand Down
24 changes: 17 additions & 7 deletions lib/services/HomeMaticSwitchAccessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class HomeMaticSwitchAccessory extends HomeMaticAccessory {
let self = this
var service
let subType = this.getDeviceSettings().Type || 'Lightbulb'

let onTime = this.getDeviceSettings().OnTime
let readOnly = this.isReadOnly()

switch (subType) {
Expand All @@ -54,7 +54,7 @@ class HomeMaticSwitchAccessory extends HomeMaticAccessory {
break
}

this.log.debug('[SWITCH] creating Service %s', subType)
this.debugLog('[SWITCH] creating Service %s', subType)
this.isOnCharacteristic = service.getCharacteristic(Characteristic.On)

this.isOnCharacteristic.on('get', (callback) => {
Expand All @@ -63,18 +63,22 @@ class HomeMaticSwitchAccessory extends HomeMaticAccessory {
})
})

this.isOnCharacteristic.on('set', (value, callback) => {
this.isOnCharacteristic.on('set', async (value, callback) => {
if (!readOnly) {
self.log.debug('[Switch] set switch %s', value)

self.debugLog('[Switch] set switch %s', value)
if ((value === true) && (onTime) && (parseInt(onTime) > 0)) {
self.debugLog('set onTime %s seconds', onTime)
await self.setValue('ON_TIME', onTime)
}
self.debugLog('set value %s', value)
if (value === false) {
self.setValue('STATE', 0)
} else {
self.setValue('STATE', 1)
}
} else {
// check the state to reset the HomeKit State
self.log.debug('[Switch] is readOnly .. skipping')
self.debugLog('[Switch] is readOnly .. skipping')
setTimeout(() => {
self.getValue('STATE', true)
}, 1000)
Expand All @@ -83,7 +87,7 @@ class HomeMaticSwitchAccessory extends HomeMaticAccessory {
})

this.registerAddressForEventProcessingAtAccessory(this.buildAddress('STATE'), (newValue) => {
self.log.debug('[SWITCH] event state %s', newValue)
self.debugLog('[SWITCH] event state %s', newValue)
// Add a Log Entry for Eve
self.addLogEntry({status: self.isTrue(newValue) ? 1 : 0})
// Set Last Activation if the switch is on
Expand Down Expand Up @@ -119,6 +123,12 @@ class HomeMaticSwitchAccessory extends HomeMaticAccessory {
default: 'Lightbulb',
label: 'Subtype of this device',
hint: 'A switch can have different sub types'
},
'OnTime': {
type: 'number',
default: 0,
label: 'On Time',
hint: 'HAP will switch off this device automatically after the given seconds. Set this to 0 to turn off this feature.'
}
}
}
Expand Down

0 comments on commit 26d9dfd

Please sign in to comment.