38 lines
1.3 KiB
JavaScript
38 lines
1.3 KiB
JavaScript
|
import Hass from '../src/homeassistant'
|
||
|
import readYaml from 'load-yaml-file'
|
||
|
|
||
|
;
|
||
|
(async () => {
|
||
|
let opts = await readYaml(process.env.HA_OPTS_PATH || './examples/opts.yaml')
|
||
|
console.log(opts)
|
||
|
const hass = new Hass(opts)
|
||
|
hass.on('connection', msg => console.log(`connection: ${msg}`))
|
||
|
await hass.connect()
|
||
|
if (hass.eventBusId) console.log('listener established for HA event bus')
|
||
|
|
||
|
hass.on('error', err => console.log(err))
|
||
|
|
||
|
let name = 'test'
|
||
|
let list = ['input_number.node_test_first_number','input_number.node_test_second_number']
|
||
|
await hass.makeWatchList(list,name)
|
||
|
hass.on('wl-'+name,(ent) => {
|
||
|
testAction(ent.entity_id)
|
||
|
})
|
||
|
|
||
|
function testAction() {
|
||
|
let vars = hass.getWatchList(name)
|
||
|
// console.log(vars)
|
||
|
let first = vars[list[0]].state
|
||
|
let second = vars[list[1]].state
|
||
|
let sum = +first + +second
|
||
|
console.log(`return sum of ${first} and ${second} which is ${sum}`)
|
||
|
hass.setVariable('node_test_sum', sum)
|
||
|
// sum > 99 ? hass.switch('node_test_switch','on') : hass.switch('node_test_switch','off')
|
||
|
sum > 99 ? hass.setVariable('node_test_switch', 'ON') : hass.setVariable('node_test_switch','OFF')
|
||
|
}
|
||
|
|
||
|
})().catch(err => {
|
||
|
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
||
|
process.kill(process.pid, 'SIGTERM')
|
||
|
})
|