0.0.10 remove original watchlist no longer needed. per entity watch
changed ping pong prop to connectionMonitor fix little bug in getEntitiesmaster
parent
7c24aa3634
commit
969f1f8600
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@uci/ha",
|
||||
"version": "0.0.9",
|
||||
"version": "0.0.10",
|
||||
"description": "websocket api access to home assistant",
|
||||
"main": "./src/homeassistant.js",
|
||||
"scripts": {
|
||||
|
@ -15,9 +15,9 @@
|
|||
"@uci-utils/logger": "^0.0.18",
|
||||
"await-to-js": "^2.1.1",
|
||||
"better-try-catch": "^0.6.2",
|
||||
"delay": "^4.3.0",
|
||||
"delay": "^4.4.0",
|
||||
"faye-websocket": "^0.11.3",
|
||||
"is-plain-object": "^3.0.1"
|
||||
"is-plain-object": "^4.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"esm": "^3.2.25",
|
||||
|
|
|
@ -5,8 +5,6 @@ import to from 'await-to-js'
|
|||
import logger from '@uci-utils/logger'
|
||||
let log = logger({ name: 'HomeAssistant:createSocket'})
|
||||
|
||||
|
||||
|
||||
const MSG_TYPE_AUTH_REQUIRED = 'auth_required'
|
||||
const MSG_TYPE_AUTH_INVALID = 'auth_invalid'
|
||||
const MSG_TYPE_AUTH_OK = 'auth_ok'
|
||||
|
|
|
@ -15,25 +15,26 @@ const DEFUALT_HASS_OPTS = {
|
|||
timeout: 5000,
|
||||
retryCount: -1,
|
||||
port: 8123,
|
||||
ppmonitor: true
|
||||
connectionMonitor: true
|
||||
}
|
||||
|
||||
class HomeAssistant extends EventEmitter {
|
||||
constructor(opts) {
|
||||
super()
|
||||
log = logger({ name: 'HomeAssistant', id: this.id })
|
||||
// console.log('options',opts)
|
||||
this.opts = Object.assign(DEFUALT_HASS_OPTS, opts)
|
||||
log.debug({msg:'config to constructor',opts:opts})
|
||||
// console.log({msg:'config to constructor',opts:this.opts})
|
||||
this.url = (this.opts.url ? `${this.opts.url} + /${this.opts.serverPath}` : `${this.opts.protocol}://${this.opts.host}:${this.opts.port}`) + `/${this.opts.serverPath}`
|
||||
log.debug({msg:'url for websocket', url:this.url})
|
||||
this.cmdId = 1
|
||||
this.eventBusId = null
|
||||
this._watchLists = {}
|
||||
// this._watchLists = {}
|
||||
this._watchList=[]
|
||||
this._silence = []
|
||||
this.connected=false
|
||||
this.ready=false
|
||||
this.watchListEventPrefix = opts.watchListEventPrefix || 'wl'
|
||||
// this.watchListEventPrefix = opts.watchListEventPrefix || 'wl'
|
||||
this.on('error',msg=> log.error(msg))
|
||||
}
|
||||
|
||||
|
@ -61,8 +62,7 @@ class HomeAssistant extends EventEmitter {
|
|||
log.info('Successfuly connected to Home Assistant')
|
||||
this.connected=true
|
||||
await this._listen()
|
||||
this._monitorConnection(this.opts.ppmonitor)
|
||||
|
||||
this.monitorConnection(this.opts.connectionMonitor)
|
||||
return 'success'
|
||||
|
||||
} // end connect
|
||||
|
@ -95,8 +95,7 @@ class HomeAssistant extends EventEmitter {
|
|||
if (err) {
|
||||
this.emit('error',{msg:'failed json parse of event data', event:ev.data, error:err})
|
||||
} else {
|
||||
log.debug('incoming message packet from server', packet.id, packet.type)
|
||||
// console.log('ws event packet',packet)
|
||||
// console.log('incoming event packet from server', packet.id, packet.type)
|
||||
if (packet.type === 'event') {
|
||||
if(this._watchList.includes(packet.event.data.entity_id)) {
|
||||
// console.log('emitting',packet.event.data.entity_id, packet.event.event_type)
|
||||
|
@ -174,8 +173,8 @@ class HomeAssistant extends EventEmitter {
|
|||
})
|
||||
}
|
||||
|
||||
|
||||
async getEntities (ents='all',type='obj') {
|
||||
// console.log('getting entities', ents)
|
||||
let single = false
|
||||
if (typeof ents ==='string') {
|
||||
if (ents ==='array' || ents ==='all') {ents = 'all'; type = 'array'}
|
||||
|
@ -189,7 +188,7 @@ class HomeAssistant extends EventEmitter {
|
|||
}
|
||||
if (ents !== 'all') {
|
||||
if (typeof list === 'string') ents = [ents]
|
||||
res = res.result.filter( item => ents.indexOf(item.entity_id) > -1 )
|
||||
return res.result.filter( item => ents.indexOf(item.entity_id) > -1 )
|
||||
}
|
||||
if (single) return res[0] || {}
|
||||
if (type == 'obj') {
|
||||
|
@ -197,7 +196,7 @@ class HomeAssistant extends EventEmitter {
|
|||
res.forEach(ent => obj[ent.entity_id]=ent)
|
||||
return obj
|
||||
}
|
||||
return res
|
||||
return res.result
|
||||
}
|
||||
// todo need to autobind
|
||||
async callService(domain,service,data) {
|
||||
|
@ -223,11 +222,7 @@ class HomeAssistant extends EventEmitter {
|
|||
|
||||
// only changes state passively
|
||||
async updateEntity(entity,state) {
|
||||
// this.silence(entity)
|
||||
// console.log('silence on',entity)
|
||||
return await this.callService('python_script','state_change',{entity_id:entity, state:state})
|
||||
// console.log('silence off', entity)
|
||||
// this.silence(entity)
|
||||
}
|
||||
|
||||
async setVariable(eid,value) {
|
||||
|
@ -256,37 +251,13 @@ class HomeAssistant extends EventEmitter {
|
|||
return await this.callService(domain,service, {entity_id:eid, [key]:value})
|
||||
}
|
||||
|
||||
async makeWatchList (ents,name='default') {
|
||||
if (typeof entity === 'string') ents = [ents]
|
||||
this._watchLists[name] = ents // await this.getEntities(ents)
|
||||
let list = this._watchLists[name]
|
||||
ents.forEach(ent => {
|
||||
this.on(ent, handleUpdate)
|
||||
})
|
||||
monitorConnection(enabled) {
|
||||
|
||||
this._handleUpdate = handleUpdate // need to save pointer for removing listener
|
||||
// console.log('monitoring', enabled)
|
||||
|
||||
function handleUpdate (changed) {
|
||||
list[changed.entity_id] = changed // update entity in watch list
|
||||
// log.debug(changed.state,list[changed.entity_id])
|
||||
this.emit(`${this.watchListEventPrefix}-${name}`, changed)
|
||||
}
|
||||
if (!enabled) console.log('WARNING: connection monitoring is disabled, system will not know if HA disconnected')
|
||||
|
||||
}
|
||||
|
||||
getWatchList(name) {
|
||||
return this._watchLists[name]
|
||||
}
|
||||
|
||||
removeWatchList (name) {
|
||||
this.getWatchList(name).forEach(ent => this.removeListener(ent.entity_id,this._handleUpdate))
|
||||
delete this._watchLists[name]
|
||||
this.removeAllListeners(`wl-${name}`)
|
||||
}
|
||||
|
||||
_monitorConnection(enabled=true) {
|
||||
|
||||
let ping = null
|
||||
let ping = null // reference to holds the ping timeout
|
||||
|
||||
async function queuePing() {
|
||||
// let id = this.nextId()
|
||||
|
|
Loading…
Reference in New Issue