homebridge-tasmota-control
Version:
Homebridge plugin to control Tasmota flashed devices.
237 lines (206 loc) • 10.2 kB
JavaScript
import { promises as fsPromises } from 'fs';
import axios from 'axios';
import EventEmitter from 'events';
import ImpulseGenerator from './impulsegenerator.js';
import { ApiCommands } from './constants.js';
let Accessory, Characteristic, Service, Categories, AccessoryUUID;
class Switches extends EventEmitter {
constructor(api, config, info, serialNumber, refreshInterval) {
super();
Accessory = api.platformAccessory;
Characteristic = api.hap.Characteristic;
Service = api.hap.Service;
Categories = api.hap.Categories;
AccessoryUUID = api.hap.uuid;
//info
this.info = info;
this.serialNumber = serialNumber;
this.relaysCount = info.friendlyNames.length;
//other config
this.relaysDisplayType = config.relaysDisplayType || 0;
this.relaysNamePrefix = config.relaysNamePrefix || false;
this.enableDebugMode = config.enableDebugMode || false;
this.disableLogInfo = config.disableLogInfo || false;
this.disableLogDeviceInfo = config.disableLogDeviceInfo || false;
this.refreshInterval = refreshInterval;
//variable
this.startPrepareAccessory = true;
//axios instance
const url = `http://${config.host}/cm?cmnd=`;
this.axiosInstance = axios.create({
method: 'GET',
baseURL: url,
timeout: refreshInterval > 10000 ? 10000 : refreshInterval,
withCredentials: config.auth,
auth: {
username: config.user,
password: config.passwd
}
});
//impulse generator
this.impulseGenerator = new ImpulseGenerator();
this.impulseGenerator.on('checkDeviceState', async () => {
try {
await this.checkDeviceState();
} catch (error) {
this.emit('error', `Impulse generator error: ${error}`);
}
}).on('state', (state) => {
const emitState = state ? this.emit('success', `Impulse generator started`) : this.emit('warn', `Impulse generator stopped`);
});
}
async checkDeviceState() {
const debug = this.enableDebugMode ? this.emit('debug', `Requesting status`) : false;
try {
//power status
const powerStatusData = await this.axiosInstance(ApiCommands.PowerStatus);
const powerStatus = powerStatusData.data ?? {};
const debug = this.enableDebugMode ? this.emit('debug', `Power status: ${JSON.stringify(powerStatus, null, 2)}`) : false;
//relays
const relaysCount = this.relaysCount;
if (relaysCount > 0) {
this.switchesOutlets = [];
for (let i = 0; i < relaysCount; i++) {
const friendlyName = this.info.friendlyNames[i];
const powerNr = i + 1;
const powerKey = relaysCount === 1 ? 'POWER' : `POWER${powerNr}`;
const power = powerStatus[powerKey] === 'ON';
//push to array
const switchOutlet = {
friendlyName: friendlyName,
power: power
};
this.switchesOutlets.push(switchOutlet);
//update characteristics
const service = this.lightServices?.[i];
if (service) {
const serviceName = this.relaysNamePrefix ? `${this.info.deviceName} ${friendlyName}` : friendlyName;
service.updateCharacteristic(Characteristic.ConfiguredName, serviceName);
service.updateCharacteristic(Characteristic.On, power);
}
//log info
if (!this.disableLogInfo) {
this.emit('info', `${friendlyName}, state: ${power ? 'ON' : 'OFF'}`);
}
}
}
return true;
} catch (error) {
throw new Error(`Check state error: ${error}`);
}
}
async saveData(path, data) {
try {
data = JSON.stringify(data, null, 2);
await fsPromises.writeFile(path, data);
const debug = !this.enableDebugMode ? false : this.emit('debug', `Saved data: ${data}`);
return true;
} catch (error) {
throw new Error(`Save data error: ${error}`);
}
}
async readData(path) {
try {
const data = await fsPromises.readFile(path);
return data;
} catch (error) {
throw new Error(`Read data error: ${error}`);
}
}
async startImpulseGenerator() {
try {
//start impulse generator
const timers = [{ name: 'checkDeviceState', sampling: this.refreshInterval }];
await this.impulseGenerator.start(timers);
return true;
} catch (error) {
throw new Error(`Impulse generator start error: ${error}`);
}
}
async deviceInfo() {
this.emit('devInfo', `----- ${this.info.deviceName} -----`);
this.emit('devInfo', `Manufacturer: Tasmota`);
this.emit('devInfo', `Hardware: ${this.info.modelName}`);
this.emit('devInfo', `Serialnr: ${this.serialNumber}`)
this.emit('devInfo', `Firmware: ${this.info.firmwareRevision}`);
this.emit('devInfo', `Relays: ${this.relaysCount}`);
this.emit('devInfo', `----------------------------------`);
return;
}
//prepare accessory
async prepareAccessory() {
const debug = this.enableDebugMode ? this.emit('debug', `Prepare Accessory`) : false;
try {
//accessory
const accessoryName = this.info.deviceName;
const accessoryUUID = AccessoryUUID.generate(this.serialNumber);
const accessoryCategory = this.relaysDisplayType === 0 ? Categories.OUTLET : Categories.SWITCH;
const accessory = new Accessory(accessoryName, accessoryUUID, accessoryCategory);
//Prepare information service
const debug1 = this.enableDebugMode ? this.emit('debug', `Prepare Information Service`) : false;
accessory.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, 'Tasmota')
.setCharacteristic(Characteristic.Model, this.info.modelName ?? 'Model Name')
.setCharacteristic(Characteristic.SerialNumber, this.serialNumber ?? 'Serial Number')
.setCharacteristic(Characteristic.FirmwareRevision, this.info.firmwareRevision.replace(/[a-zA-Z]/g, '') ?? '0')
.setCharacteristic(Characteristic.ConfiguredName, accessoryName);
//Prepare services
const debug2 = this.enableDebugMode ? this.emit('debug', `Prepare Services`) : false;
if (this.switchesOutlets.length > 0) {
const debug = this.enableDebugMode ? this.emit('debug', `Prepare Switch/Outlet Services`) : false;
this.switchOutletServices = [];
for (let i = 0; i < this.switchesOutlets.length; i++) {
const friendlyName = this.switchesOutlets[i].friendlyName;
const serviceName = this.relaysNamePrefix ? `${accessoryName} ${friendlyName}` : friendlyName;
const serviceSwitchOutlet = [Service.Outlet, Service.Switch][this.relaysDisplayType];
const switchOutletService = accessory.addService(serviceSwitchOutlet, serviceName, `Power ${i}`)
switchOutletService.setPrimaryService(true);
switchOutletService.addOptionalCharacteristic(Characteristic.ConfiguredName);
switchOutletService.setCharacteristic(Characteristic.ConfiguredName, serviceName);
switchOutletService.getCharacteristic(Characteristic.On)
.onGet(async () => {
const state = this.switchesOutlets[i].power ?? false;
return state;
})
.onSet(async (state) => {
try {
const relayNr = i + 1;
const powerOn = this.switchesOutlets.length === 1 ? ApiCommands.PowerOn : `${ApiCommands.Power}${relayNr}${ApiCommands.On}`;
const powerOff = this.switchesOutlets.length === 1 ? ApiCommands.PowerOff : `${ApiCommands.Power}${relayNr}${ApiCommands.Off}`;
state = state ? powerOn : powerOff;
await this.axiosInstance(state);
const logInfo = this.disableLogInfo ? false : this.emit('info', `${friendlyName}, set state: ${state ? 'ON' : 'OFF'}`);
} catch (error) {
this.emit('warn', `${friendlyName}, set state error: ${error}`);
}
});
this.switchOutletServices.push(switchOutletService);
}
};
return accessory;
} catch (error) {
throw new Error(`Prepare accessory error: ${error}`)
}
}
//start
async start() {
try {
//check device state
await this.checkDeviceState();
//connect to deice success
this.emit('success', `Connect Success`)
//check device info
const devInfo = !this.disableLogDeviceInfo ? await this.deviceInfo() : false;
//start prepare accessory
if (this.startPrepareAccessory) {
const accessory = await this.prepareAccessory();
const publishAccessory = this.emit('publishAccessory', accessory);
this.startPrepareAccessory = false;
}
return true;
} catch (error) {
throw new Error(`Start error: ${error}`);
}
}
}
export default Switches;