matterbridge-daikin-ac
Version:
Daikin AC Matterbridge plugin
186 lines • 7.75 kB
JavaScript
import { DaikinAC } from 'daikin-controller';
import { DaikinAcState } from './models/DaikinAcState.js';
class DaikinAcDevice {
ip;
connectionPromise;
platform;
daikinDevice;
isConnected = false;
currentState = undefined;
name = '';
powerUpdatedCallback;
indoorTempUpdatedCallback;
targetTempUpdatedCallback;
modeUpdatedCallback;
constructor(platform, ip, options = {}) {
this.ip = ip;
this.platform = platform;
// Create the connection promise
this.connectionPromise = new Promise((resolve, reject) => {
this.daikinDevice = new DaikinAC(this.ip, options, (err) => {
if (err) {
reject(err);
}
else {
this.isConnected = true;
resolve();
}
});
});
}
async connect() {
try {
this.platform.log.info(`Connecting to Daikin AC at ${this.ip}`);
await this.connectionPromise;
this.platform.log.info(`Connected`);
}
catch (error) {
this.platform.log.error(`Failed to connect to Daikin AC:`, error);
throw error;
}
const info = await this.getCommonBasicInfoAsync();
this.name = info.name ?? `Daikin AC ${this.ip}`;
await this.getACControlInfo();
await this.getACControlInfo();
this.updateCurrentState();
}
startUpdates(powerUpdatedCallback, modeUpdatedCallback, indoorTempUpdatedCallback, targetTempUpdatedCallback) {
this.powerUpdatedCallback = powerUpdatedCallback;
this.modeUpdatedCallback = modeUpdatedCallback;
this.indoorTempUpdatedCallback = indoorTempUpdatedCallback;
this.targetTempUpdatedCallback = targetTempUpdatedCallback;
this.daikinDevice.setUpdate(15000, this.calculateDelta.bind(this));
}
calculateDelta() {
if (this.currentState?.power !== this.daikinDevice.currentACControlInfo?.power) {
this.platform.log.info(`Power changed from ${this.currentState?.power} to ${this.daikinDevice.currentACControlInfo?.power}`);
if (this.powerUpdatedCallback && this.daikinDevice.currentACControlInfo?.power !== undefined) {
this.powerUpdatedCallback(this.daikinDevice.currentACControlInfo.power);
}
}
if (this.currentState?.mode !== this.daikinDevice.currentACControlInfo?.mode) {
this.platform.log.info(`Mode changed from ${this.currentState?.mode} to ${this.daikinDevice.currentACControlInfo?.mode}`);
if (this.modeUpdatedCallback && this.daikinDevice.currentACControlInfo?.power !== undefined && this.daikinDevice.currentACControlInfo?.mode !== undefined) {
this.modeUpdatedCallback(this.daikinDevice.currentACControlInfo.power, this.daikinDevice.currentACControlInfo.mode);
}
}
if (this.currentState?.indoorTemperature !== this.daikinDevice.currentACSensorInfo?.indoorTemperature) {
this.platform.log.info(`Indoor temperature changed from ${this.currentState?.indoorTemperature} to ${this.daikinDevice.currentACSensorInfo?.indoorTemperature}`);
if (this.indoorTempUpdatedCallback && this.daikinDevice.currentACSensorInfo?.indoorTemperature !== undefined) {
this.indoorTempUpdatedCallback(this.daikinDevice.currentACSensorInfo.indoorTemperature);
}
}
if (this.currentState?.targetTemperature !== this.daikinDevice.currentACControlInfo?.targetTemperature) {
this.platform.log.info(`Target temperature changed from ${this.currentState?.targetTemperature} to ${this.daikinDevice.currentACControlInfo?.targetTemperature}`);
if (this.targetTempUpdatedCallback &&
this.daikinDevice.currentACControlInfo?.targetTemperature !== undefined &&
this.daikinDevice.currentACControlInfo?.targetTemperature !== 'M') {
this.targetTempUpdatedCallback(this.daikinDevice.currentACControlInfo.targetTemperature);
}
}
this.updateCurrentState();
}
updateCurrentState() {
if (!this.currentState) {
this.currentState = new DaikinAcState();
}
this.currentState.indoorTemperature = this.daikinDevice.currentACSensorInfo?.indoorTemperature;
this.currentState.targetTemperature = this.daikinDevice.currentACControlInfo?.targetTemperature;
this.currentState.power = this.daikinDevice.currentACControlInfo?.power;
this.currentState.mode = this.daikinDevice.currentACControlInfo?.mode;
}
async getCommonBasicInfoAsync() {
if (!this.isConnected) {
this.platform.log.warn(`Connect needs to be called first`);
await this.connect();
}
return new Promise((resolve, reject) => {
this.daikinDevice.getCommonBasicInfo((err, data) => {
if (err) {
reject(err);
}
else {
if (data === null) {
reject(new Error('No data received from Daikin AC'));
return;
}
resolve(data);
}
});
});
}
async getACControlInfo() {
if (!this.isConnected) {
this.platform.log.warn(`Connect needs to be called first`);
await this.connect();
}
return new Promise((resolve, reject) => {
this.daikinDevice.getACControlInfo((err, data) => {
if (err) {
reject(err);
}
else {
if (data === null) {
reject(new Error('No data received from Daikin AC'));
return;
}
resolve(data);
}
});
});
}
async setACControlInfo(obj) {
if (!this.isConnected) {
this.platform.log.warn(`Connect needs to be called first`);
await this.connect();
}
return new Promise((resolve, reject) => {
this.daikinDevice.setACControlInfo(obj, (err, data) => {
if (err) {
reject(err);
}
else {
if (data === null) {
reject(new Error('No data received from Daikin AC'));
return;
}
resolve(data);
}
});
});
}
async getACSensorInfo() {
if (!this.isConnected) {
this.platform.log.warn(`Connect needs to be called first`);
await this.connect();
}
return new Promise((resolve, reject) => {
this.daikinDevice.getACSensorInfo((err, data) => {
if (err) {
reject(err);
}
else {
if (data === null) {
reject(new Error('No data received from Daikin AC'));
return;
}
resolve(data);
}
});
});
}
async setMode(mode) {
await this.setACControlInfo({ mode: mode });
}
async switchOn() {
await this.setACControlInfo({ power: true });
}
async switchOff() {
await this.setACControlInfo({ power: false });
}
async setTargetTemperature(newValue) {
await this.setACControlInfo({ targetTemperature: newValue });
}
}
export { DaikinAcDevice };
//# sourceMappingURL=DaikinAcDevice.js.map