Skip to content

Commit

Permalink
Homeassisant: rediscover devices on MQTT reconnect. #105
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Jun 11, 2018
1 parent b725237 commit cd13286
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
35 changes: 18 additions & 17 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,40 +50,41 @@ class Controller {
this.configureDevice(device);
});

// Enable zigbee join.
if (settings.get().permit_join) {
logger.warn('`permit_join` set to `true` in configuration.yaml.');
logger.warn('Allowing new devices to join.');
logger.warn('Set `permit_join` to `false` once you joined all devices.');
this.zigbee.permitJoin(true);
}

// Start timers.
this.pollTimer(true);
this.softResetTimeout(true);

// Connect to MQTT broker
const subscriptions = [
`${settings.get().mqtt.base_topic}/+/set`,
`${settings.get().mqtt.base_topic}/+/+/set`,
`${settings.get().mqtt.base_topic}/bridge/config/+`,
];
this.mqtt.connect(this.handleMQTTMessage, subscriptions, () => this.handleStarted());

this.mqtt.connect(this.handleMQTTMessage, subscriptions, () => this.handleMQTTConnected());
}
});
}

handleStarted() {
// Home Assistant MQTT discovery on startup.
handleMQTTConnected() {
// Home Assistant MQTT discovery on MQTT connected.
if (settings.get().homeassistant) {
// MQTT discovery of all paired devices on startup.
this.zigbee.getAllClients().forEach((device) => {
const mappedModel = zigbeeShepherdConverters.findByZigbeeModel(device.modelId);
if (mappedModel) {
homeassistant.discover(device.ieeeAddr, mappedModel.model, this.mqtt);
homeassistant.discover(device.ieeeAddr, mappedModel.model, this.mqtt, true);
}
});
}

// Enable zigbee join.
if (settings.get().permit_join) {
logger.warn('`permit_join` set to `true` in configuration.yaml.');
logger.warn('Allowing new devices to join.');
logger.warn('Set `permit_join` to `false` once you joined all devices.');
this.zigbee.permitJoin(true);
}

// Start timers.
this.pollTimer(true);
this.softResetTimeout(true);
}

softResetTimeout(start) {
Expand Down Expand Up @@ -238,7 +239,7 @@ class Controller {

// Home Assistant MQTT discovery
if (settings.get().homeassistant) {
homeassistant.discover(device.ieeeAddr, mappedModel.model, this.mqtt);
homeassistant.discover(device.ieeeAddr, mappedModel.model, this.mqtt, false);
}

// After this point we cant handle message withoud cid anymore.
Expand Down
7 changes: 4 additions & 3 deletions lib/homeassistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,10 @@ const mapping = {
// A map of all discoverd devices
const discovered = {};

function discover(deviceID, model, mqtt) {
function discover(deviceID, model, mqtt, force) {
// Check if already discoverd and check if there are configs.
if (discovered[deviceID] || !mapping[model] || !settings.getDevice(deviceID)) {
const discover = force || !discovered[deviceID];
if (!discover || !mapping[model] || !settings.getDevice(deviceID)) {
return;
}

Expand Down Expand Up @@ -313,6 +314,6 @@ function clear(deviceID, model, mqtt) {

module.exports = {
mapping: mapping,
discover: (deviceID, model, mqtt) => discover(deviceID, model, mqtt),
discover: (deviceID, model, mqtt, force) => discover(deviceID, model, mqtt, force),
clear: (deviceID, model, mqtt) => clear(deviceID, model, mqtt),
};

0 comments on commit cd13286

Please sign in to comment.