hassio-plejd/plejd/PlejdAddon.js

143 lines
4.2 KiB
JavaScript
Raw Normal View History

const EventEmitter = require('events');
const Configuration = require('./Configuration');
const Logger = require('./Logger');
const PlejdApi = require('./PlejdApi');
const PlejdDeviceCommunication = require('./PlejdDeviceCommunication');
const MqttClient = require('./MqttClient');
const SceneManager = require('./SceneManager');
const DeviceRegistry = require('./DeviceRegistry');
const logger = Logger.getLogger('plejd-main');
class PlejdAddon extends EventEmitter {
bleInitTimeout;
config;
deviceRegistry;
plejdApi;
plejdDeviceCommunication;
mqttClient;
2021-02-20 15:33:06 +01:00
processCleanupFunc;
sceneManager;
constructor() {
super();
this.config = Configuration.getOptions();
this.deviceRegistry = new DeviceRegistry();
this.plejdApi = new PlejdApi(this.deviceRegistry);
this.plejdDeviceCommunication = new PlejdDeviceCommunication(this.deviceRegistry);
this.sceneManager = new SceneManager(this.deviceRegistry, this.plejdDeviceCommunication);
this.mqttClient = new MqttClient(this.deviceRegistry);
}
2021-02-20 15:33:06 +01:00
cleanup() {
this.mqttClient.cleanup();
this.mqttClient.removeAllListeners();
this.plejdDeviceCommunication.cleanup();
this.plejdDeviceCommunication.removeAllListeners();
}
async init() {
logger.info('Main Plejd addon init()...');
await this.plejdApi.init();
this.sceneManager.init();
2021-02-20 15:33:06 +01:00
this.processCleanupFunc = () => {
this.cleanup();
this.processCleanupFunc = () => {};
this.mqttClient.disconnect(() => process.exit(0));
};
['SIGINT', 'SIGHUP', 'SIGTERM'].forEach((signal) => {
2021-02-20 15:33:06 +01:00
process.on(signal, this.processCleanupFunc);
});
2021-02-20 15:33:06 +01:00
this.mqttClient.on(MqttClient.EVENTS.connected, () => {
try {
logger.verbose('connected to mqtt.');
this.mqttClient.sendDiscoveryToHomeAssistant();
} catch (err) {
logger.error('Error in MqttClient.connected callback in main.js', err);
}
});
// subscribe to changes from HA
this.mqttClient.on(
MqttClient.EVENTS.stateChanged,
/** @param device {import('./types/DeviceRegistry').OutputDevice} */
(device, command) => {
try {
const { uniqueId } = device;
if (device.typeName === 'Scene') {
// we're triggering a scene, lets do that and jump out.
// since scenes aren't "real" devices.
this.sceneManager.executeScene(uniqueId);
return;
}
let state = false;
let commandObj = {};
if (typeof command === 'string') {
// switch command
state = command === 'ON';
commandObj = {
state,
};
// since the switch doesn't get any updates on whether it's on or not,
// we fake this by directly send the updateState back to HA in order for
// it to change state.
this.mqttClient.updateOutputState(uniqueId, {
state,
});
} else {
// eslint-disable-next-line prefer-destructuring
state = command.state === 'ON';
commandObj = command;
}
if (state) {
this.plejdDeviceCommunication.turnOn(uniqueId, commandObj);
} else {
this.plejdDeviceCommunication.turnOff(uniqueId, commandObj);
}
} catch (err) {
logger.error('Error in MqttClient.stateChanged callback', err);
}
},
);
this.mqttClient.init();
// subscribe to changes from Plejd
2021-02-20 15:33:06 +01:00
this.plejdDeviceCommunication.on(
PlejdDeviceCommunication.EVENTS.stateChanged,
(uniqueOutputId, command) => {
2021-02-20 15:33:06 +01:00
try {
this.mqttClient.updateOutputState(uniqueOutputId, command);
2021-02-20 15:33:06 +01:00
} catch (err) {
logger.error('Error in PlejdService.stateChanged callback', err);
2021-02-20 15:33:06 +01:00
}
},
);
this.plejdDeviceCommunication.on(PlejdDeviceCommunication.EVENTS.sceneTriggered, (sceneId) => {
try {
this.mqttClient.sceneTriggered(sceneId);
} catch (err) {
logger.error('Error in PlejdService.sceneTriggered callback', err);
}
});
await this.plejdDeviceCommunication.init();
2021-02-08 19:54:24 +01:00
logger.info('Main init done');
}
}
module.exports = PlejdAddon;