Merge pull request #114 from thomasloven/status-topic

Listen for Home Assistant default birth message
This commit is contained in:
Marcus Westin 2021-01-29 22:02:37 +01:00 committed by GitHub
commit 594e6e214e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 13 deletions

View file

@ -93,7 +93,7 @@ If you restore a backup from a 32bit system to a new 64bit system, use the Rebui
### Configuration ### Configuration
You need to add the following to your `configuration.yaml` file: You need to add the MQTT integration to Home Assistant either by going to Configuration -> Integrations and clicking the Add Integration button, or by adding the following to your `configuration.yaml` file:
``` ```
mqtt: mqtt:
@ -102,16 +102,8 @@ mqtt:
password: !secret mqtt_password password: !secret mqtt_password
discovery: true discovery: true
discovery_prefix: homeassistant discovery_prefix: homeassistant
birth_message:
topic: 'hass/status'
payload: 'online'
will_message:
topic: 'hass/status'
payload: 'offline'
``` ```
The above is used to notify the add-on when Home Assistant has started successfully and let the add-on send the discovery response (containing all devices).
The plugin needs you to configure some settings before working. You find these on the Add-on page after you've installed it. The plugin needs you to configure some settings before working. You find these on the Add-on page after you've installed it.
| Parameter | Value | | Parameter | Value |

View file

@ -2,7 +2,7 @@ const EventEmitter = require('events');
const mqtt = require('mqtt'); const mqtt = require('mqtt');
const Logger = require('./Logger'); const Logger = require('./Logger');
const startTopic = 'hass/status'; const startTopics = ['hass/status', 'homeassistant/status'];
const logger = Logger.getLogger('plejd-mqtt'); const logger = Logger.getLogger('plejd-mqtt');
@ -74,9 +74,9 @@ class MqttClient extends EventEmitter {
this.client.on('connect', () => { this.client.on('connect', () => {
logger.info('Connected to MQTT.'); logger.info('Connected to MQTT.');
this.client.subscribe(startTopic, (err) => { this.client.subscribe(startTopics, (err) => {
if (err) { if (err) {
logger.error(`Unable to subscribe to ${startTopic}`); logger.error(`Unable to subscribe to status topics`);
} }
self.emit('connected'); self.emit('connected');
@ -100,7 +100,7 @@ class MqttClient extends EventEmitter {
? JSON.parse(message.toString()) ? JSON.parse(message.toString())
: message.toString(); : message.toString();
if (topic === startTopic) { if (startTopics.includes(topic)) {
logger.info('Home Assistant has started. lets do discovery.'); logger.info('Home Assistant has started. lets do discovery.');
self.emit('connected'); self.emit('connected');
} else if (topic.includes('set')) { } else if (topic.includes('set')) {