Skip to content

Commit

Permalink
Allow custom home-assistant status topic
Browse files Browse the repository at this point in the history
  • Loading branch information
cybe committed Jun 24, 2019
1 parent 38a1aec commit 1afa43b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/extension/homeassistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,10 +773,11 @@ class HomeAssistant {
}

this.discoveryTopic = settings.get().advanced.homeassistant_discovery_topic;
this.statusTopic = settings.get().advanced.homeassistant_status_topic;
}

onMQTTConnected() {
this.mqtt.subscribe('hass/status');
this.mqtt.subscribe(this.statusTopic);

// MQTT discovery of all paired devices on startup.
this.zigbee.getAllClients().forEach((device) => {
Expand Down Expand Up @@ -932,7 +933,7 @@ class HomeAssistant {
}

onMQTTMessage(topic, message) {
if (!topic === 'hass/status') {
if (!topic === this.statusTopic) {
return false;
}

Expand Down
5 changes: 5 additions & 0 deletions lib/util/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ const defaults = {
* Home Assistant discovery topic
*/
homeassistant_discovery_topic: 'homeassistant',

/**
* Home Assistant status topic
*/
homeassistant_status_topic: 'hass/status',
},
};

Expand Down
31 changes: 29 additions & 2 deletions test/homeassistant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ describe('HomeAssistant extension', () => {
output: 'json',
},
advanced: {
homeassistant_discovery_topic: 'my_custom_topic',
homeassistant_discovery_topic: 'my_custom_discovery_topic',
},
});

Expand All @@ -627,6 +627,33 @@ describe('HomeAssistant extension', () => {
homeassistant.discover('0x12345678', WSDCGQ11LM, false);

expect(mqtt.publish).toHaveBeenCalledTimes(5);
expect(mqtt.publish.mock.calls[0][4]).toBe('my_custom_topic');
expect(mqtt.publish.mock.calls[0][4]).toBe('my_custom_discovery_topic');
});

it('Should subscribe to custom status topic', () => {
jest.spyOn(settings, 'get').mockReturnValue({
experimental: {
output: 'json',
},
advanced: {
homeassistant_status_topic: 'my_custom_status_topic',
},
});

zigbee = {
getAllClients: jest.fn().mockReturnValue([]),
};

mqtt = {
subscribe: jest.fn(),
};


homeassistant = new HomeassistantExtension(zigbee, mqtt, null, null);

homeassistant.onMQTTConnected();

expect(mqtt.subscribe).toHaveBeenCalledTimes(1);
expect(mqtt.subscribe.mock.calls[0][0]).toBe('my_custom_status_topic');
});
});

0 comments on commit 1afa43b

Please sign in to comment.