homebridge-daikin-smart-ac
Version:
A Homebridge plugin for Daikin Wifi Adapter AC using Local API
271 lines • 10.4 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DaikinAC = void 0;
const async_lock_1 = __importDefault(require("async-lock"));
const axios_1 = __importDefault(require("axios"));
const Status_1 = require("./types/Status");
class DaikinAC {
constructor(log, api, ip) {
this.log = log;
this.api = api;
this.ip = ip;
this.lock = new async_lock_1.default();
this.lastUpdate = 0;
this.Characteristic = this.api.hap.Characteristic;
this.status = new Status_1.Status();
this.defaultName = 'DaikinAC';
this.mac = 'UNKNOWN';
this.MAX_COOLING_TEMPERATURE = 32;
this.MIN_COOLING_TEMPERATURE = 18;
this.MAX_HEATING_TEMPERATURE = 30;
this.MIN_HEATING_TEMPERATURE = 10;
this.MAX_SPEED = 6;
this.UUID = this.api.hap.uuid.generate(ip);
this.basicInfo = new URL(`http://${ip}/common/basic_info`);
this.setControlInfoUrl = new URL(`http://${ip}/aircon/set_control_info`);
this.getControlInfoUrl = new URL(`http://${ip}/aircon/get_control_info`);
this.sensorInfoUrl = new URL(`http://${ip}/aircon/get_sensor_info`);
}
get acStatus() {
return this.status.clone();
}
get DefaultName() {
return this.defaultName.toString();
}
get MAC() {
return this.mac.toString();
}
async setup() {
const success = await this.lock.acquire('api', async () => {
var _a;
try {
this.log.debug(`[${this.ip}] Getting device info`);
const bInfo = (_a = (await axios_1.default.get(this.basicInfo.toString()))) === null || _a === void 0 ? void 0 : _a.data;
this.log.debug(`[${this.ip}] Raw response BASIC: (${bInfo})`);
const basicInfoRaw = bInfo === null || bInfo === void 0 ? void 0 : bInfo.split(',');
if (basicInfoRaw.length <= 1) {
return false;
}
const basicInfo = this.rawToObject(basicInfoRaw);
if (basicInfo.ret !== 'OK') {
return false;
}
const { type, name, mac } = basicInfo;
if (type !== 'aircon') {
return false;
}
this.defaultName = decodeURIComponent(name);
this.mac = this.formatMAC(mac);
return true;
}
catch (error) {
this.log.error(error);
return false;
}
});
if (!success) {
return false;
}
return this.getControlInfo();
}
async getControlInfo() {
return this.lock.acquire('api', async () => {
try {
if (Date.now() - this.lastUpdate < 200) {
return true;
}
this.log.debug(`[${this.ip}] Getting device control info`);
const [cInfo, sInfo] = (await Promise.all([
axios_1.default.get(this.getControlInfoUrl.toString()),
axios_1.default.get(this.sensorInfoUrl.toString())
])).map((response) => response.data);
this.log.debug(`[${this.ip}] Raw response CONTROL: (${cInfo})`);
this.log.debug(`[${this.ip}] Raw response SENSOR: (${sInfo})`);
const controlInfoRaw = cInfo === null || cInfo === void 0 ? void 0 : cInfo.split(',');
const sensorInfoRaw = sInfo === null || sInfo === void 0 ? void 0 : sInfo.split(',');
if (controlInfoRaw.length <= 1 || sensorInfoRaw.length <= 1) {
return false;
}
const controlInfo = this.rawToObject(controlInfoRaw);
const sensorInfo = this.rawToObject(sensorInfoRaw);
if (controlInfo.ret !== 'OK' || sensorInfo.ret !== 'OK') {
return false;
}
this.status.update(controlInfo, sensorInfo);
this.lastUpdate = Date.now();
return this.status.wasSuccessUpdate;
}
catch (error) {
this.lastUpdate = Date.now();
this.log.error(error);
return false;
}
});
}
async saveControlInfo() {
return this.lock.acquire('api', async () => {
var _a, _b, _c, _d;
try {
this.log.debug(`[${this.ip}] Saving device info with: (${this.status
.toParams()
.replace('&', ',')})`);
const response = await axios_1.default.get(`${this.setControlInfoUrl.toString()}?${this.status.toParams()}`);
const isSuccess = ((_d = (_c = (_b = (_a = response.data) === null || _a === void 0 ? void 0 : _a.split(',')) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.split('=')) === null || _d === void 0 ? void 0 : _d[1]) === 'OK';
return isSuccess;
}
catch (error) {
this.log.error(error);
return false;
}
});
}
rawToObject(raw) {
return raw.reduce((acc, item) => {
const [key, value] = item.split('=');
return {
...acc,
[key]: value
};
}, {});
}
formatMAC(value) {
const len = value.length / 2;
let mac = '';
for (let i = 0; i < len; i++) {
mac += value.slice(i * 2, i * 2 + 2) + (i < len - 1 ? ':' : '');
}
return mac;
}
get TargetHeaterCoolerState() {
switch (this.status.mode) {
case Status_1.Mode.Cool:
return this.Characteristic.TargetHeaterCoolerState.COOL;
case Status_1.Mode.Heat:
return this.Characteristic.TargetHeaterCoolerState.HEAT;
default:
return this.Characteristic.TargetHeaterCoolerState.AUTO;
}
}
set TargetHeaterCoolerState(value) {
switch (value) {
case this.Characteristic.TargetHeaterCoolerState.COOL:
this.status.mode = Status_1.Mode.Cool;
break;
case this.Characteristic.TargetHeaterCoolerState.HEAT:
this.status.mode = Status_1.Mode.Heat;
break;
default:
this.status.mode = Status_1.Mode.Auto;
break;
}
}
get CurrentHeaterCoolerState() {
if (!this.status.power) {
return this.Characteristic.CurrentHeaterCoolerState.INACTIVE;
}
if (this.status.mode === Status_1.Mode.Heat) {
return this.Characteristic.CurrentHeaterCoolerState.HEATING;
}
return this.Characteristic.CurrentHeaterCoolerState.COOLING;
}
get Active() {
return this.status.power
? this.Characteristic.Active.ACTIVE
: this.Characteristic.Active.INACTIVE;
}
set Active(value) {
this.status.power = value === this.Characteristic.Active.ACTIVE;
}
get CoolingThresholdTemperature() {
var _a;
return (_a = this.status.coolTargetTemperature) !== null && _a !== void 0 ? _a : this.MIN_COOLING_TEMPERATURE;
}
set CoolingThresholdTemperature(value) {
let temp = value;
if (temp < this.MIN_COOLING_TEMPERATURE) {
temp = this.MIN_COOLING_TEMPERATURE;
}
if (temp > this.MAX_COOLING_TEMPERATURE) {
temp = this.MAX_COOLING_TEMPERATURE;
}
this.status.coolTargetTemperature = temp;
}
get HeatingThresholdTemperature() {
var _a;
return (_a = this.status.heatTargetTemperature) !== null && _a !== void 0 ? _a : this.MIN_HEATING_TEMPERATURE;
}
set HeatingThresholdTemperature(value) {
let temp = value;
if (temp < this.MIN_HEATING_TEMPERATURE) {
temp = this.MIN_HEATING_TEMPERATURE;
}
if (temp > this.MAX_HEATING_TEMPERATURE) {
temp = this.MAX_HEATING_TEMPERATURE;
}
this.status.heatTargetTemperature = temp;
}
get CurrentTemperature() {
var _a;
return (_a = this.status.roomTemperature) !== null && _a !== void 0 ? _a : 0;
}
get RotationSpeed() {
switch (this.status.fanSpeed) {
case Status_1.FanSpeed.S1:
return 2;
case Status_1.FanSpeed.S2:
return 3;
case Status_1.FanSpeed.S3:
return 4;
case Status_1.FanSpeed.S4:
return 5;
case Status_1.FanSpeed.S5:
return 6;
default:
return 1;
}
}
set RotationSpeed(value) {
switch (value) {
case 2:
this.status.fanSpeed = Status_1.FanSpeed.S1;
break;
case 3:
this.status.fanSpeed = Status_1.FanSpeed.S2;
break;
case 4:
this.status.fanSpeed = Status_1.FanSpeed.S3;
break;
case 5:
this.status.fanSpeed = Status_1.FanSpeed.S4;
break;
case 6:
this.status.fanSpeed = Status_1.FanSpeed.S5;
break;
default:
this.status.fanSpeed = Status_1.FanSpeed.Night;
break;
}
}
get SwingMode() {
switch (this.status.fanDirection) {
case Status_1.FanDirection.Vertical:
case Status_1.FanDirection.Horizontal:
case Status_1.FanDirection.VerticalAndHorizontal:
return this.Characteristic.SwingMode.SWING_ENABLED;
default:
return this.Characteristic.SwingMode.SWING_DISABLED;
}
}
set SwingMode(value) {
if (value === this.Characteristic.SwingMode.SWING_ENABLED) {
this.status.fanDirection = Status_1.FanDirection.Vertical;
return;
}
this.status.fanDirection = Status_1.FanDirection.Off;
}
}
exports.DaikinAC = DaikinAC;
//# sourceMappingURL=DaikinAC.js.map