Skip to content

Commit

Permalink
fix: ensure subscribe to topics (#1466)
Browse files Browse the repository at this point in the history
Fixes #1464 Fixes #1322
  • Loading branch information
robertsLando authored Jul 21, 2021
1 parent 84c25a1 commit 3cb8130
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions lib/Gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1682,16 +1682,6 @@ export default class Gateway {

const isDiscovered = this.discovered[valueId.id]

const isFromCache = !node.ready

// prevent to send cached values if them are stateless
if (isFromCache && valueId.stateless) {
logger.debug(
`Skipping send of stateless value ${valueId.id}: it's from cache`
)
return
}

// check if this value isn't discovered yet (values added after node is ready)
if (this.config.hassDiscovery && !isDiscovered) {
this.discoverValue(node, this._getIdWithoutNode(valueId))
Expand Down Expand Up @@ -1785,6 +1775,8 @@ export default class Gateway {
if (valueId.writeable && !this.topicValues[topic]) {
const levels = topic.split('/').length

logger.debug(`Subscribing to updates of ${valueId.id}`)

if (this.topicLevels.indexOf(levels) < 0) {
this.topicLevels.push(levels)
this._mqtt.subscribe('+'.repeat(levels).split('').join('/'))
Expand Down Expand Up @@ -1816,7 +1808,16 @@ export default class Gateway {
}
}

this._mqtt.publish(topic, data, mqttOptions)
const isFromCache = !node.ready

// prevent to send cached values if them are stateless
if (isFromCache && valueId.stateless) {
logger.debug(
`Skipping send of stateless value ${valueId.id}: it's from cache`
)
} else {
this._mqtt.publish(topic, data, mqttOptions)
}
}

/**
Expand Down Expand Up @@ -2347,6 +2348,10 @@ export default class Gateway {
true
) as ValueIdTopic

if (!topics) {
throw Error(`Can't find topics for ${vID}`)
}

cfg.values.push(vID, valueIdState.targetValue)

discoveredStateTopic = this._mqtt.getTopic(topics.topic)
Expand Down

0 comments on commit 3cb8130

Please sign in to comment.