Skip to content

Commit

Permalink
remove switcher variable from accessory
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitay Ben-Zvi committed Sep 26, 2020
1 parent 3751a9f commit 1471a29
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 61 deletions.
28 changes: 14 additions & 14 deletions accessories/Outlet.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
let log, Characteristic, Service, accessory
let log, Characteristic, Service
const device = {}
const switcherInit = require('../lib/switcher')
const addExtras = require('./extras')

const Outlet = (that) => {
accessory = that
const Outlet = (accessory) => {
Characteristic = accessory.api.hap.Characteristic
Service = accessory.api.hap.Service
log = accessory.log
Expand All @@ -20,14 +20,14 @@ const Outlet = (that) => {
const extras = addExtras(OutletService, accessory)

accessory.updateHomeKit = () => {
OutletService.getCharacteristic(Characteristic.On).updateValue(!!accessory.switcher.state.state)
OutletService.getCharacteristic(Characteristic.OutletInUse).updateValue(accessory.switcher.state.power_consumption > 0)
OutletService.getCharacteristic(Characteristic.On).updateValue(!!device.switcher.state.state)
OutletService.getCharacteristic(Characteristic.OutletInUse).updateValue(device.switcher.state.power_consumption > 0)
extras.updateHomeKit()
}

switcherInit(accessory)
.then(switcher => {
accessory.switcher = switcher
device.switcher = switcher
log(`Successfully initialized Switcher accessory: ${switcher.state.name} (id:${switcher.device_id}) at ${switcher.switcher_ip}`)
})
.catch(err => {
Expand All @@ -44,38 +44,38 @@ const Outlet = (that) => {
module.exports = Outlet

const getOn = (callback) => {
if (!accessory.switcher) {
if (!device.switcher) {
log('switcher has yet to connect')
callback('switcher has yet to connect')
return
}
log(`Switcher is ${accessory.switcher.state.state ? 'ON' : 'OFF'}`)
callback(null, !!accessory.switcher.state.state)
log(`Switcher is ${device.switcher.state.state ? 'ON' : 'OFF'}`)
callback(null, !!device.switcher.state.state)
}

const getOutletInUse = (callback) => {
if (!accessory.switcher) {
if (!device.switcher) {
log('switcher has yet to connect')
callback('switcher has yet to connect')
return
}
const inUse = accessory.switcher.state.power_consumption > 0
const inUse = device.switcher.state.power_consumption > 0
log(`Switcher is ${inUse ? 'IN USE' : 'NOT IN USE'}`)
callback(null, inUse)
}

const setOn = (state, callback) => {
if (!accessory.switcher) {
if (!device.switcher) {
log('switcher has yet to connect')
callback('switcher has yet to connect')
return
}
if (state) {
log('Turning ON Switcher')
accessory.switcher.turn_on()
device.switcher.turn_on()
} else {
log('Turning OFF Switcher')
accessory.switcher.turn_off()
device.switcher.turn_off()
}
callback()
}
22 changes: 11 additions & 11 deletions accessories/Switch.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
let log, Characteristic, Service, accessory
let log, Characteristic, Service
const device = {}
const switcherInit = require('../lib/switcher')
const addExtras = require('./extras')

const Switch = (that) => {
accessory = that
const Switch = (accessory) => {
Characteristic = accessory.api.hap.Characteristic
Service = accessory.api.hap.Service
log = accessory.log
Expand All @@ -17,13 +17,13 @@ const Switch = (that) => {
const extras = addExtras(SwitchService, accessory)

accessory.updateHomeKit = () => {
SwitchService.getCharacteristic(Characteristic.On).updateValue(!!accessory.switcher.state.state)
SwitchService.getCharacteristic(Characteristic.On).updateValue(!!device.switcher.state.state)
extras.updateHomeKit()
}

switcherInit(accessory)
.then(switcher => {
accessory.switcher = switcher
device.switcher = switcher
log(`Successfully initialized Switcher accessory: ${switcher.state.name} (id:${switcher.device_id}) at ${switcher.switcher_ip}`)
})
.catch(err => {
Expand All @@ -40,28 +40,28 @@ const Switch = (that) => {
module.exports = Switch

const getOn = (callback) => {
if (!accessory.switcher) {
if (!device.switcher) {
log('switcher has yet to connect')
callback('switcher has yet to connect')
return
}
log(`Switcher is ${accessory.switcher.state.state ? 'ON' : 'OFF'}`)
callback(null, !!accessory.switcher.state.state)
log(`Switcher is ${device.switcher.state.state ? 'ON' : 'OFF'}`)
callback(null, !!device.switcher.state.state)
}


const setOn = (state, callback) => {
if (!accessory.switcher) {
if (!device.switcher) {
log('switcher has yet to connect')
callback('switcher has yet to connect')
return
}
if (state) {
log('Turning ON Switcher')
accessory.switcher.turn_on()
device.switcher.turn_on()
} else {
log('Turning OFF Switcher')
accessory.switcher.turn_off()
device.switcher.turn_off()
}
callback()
}
30 changes: 15 additions & 15 deletions accessories/Valve.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
let log, Characteristic, Service, accessory
let log, Characteristic, Service
const device = {}
const switcherInit = require('../lib/switcher')
const addExtras = require('./extras')

const Valve = (that) => {
accessory = that
const Valve = (accessory) => {
Characteristic = accessory.api.hap.Characteristic
Service = accessory.api.hap.Service
log = accessory.log
Expand All @@ -23,14 +23,14 @@ const Valve = (that) => {
const extras = addExtras(ValveService, accessory)

accessory.updateHomeKit = () => {
ValveService.getCharacteristic(Characteristic.Active).updateValue(accessory.switcher.state.state)
ValveService.getCharacteristic(Characteristic.InUse).updateValue(accessory.switcher.state.state)
ValveService.getCharacteristic(Characteristic.Active).updateValue(device.switcher.state.state)
ValveService.getCharacteristic(Characteristic.InUse).updateValue(device.switcher.state.state)
extras.updateHomeKit()
}

switcherInit(accessory)
.then(switcher => {
accessory.switcher = switcher
device.switcher = switcher
log(`Successfully initialized Switcher accessory: ${switcher.state.name} (id:${switcher.device_id}) at ${switcher.switcher_ip}`)
})
.catch(err => {
Expand All @@ -47,37 +47,37 @@ const Valve = (that) => {
module.exports = Valve

const getActive = (callback) => {
if (!accessory.switcher) {
if (!device.switcher) {
log('switcher has yet to connect')
callback('switcher has yet to connect')
return
}
log(`Switcher is ${accessory.switcher.state.state ? 'ON' : 'OFF'}`)
callback(null, accessory.switcher.state.state)
log(`Switcher is ${device.switcher.state.state ? 'ON' : 'OFF'}`)
callback(null, device.switcher.state.state)
}

const getValveInUse = (callback) => {
if (!accessory.switcher) {
if (!device.switcher) {
log('switcher has yet to connect')
callback('switcher has yet to connect')
return
}
log(`Switcher is ${accessory.switcher.state.state ? 'IN USE' : 'NOT IN USE'}`)
callback(null, accessory.switcher.state.state)
log(`Switcher is ${device.switcher.state.state ? 'IN USE' : 'NOT IN USE'}`)
callback(null, device.switcher.state.state)
}

const setActive = (state, callback) => {
if (!accessory.switcher) {
if (!device.switcher) {
log('switcher has yet to connect')
callback('switcher has yet to connect')
return
}
if (state) {
log('Turning ON Switcher')
accessory.switcher.turn_on()
device.switcher.turn_on()
} else {
log('Turning OFF Switcher')
accessory.switcher.turn_off()
device.switcher.turn_off()
}
callback()
}
42 changes: 21 additions & 21 deletions accessories/extras.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
let log, Characteristic, accessory
let log, Characteristic, device

const Extras = (service, that) => {
accessory = that
const Extras = (service, accessory, switcher) => {
device = switcher
Characteristic = accessory.api.hap.Characteristic
log = accessory.log
accessory.totalEnergy = 0
Expand Down Expand Up @@ -71,8 +71,8 @@ const Extras = (service, that) => {

return {
updateHomeKit: () => {
service.getCharacteristic(Characteristic.RemainingDuration).updateValue(accessory.switcher.state.remaining_seconds)
service.getCharacteristic(Characteristic.SetDuration).updateValue(accessory.switcher.state.default_shutdown_seconds)
service.getCharacteristic(Characteristic.RemainingDuration).updateValue(device.switcher.state.remaining_seconds)
service.getCharacteristic(Characteristic.SetDuration).updateValue(device.switcher.state.default_shutdown_seconds)
service.getCharacteristic(EnergyCharacteristics.Watts).getValue(null)
service.getCharacteristic(EnergyCharacteristics.Volts).getValue(null)
service.getCharacteristic(EnergyCharacteristics.Amperes).getValue(null)
Expand All @@ -84,22 +84,22 @@ const Extras = (service, that) => {
if (accessory.loggingService.isHistoryLoaded()) {
const extraPersistedData = accessory.loggingService.getExtraPersistedData()
if (extraPersistedData != undefined) {
accessory.totalEnergy = extraPersistedData.totalEnergy + accessory.totalEnergyTemp + accessory.switcher.state.power_consumption * (timeSinceLastState / 1000) / 3600 / 1000
accessory.totalEnergy = extraPersistedData.totalEnergy + accessory.totalEnergyTemp + device.switcher.state.power_consumption * (timeSinceLastState / 1000) / 3600 / 1000
accessory.loggingService.setExtraPersistedData({ totalEnergy: accessory.totalEnergy, lastReset: extraPersistedData.lastReset })
}
else {
accessory.totalEnergy = accessory.totalEnergyTemp + accessory.switcher.state.power_consumption * (timeSinceLastState / 1000) / 3600 / 1000
accessory.totalEnergy = accessory.totalEnergyTemp + device.switcher.state.power_consumption * (timeSinceLastState / 1000) / 3600 / 1000
accessory.loggingService.setExtraPersistedData({ totalEnergy: accessory.totalEnergy, lastReset: 0 })
}
accessory.totalEnergyTemp = 0

} else {
accessory.totalEnergyTemp = accessory.totalEnergyTemp + accessory.switcher.state.power_consumption * (timeSinceLastState / 1000) / 3600 / 1000
accessory.totalEnergyTemp = accessory.totalEnergyTemp + device.switcher.state.power_consumption * (timeSinceLastState / 1000) / 3600 / 1000
accessory.totalEnergy = accessory.totalEnergyTemp
}


accessory.loggingService.addEntry({time: Math.floor((new Date()).getTime()/1000), power: accessory.switcher.state.power_consumption})
accessory.loggingService.addEntry({time: Math.floor((new Date()).getTime()/1000), power: device.switcher.state.power_consumption})
}
}
}
Expand All @@ -109,54 +109,54 @@ const Extras = (service, that) => {
module.exports = Extras

const getVolts = (callback) => {
if (!accessory.switcher) {
if (!device.switcher) {
callback('switcher has yet to connect')
return
}
const volts = accessory.switcher.state.state ? 220 : 0
const volts = device.switcher.state.state ? 220 : 0
callback(null, volts)
}

const getAmperes = (callback) => {
if (!accessory.switcher) {
if (!device.switcher) {
callback('switcher has yet to connect')
return
}
const amperes = Number(Math.round(accessory.switcher.state.power_consumption/220 + "e1") + "e-1")
const amperes = Number(Math.round(device.switcher.state.power_consumption/220 + "e1") + "e-1")
callback(null, amperes)
}

const getWatts = (callback) => {
if (!accessory.switcher) {
if (!device.switcher) {
callback('switcher has yet to connect')
return
}
const watts = accessory.switcher.state.power_consumption
const watts = device.switcher.state.power_consumption
callback(null, watts)
}

const getDuration = (callback) => {
if (!accessory.switcher) {
if (!device.switcher) {
callback('switcher has yet to connect')
return
}
const duration = accessory.switcher.state.default_shutdown_seconds
const duration = device.switcher.state.default_shutdown_seconds
log.easyDebug("Auto Shutdown in Seconds:" + duration)
callback(null, duration)
}

const getRemainingDuration = (callback) => {
if (!accessory.switcher) {
if (!device.switcher) {
callback('switcher has yet to connect')
return
}
const duration = accessory.switcher.state.remaining_seconds
const duration = device.switcher.state.remaining_seconds
log.easyDebug("Remaining duration in Seconds:" + duration)
callback(null, duration)
}

const setDuration = (seconds, callback) => {
if (!accessory.switcher) {
if (!device.switcher) {
callback('switcher has yet to connect')
return
}
Expand All @@ -166,6 +166,6 @@ const setDuration = (seconds, callback) => {
const formattedTime = hours + ':' + ('0' + minutes).slice(-2)

log("Setting new \"Auto Shutdown\" time - " + formattedTime)
accessory.switcher.set_default_shutdown(seconds)
device.switcher.set_default_shutdown(seconds)
callback()
}

0 comments on commit 1471a29

Please sign in to comment.