homebridge-fenix-tft-wifi
Version:
Homebridge plugin which adds a Fenix TFT WiFi thermostat as HomeKit device.
206 lines • 11.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FenixTFTThermostatPlatformAccessory = void 0;
const ThermostatMode_1 = require("./Enum/ThermostatMode");
const colors_1 = require("./colors");
class FenixTFTThermostatPlatformAccessory {
constructor(platform, accessory, tApi, temperatureUnit, temperatureCheckInterval) {
this.platform = platform;
this.accessory = accessory;
this.tApi = tApi;
this.temperatureUnit = temperatureUnit;
this.temperatureCheckInterval = temperatureCheckInterval;
this.logger = platform.log;
this.name = platform.config.name;
this.service = this.accessory.getService(this.platform.api.hap.Service.Thermostat)
|| this.accessory.addService(this.platform.api.hap.Service.Thermostat);
}
initialize() {
this.debug('Initializing Fenix TFT accessory');
this.updateValues()
.then(() => {
this.service.getCharacteristic(this.platform.Characteristic.CurrentHeatingCoolingState)
.onGet(this.handleCurrentHeatingCoolingStateGet.bind(this))
.setProps({
validValues: [
this.platform.Characteristic.CurrentHeatingCoolingState.OFF,
this.platform.Characteristic.CurrentHeatingCoolingState.HEAT,
],
});
this.service.getCharacteristic(this.platform.Characteristic.TargetHeatingCoolingState)
.onGet(this.handleTargetHeatingCoolingStateGet.bind(this))
.onSet(this.handleTargetHeatingCoolingStateSet.bind(this))
.setProps({
validValues: [
this.platform.Characteristic.TargetHeatingCoolingState.OFF,
this.platform.Characteristic.TargetHeatingCoolingState.HEAT,
this.platform.Characteristic.TargetHeatingCoolingState.COOL,
this.platform.Characteristic.TargetHeatingCoolingState.AUTO,
],
});
this.service.getCharacteristic(this.platform.Characteristic.CurrentTemperature)
.onGet(this.handleCurrentTemperatureGet.bind(this));
this.debug('Setting unit ' +
this.cyanize(this.stringifyUnit));
this.service.getCharacteristic(this.platform.Characteristic.TargetTemperature)
.onGet(this.handleTargetTemperatureGet.bind(this))
.onSet(this.handleTargetTemperatureSet.bind(this))
.setProps({
minValue: this.temperatureUnit === this.platform.Characteristic.TemperatureDisplayUnits.CELSIUS ? 5 : 0,
maxValue: this.temperatureUnit === this.platform.Characteristic.TemperatureDisplayUnits.CELSIUS ? 35 : 1000,
minStep: this.temperatureUnit === this.platform.Characteristic.TemperatureDisplayUnits.CELSIUS ? 0.5 : 5,
});
this.service.getCharacteristic(this.platform.Characteristic.TemperatureDisplayUnits)
.onGet(this.handleTemperatureDisplayUnitsGet.bind(this));
setInterval(() => {
this.updateValues()
.then(() => this.debug('Value updates was successfully'))
.catch((error) => this.error(`Is not possible to update values ${error}`));
}, this.temperatureCheckInterval);
}).catch((error) => this.error(`Is not possible to update values ${error}`));
}
cToF(celsius) {
const converted = celsius * 9 / 5 + 32;
this.debug('Converting ' + this.cyanize(celsius + '°C') + `${colors_1.GREY} to ${colors_1.RESET}${this.cyanize(converted + '°F')}`);
return converted;
}
fToC(fahrenheit) {
const converted = Math.round((((fahrenheit - 32) * 5 / 9) + Number.EPSILON) * 100) / 100;
this.debug('Converting ' + this.cyanize(fahrenheit + '°F') + `${colors_1.GREY} to ${colors_1.RESET}${this.cyanize(converted + '°C')}`);
return converted;
}
handleCurrentHeatingCoolingStateGet() {
var _a, _b;
this.debug('Triggered GET CurrentHeatingCoolingState');
const temp = (_b = (_a = this.thermostatData) === null || _a === void 0 ? void 0 : _a.currentHeatingCoolingState) !== null && _b !== void 0 ? _b : this.platform.Characteristic.CurrentHeatingCoolingState.OFF;
this.debug('Current heating cooling state' + this.cyanize(temp));
return temp;
}
handleTargetHeatingCoolingStateGet() {
var _a, _b;
const temp = (_b = (_a = this.thermostatData) === null || _a === void 0 ? void 0 : _a.targetHeatingCoolingState) !== null && _b !== void 0 ? _b : this.platform.Characteristic.TargetHeatingCoolingState.OFF;
this.debug('Target heating cooling state' + this.cyanize(temp));
return temp;
}
handleTargetHeatingCoolingStateSet(value) {
var _a;
this.info(`Changing thermostat mode from ${this.cyanize((_a = this.thermostatData) === null || _a === void 0 ? void 0 : _a.mode)} to ${this.cyanize(value)}`);
if (!this.thermostatData) {
this.warning('Thermostat data was not found');
return;
}
switch (value) {
case this.platform.Characteristic.TargetHeatingCoolingState.COOL:
this.thermostatData.mode = ThermostatMode_1.ThermostatMode.ANTIFREEZE;
this.debug('Setting ' + this.cyanize('Antifreeze mode'));
break;
case this.platform.Characteristic.TargetHeatingCoolingState.AUTO:
this.thermostatData.mode = ThermostatMode_1.ThermostatMode.AUTO;
this.debug('Setting ' + this.cyanize('Auto mode'));
break;
case this.platform.Characteristic.TargetHeatingCoolingState.OFF:
this.thermostatData.mode = ThermostatMode_1.ThermostatMode.OFF;
this.debug('Setting ' + this.cyanize('Off mode'));
break;
default:
this.thermostatData.mode = ThermostatMode_1.ThermostatMode.MANUAL;
this.debug('Setting ' + this.cyanize('Manual mode'));
this.tApi.setTemperature(this.thermostatData)
.then(() => this.info(`${colors_1.GREEN}Manual mode was set${colors_1.RESET}`))
.catch(() => this.error('Cannot to set mode for thermostat ' + this.accessory.displayName));
return;
}
this.tApi.changeMode(this.thermostatData.mode)
.then(() => this.info(`${colors_1.GREEN}Mode was set${colors_1.RESET}`))
.catch(() => this.error('Cannot to set mode for thermostat ' + this.accessory.displayName));
}
handleCurrentTemperatureGet() {
var _a, _b, _c, _d;
if (this.temperatureUnit === this.platform.Characteristic.TemperatureDisplayUnits.CELSIUS) {
if (!((_a = this.thermostatData) === null || _a === void 0 ? void 0 : _a.actualTemperature)) {
this.debug('Current temperature ' + this.cyanize('0' + this.stringifyUnit));
return 0;
}
const temp = this.fToC((_b = this.thermostatData) === null || _b === void 0 ? void 0 : _b.actualTemperature);
this.debug('Current temperature ' + this.cyanize(temp + this.stringifyUnit));
return temp;
}
const temp = (_d = (_c = this.thermostatData) === null || _c === void 0 ? void 0 : _c.actualTemperature) !== null && _d !== void 0 ? _d : 0;
this.debug('Current temperature ' + this.cyanize(temp + this.stringifyUnit));
return temp;
}
handleTargetTemperatureGet() {
var _a, _b, _c, _d;
if (this.temperatureUnit === this.platform.Characteristic.TemperatureDisplayUnits.CELSIUS) {
if (!((_a = this.thermostatData) === null || _a === void 0 ? void 0 : _a.requiredTemperature)) {
this.debug('Target temperature is ' + this.cyanize('0' + this.stringifyUnit));
return 0;
}
const temp = this.fToC((_b = this.thermostatData) === null || _b === void 0 ? void 0 : _b.requiredTemperature);
this.debug('Target temperature is ' + this.cyanize(temp + this.stringifyUnit));
return temp;
}
const temp = (_d = (_c = this.thermostatData) === null || _c === void 0 ? void 0 : _c.requiredTemperature) !== null && _d !== void 0 ? _d : 0;
this.debug('Target temperature is ' + this.cyanize(temp + this.stringifyUnit));
return temp;
}
get stringifyUnit() {
return this.temperatureUnit === this.platform.Characteristic.TemperatureDisplayUnits.CELSIUS ? '°C' : '°F';
}
handleTargetTemperatureSet(value) {
this.info('Target temperature was set on ' + this.cyanize(value + this.stringifyUnit));
const targetTemperature = value;
if (this.temperatureUnit === this.platform.Characteristic.TemperatureDisplayUnits.CELSIUS) {
value = this.cToF(value);
}
if (this.thermostatData === undefined) {
this.error('Thermostat data was not found');
return;
}
if (value === this.thermostatData.requiredTemperature) {
this.debug('Temperature is same as actually required');
return;
}
this.thermostatData.requiredTemperature = value;
this.tApi.setTemperature(this.thermostatData)
.then(() => this.info(`${colors_1.GREEN}Temperature was set on ${targetTemperature}${this.stringifyUnit}${colors_1.RESET}`))
.catch(() => this.error('Cannot to set temperature for thermostat ' + this.accessory.displayName));
}
handleTemperatureDisplayUnitsGet() {
return this.temperatureUnit;
}
async updateValues() {
this.debug('Update Fenix TFT accessory');
this.thermostatData = await this.tApi.getInformation();
const informationService = this.accessory.getService(this.platform.api.hap.Service.AccessoryInformation);
if (informationService) {
informationService
.setCharacteristic(this.platform.api.hap.Characteristic.Manufacturer, 'Fenix Trading s.r.o.')
.setCharacteristic(this.platform.api.hap.Characteristic.Model, 'Fenix TFT Wifi ' + this.thermostatData.model)
.setCharacteristic(this.platform.api.hap.Characteristic.SerialNumber, this.thermostatData.softwareVersion);
}
}
debug(message) {
this.logger.debug(this.baseLogMessage + `${colors_1.GREY}${message}${colors_1.RESET}`);
}
info(message) {
this.logger.info(this.baseLogMessage + message);
}
warning(message) {
this.logger.warn(this.baseLogMessage + message);
}
error(message) {
this.logger.error(this.baseLogMessage + message);
}
get baseLogMessage() {
return `${colors_1.LIGHT_GREY}[${this.accessory.context.device.uuid}]${colors_1.RESET} ${colors_1.BLUE}[${this.accessory.displayName}]: ${colors_1.RESET}`;
}
cyanize(value) {
if (value === undefined) {
return '';
}
return `${colors_1.CYAN}${value}${colors_1.RESET}`;
}
}
exports.FenixTFTThermostatPlatformAccessory = FenixTFTThermostatPlatformAccessory;
//# sourceMappingURL=platformAccessory.js.map