Clean up events and subscriptions
This commit is contained in:
parent
4d7de61e42
commit
ca7a5cdd57
6 changed files with 117 additions and 51 deletions
|
|
@ -70,6 +70,11 @@ const getSwitchPayload = (device) => ({
|
|||
class MqttClient extends EventEmitter {
|
||||
deviceRegistry;
|
||||
|
||||
static EVENTS = {
|
||||
connected: 'connected',
|
||||
stateChanged: 'stateChanged',
|
||||
};
|
||||
|
||||
constructor(deviceRegistry) {
|
||||
super();
|
||||
|
||||
|
|
@ -97,7 +102,7 @@ class MqttClient extends EventEmitter {
|
|||
logger.error('Unable to subscribe to status topics');
|
||||
}
|
||||
|
||||
this.emit('connected');
|
||||
this.emit(MqttClient.EVENTS.connected);
|
||||
});
|
||||
|
||||
this.client.subscribe(getSubscribePath(), (err) => {
|
||||
|
|
@ -115,7 +120,7 @@ class MqttClient extends EventEmitter {
|
|||
this.client.on('message', (topic, message) => {
|
||||
if (startTopics.includes(topic)) {
|
||||
logger.info('Home Assistant has started. lets do discovery.');
|
||||
this.emit('connected');
|
||||
this.emit(MqttClient.EVENTS.connected);
|
||||
} else {
|
||||
const decodedTopic = decodeTopic(topic);
|
||||
if (decodedTopic) {
|
||||
|
|
@ -148,7 +153,7 @@ class MqttClient extends EventEmitter {
|
|||
);
|
||||
|
||||
if (device) {
|
||||
this.emit('stateChanged', device, command);
|
||||
this.emit(MqttClient.EVENTS.stateChanged, device, command);
|
||||
} else {
|
||||
logger.warn(
|
||||
`Device for topic ${topic} not found! Can happen if HA calls previously existing devices.`,
|
||||
|
|
@ -182,6 +187,10 @@ class MqttClient extends EventEmitter {
|
|||
this.client.reconnect();
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
this.client.removeAllListeners();
|
||||
}
|
||||
|
||||
disconnect(callback) {
|
||||
this.deviceRegistry.allDevices.forEach((device) => {
|
||||
this.client.publish(getAvailabilityTopic(device), 'offline');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue