homebridge-smartthings-ac
Version:
Control your Samsung SmartThings AC using Homebridge.
164 lines • 7.88 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SmartThingsAirConditionerAccessory = void 0;
const definitions_1 = require("hap-nodejs/dist/lib/definitions");
const defaultUpdateInterval = 15;
const defaultMinTemperature = 16;
const defaultMaxTemperature = 30;
class SmartThingsAirConditionerAccessory {
constructor(platform, accessory, deviceAdapter) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
this.platform = platform;
this.accessory = accessory;
this.deviceAdapter = deviceAdapter;
this.device = accessory.context.device;
this.deviceStatus = {
mode: 'auto',
active: false,
currentHumidity: 0,
currentTemperature: (_a = this.platform.config.minTemperature) !== null && _a !== void 0 ? _a : defaultMinTemperature,
targetTemperature: (_b = this.platform.config.minTemperature) !== null && _b !== void 0 ? _b : defaultMinTemperature,
};
this.accessory.getService(this.platform.Service.AccessoryInformation)
.setCharacteristic(this.platform.Characteristic.Manufacturer, (_c = this.device.manufacturerName) !== null && _c !== void 0 ? _c : 'unknown')
.setCharacteristic(this.platform.Characteristic.Model, (_d = this.device.name) !== null && _d !== void 0 ? _d : 'unknown')
.setCharacteristic(this.platform.Characteristic.SerialNumber, (_e = this.device.presentationId) !== null && _e !== void 0 ? _e : 'unknown');
this.service = this.accessory.getService(this.platform.Service.HeaterCooler)
|| this.accessory.addService(this.platform.Service.HeaterCooler);
this.service.setCharacteristic(this.platform.Characteristic.Name, (_f = this.device.label) !== null && _f !== void 0 ? _f : 'unkown');
this.service.getCharacteristic(this.platform.Characteristic.Active)
.onSet(this.setActive.bind(this))
.onGet(this.getActive.bind(this));
const temperatureProperties = {
maxValue: (_g = this.platform.config.maxTemperature) !== null && _g !== void 0 ? _g : defaultMaxTemperature,
minValue: (_h = this.platform.config.minTemperature) !== null && _h !== void 0 ? _h : defaultMinTemperature,
minStep: 1,
};
this.service.getCharacteristic(this.platform.Characteristic.HeatingThresholdTemperature)
.setProps(temperatureProperties)
.onGet(this.getCoolingTemperature.bind(this))
.onSet(this.setCoolingTemperature.bind(this));
this.service.getCharacteristic(this.platform.Characteristic.CoolingThresholdTemperature)
.setProps(temperatureProperties)
.onGet(this.getCoolingTemperature.bind(this))
.onSet(this.setCoolingTemperature.bind(this));
this.service.getCharacteristic(this.platform.Characteristic.TargetHeaterCoolerState)
.onGet(this.getHeaterCoolerState.bind(this))
.onSet(this.setHeaterCoolerState.bind(this));
this.service.getCharacteristic(this.platform.Characteristic.CurrentTemperature)
.onGet(this.getCurrentTemperature.bind(this));
if (this.hasCapability('relativeHumidityMeasurement')) {
this.platform.log.debug('Registering current relative humidity characteristic for device', this.device.deviceId);
this.service.getCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity)
.onGet(this.getCurrentHumidity.bind(this));
}
else {
this.platform.log.info('Current relative humidity will not be available for device', this.device.deviceId);
}
const updateInterval = (_j = this.platform.config.updateInterval) !== null && _j !== void 0 ? _j : defaultUpdateInterval;
this.platform.log.info('Update status every', updateInterval, 'secs');
this.updateStatus();
setInterval(async () => {
await this.updateStatus();
}, updateInterval * 1000);
}
hasCapability(id) {
var _a, _b, _c;
return !!((_c = (_b = (_a = this.device.components) === null || _a === void 0 ? void 0 : _a.filter((component) => component.id === 'main')) === null || _b === void 0 ? void 0 : _b.flatMap((component) => component.capabilities)) === null || _c === void 0 ? void 0 : _c.find((capabilityReference) => capabilityReference.id === id));
}
getHeaterCoolerState() {
return this.fromSmartThingsMode(this.deviceStatus.mode);
}
getCoolingTemperature() {
return this.deviceStatus.targetTemperature;
}
getActive() {
return this.deviceStatus.active;
}
getCurrentTemperature() {
return this.deviceStatus.currentTemperature;
}
getCurrentHumidity() {
return this.deviceStatus.currentHumidity;
}
async setActive(value) {
const isActive = value === 1;
try {
await this.executeCommand(isActive ? 'on' : 'off', 'switch');
this.deviceStatus.active = isActive;
}
catch (error) {
this.platform.log.error('Cannot set device active', error);
await this.updateStatus();
}
}
async setHeaterCoolerState(value) {
const mode = this.toSmartThingsMode(value);
try {
await this.executeCommand('setAirConditionerMode', 'airConditionerMode', [mode]);
this.deviceStatus.mode = mode;
}
catch (error) {
this.platform.log.error('Cannot set device mode', error);
await this.updateStatus();
}
}
async setCoolingTemperature(value) {
const targetTemperature = value;
try {
await this.executeCommand('setCoolingSetpoint', 'thermostatCoolingSetpoint', [targetTemperature]);
this.deviceStatus.targetTemperature = targetTemperature;
}
catch (error) {
this.platform.log.error('Cannot set device temperature', error);
await this.updateStatus();
}
}
toSmartThingsMode(value) {
switch (value) {
case definitions_1.TargetHeaterCoolerState.HEAT: return 'heat';
case definitions_1.TargetHeaterCoolerState.COOL: return 'cool';
case definitions_1.TargetHeaterCoolerState.AUTO: return 'auto';
}
this.platform.log.warn('Illegal heater-cooler state', value);
return 'auto';
}
fromSmartThingsMode(state) {
switch (state) {
case 'cool': return definitions_1.TargetHeaterCoolerState.COOL;
case 'auto': return definitions_1.TargetHeaterCoolerState.AUTO;
case 'heat': return definitions_1.TargetHeaterCoolerState.HEAT;
}
this.platform.log.warn('Received unknown heater-cooler state', state);
return definitions_1.TargetHeaterCoolerState.AUTO;
}
async updateStatus() {
try {
this.deviceStatus = await this.getStatus();
}
catch (error) {
this.platform.log.error('Error while fetching device status: ' + this.getErrorMessage(error));
this.platform.log.debug('Caught error', error);
}
}
getErrorMessage(error) {
if (error instanceof Error) {
return error.message;
}
return String(error);
}
async executeCommand(command, capability, commandArguments) {
await this.deviceAdapter.executeMainCommand(command, capability, commandArguments);
}
getStatus() {
return this.deviceAdapter.getStatus();
}
}
exports.SmartThingsAirConditionerAccessory = SmartThingsAirConditionerAccessory;
SmartThingsAirConditionerAccessory.requiredCapabilities = [
'switch',
'temperatureMeasurement',
'thermostatCoolingSetpoint',
'airConditionerMode',
];
//# sourceMappingURL=platformAccessory.js.map