2019-12-04 11:17:06 +01:00
|
|
|
const EventEmitter = require('events');
|
|
|
|
|
const mqtt = require('mqtt');
|
2021-01-21 21:31:37 +01:00
|
|
|
const Logger = require('./Logger');
|
2019-12-04 11:17:06 +01:00
|
|
|
|
|
|
|
|
const startTopic = 'hass/status';
|
|
|
|
|
|
2021-01-21 21:31:37 +01:00
|
|
|
const logger = Logger.getLogger("plejd-mqtt");
|
2019-12-04 11:17:06 +01:00
|
|
|
|
|
|
|
|
// #region discovery
|
|
|
|
|
|
|
|
|
|
const discoveryPrefix = 'homeassistant';
|
|
|
|
|
const nodeId = 'plejd';
|
|
|
|
|
|
|
|
|
|
const getSubscribePath = () => `${discoveryPrefix}/+/${nodeId}/#`;
|
|
|
|
|
const getPath = ({ id, type }) =>
|
|
|
|
|
`${discoveryPrefix}/${type}/${nodeId}/${id}`;
|
|
|
|
|
const getConfigPath = plug => `${getPath(plug)}/config`;
|
|
|
|
|
const getStateTopic = plug => `${getPath(plug)}/state`;
|
|
|
|
|
const getCommandTopic = plug => `${getPath(plug)}/set`;
|
2019-12-22 17:48:16 +00:00
|
|
|
const getSceneEventTopic = () => `plejd/event/scene`;
|
2019-12-04 11:17:06 +01:00
|
|
|
|
2019-12-10 22:01:12 +01:00
|
|
|
const getDiscoveryPayload = device => ({
|
2019-12-21 15:01:15 +00:00
|
|
|
schema: 'json',
|
2019-12-10 22:01:12 +01:00
|
|
|
name: device.name,
|
2020-01-27 20:43:52 +00:00
|
|
|
unique_id: `light.plejd.${device.name.toLowerCase().replace(/ /g, '')}`,
|
2019-12-10 22:01:12 +01:00
|
|
|
state_topic: getStateTopic(device),
|
|
|
|
|
command_topic: getCommandTopic(device),
|
2019-12-21 15:01:15 +00:00
|
|
|
optimistic: false,
|
2020-01-21 14:24:02 +00:00
|
|
|
brightness: `${device.dimmable}`,
|
|
|
|
|
device: {
|
2020-01-24 18:06:41 +00:00
|
|
|
identifiers: device.serialNumber + '_' + device.id,
|
2020-01-21 14:24:02 +00:00
|
|
|
manufacturer: 'Plejd',
|
|
|
|
|
model: device.typeName,
|
|
|
|
|
name: device.name,
|
|
|
|
|
sw_version: device.version
|
|
|
|
|
}
|
2019-12-10 22:01:12 +01:00
|
|
|
});
|
|
|
|
|
|
2020-02-20 13:02:47 +01:00
|
|
|
const getSwitchPayload = device => ({
|
|
|
|
|
name: device.name,
|
|
|
|
|
state_topic: getStateTopic(device),
|
|
|
|
|
command_topic: getCommandTopic(device),
|
|
|
|
|
optimistic: false,
|
|
|
|
|
device: {
|
|
|
|
|
identifiers: device.serialNumber + '_' + device.id,
|
|
|
|
|
manufacturer: 'Plejd',
|
|
|
|
|
model: device.typeName,
|
|
|
|
|
name: device.name,
|
|
|
|
|
sw_version: device.version
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-12-04 11:17:06 +01:00
|
|
|
// #endregion
|
|
|
|
|
|
|
|
|
|
class MqttClient extends EventEmitter {
|
|
|
|
|
constructor(mqttBroker, username, password) {
|
|
|
|
|
super();
|
|
|
|
|
|
|
|
|
|
this.mqttBroker = mqttBroker;
|
|
|
|
|
this.username = username;
|
|
|
|
|
this.password = password;
|
|
|
|
|
this.deviceMap = {};
|
|
|
|
|
this.devices = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init() {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.info("Initializing MQTT connection for Plejd addon");
|
2019-12-04 11:17:06 +01:00
|
|
|
const self = this;
|
|
|
|
|
|
|
|
|
|
this.client = mqtt.connect(this.mqttBroker, {
|
|
|
|
|
username: this.username,
|
|
|
|
|
password: this.password
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.client.on('connect', () => {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.info('Connected to MQTT.');
|
2019-12-04 11:17:06 +01:00
|
|
|
|
|
|
|
|
this.client.subscribe(startTopic, (err) => {
|
|
|
|
|
if (err) {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.error(`Unable to subscribe to ${startTopic}`);
|
2019-12-04 11:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.emit('connected');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.client.subscribe(getSubscribePath(), (err) => {
|
|
|
|
|
if (err) {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.error('Unable to subscribe to control topics');
|
2019-12-04 11:17:06 +01:00
|
|
|
}
|
|
|
|
|
});
|
2019-12-22 18:20:20 +00:00
|
|
|
|
2019-12-04 11:17:06 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.client.on('close', () => {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.verbose('Warning: mqtt channel closed event, reconnecting...');
|
2019-12-04 11:17:06 +01:00
|
|
|
self.reconnect();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.client.on('message', (topic, message) => {
|
2019-12-21 15:01:15 +00:00
|
|
|
//const command = message.toString();
|
2020-02-29 15:54:08 +00:00
|
|
|
const command = message.toString().substring(0, 1) === '{'
|
|
|
|
|
? JSON.parse(message.toString())
|
|
|
|
|
: message.toString();
|
2019-12-04 11:17:06 +01:00
|
|
|
|
|
|
|
|
if (topic === startTopic) {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.info('Home Assistant has started. lets do discovery.');
|
2019-12-04 11:17:06 +01:00
|
|
|
self.emit('connected');
|
|
|
|
|
}
|
2021-01-21 21:31:37 +01:00
|
|
|
else if (topic.includes('set')) {
|
|
|
|
|
logger.verbose(`Got mqtt command on ${topic} - ${message}`);
|
2019-12-04 11:17:06 +01:00
|
|
|
const device = self.devices.find(x => getCommandTopic(x) === topic);
|
2020-02-29 15:54:08 +00:00
|
|
|
self.emit('stateChanged', device, command);
|
2019-12-04 11:17:06 +01:00
|
|
|
}
|
2021-01-21 21:31:37 +01:00
|
|
|
else {
|
|
|
|
|
logger.verbose(`Warning: Got unrecognized mqtt command on ${topic} - ${message}`);
|
|
|
|
|
}
|
2019-12-04 11:17:06 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reconnect() {
|
|
|
|
|
this.client.reconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
discover(devices) {
|
|
|
|
|
this.devices = devices;
|
|
|
|
|
|
|
|
|
|
const self = this;
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.debug(`Sending discovery of ${devices.length} device(s).`);
|
2019-12-04 11:17:06 +01:00
|
|
|
|
|
|
|
|
devices.forEach((device) => {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.debug(`Sending discovery for ${device.name}`);
|
2019-12-04 11:17:06 +01:00
|
|
|
|
2020-02-20 13:02:47 +01:00
|
|
|
let payload = device.type === 'switch' ? getSwitchPayload(device) : getDiscoveryPayload(device);
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.info(`Discovered ${device.type} (${device.typeName}) named ${device.name} with PID ${device.id}.`);
|
2019-12-13 14:13:00 +01:00
|
|
|
|
2019-12-04 11:17:06 +01:00
|
|
|
self.deviceMap[device.id] = payload.unique_id;
|
|
|
|
|
|
|
|
|
|
self.client.publish(
|
|
|
|
|
getConfigPath(device),
|
|
|
|
|
JSON.stringify(payload)
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-21 15:01:15 +00:00
|
|
|
updateState(deviceId, data) {
|
2019-12-04 11:17:06 +01:00
|
|
|
const device = this.devices.find(x => x.id === deviceId);
|
|
|
|
|
|
|
|
|
|
if (!device) {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.warn(`Unknown device id ${deviceId} - not handled by us.`);
|
2019-12-04 11:17:06 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.verbose(`Updating state for ${device.name}: ${data.state}`);
|
2019-12-21 15:01:15 +00:00
|
|
|
let payload = null;
|
2019-12-04 11:17:06 +01:00
|
|
|
|
2020-02-29 15:54:08 +00:00
|
|
|
if (device.type === 'switch') {
|
|
|
|
|
payload = data.state === 1 ? 'ON' : 'OFF';
|
2019-12-21 15:01:15 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2020-02-29 15:54:08 +00:00
|
|
|
if (device.dimmable) {
|
|
|
|
|
payload = {
|
|
|
|
|
state: data.state === 1 ? 'ON' : 'OFF',
|
|
|
|
|
brightness: data.brightness
|
|
|
|
|
}
|
2019-12-21 15:01:15 +00:00
|
|
|
}
|
2020-02-29 15:54:08 +00:00
|
|
|
else {
|
|
|
|
|
payload = {
|
|
|
|
|
state: data.state === 1 ? 'ON' : 'OFF'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
payload = JSON.stringify(payload);
|
2019-12-04 11:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.client.publish(
|
2019-12-21 15:01:15 +00:00
|
|
|
getStateTopic(device),
|
2020-02-29 15:54:08 +00:00
|
|
|
payload
|
2019-12-04 11:17:06 +01:00
|
|
|
);
|
|
|
|
|
}
|
2019-12-22 17:48:16 +00:00
|
|
|
|
|
|
|
|
sceneTriggered(scene) {
|
2021-01-21 21:31:37 +01:00
|
|
|
logger.verbose(`Scene triggered: ${scene}`);
|
2019-12-22 17:48:16 +00:00
|
|
|
this.client.publish(
|
|
|
|
|
getSceneEventTopic(),
|
|
|
|
|
JSON.stringify({ scene: scene })
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-12-04 11:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = { MqttClient };
|