UNPKG

homebridge-vicare

Version:
132 lines 7.25 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { Service, Characteristic } from './index.js'; export class ViCareThermostatAccessory { constructor(log, requestService, apiEndpoint, installationId, gatewaySerial, config) { this.log = log; this.requestService = requestService; this.apiEndpoint = apiEndpoint; this.installationId = installationId; this.gatewaySerial = gatewaySerial; this.name = config.name; this.feature = config.feature; this.deviceId = config.deviceId; this.maxTemp = config.maxTemp; this.type = config.type || 'temperature_sensor'; this.apiEndpoint = apiEndpoint.replace(/\/$/, ''); this.temperatureService = this.type === 'thermostat' ? new Service.Thermostat(this.name, `thermostatService_${this.name}_${this.feature}`) : new Service.TemperatureSensor(this.name, `temperatureService_${this.name}_${this.feature}`); this.temperatureService .getCharacteristic(Characteristic.CurrentTemperature) .on('get', this.getTemperature.bind(this)); this.temperatureService.getCharacteristic(Characteristic.TargetTemperature).setProps({ minValue: 0, maxValue: this.maxTemp, minStep: 1, }); // TODO: Once changing to eco mode is enabled, add `Characteristic.TargetHeatingCoolingState.OFF` this.temperatureService.getCharacteristic(Characteristic.TargetHeatingCoolingState).setProps({ minValue: Characteristic.TargetHeatingCoolingState.HEAT, maxValue: Characteristic.TargetHeatingCoolingState.HEAT, validValues: [Characteristic.TargetHeatingCoolingState.HEAT], }); // TODO: Once changing to eco mode is enabled, add `Characteristic.CurrentHeatingCoolingState.OFF` if eco mode disabled this.temperatureService.getCharacteristic(Characteristic.CurrentHeatingCoolingState).setProps({ minValue: Characteristic.CurrentHeatingCoolingState.HEAT, maxValue: Characteristic.CurrentHeatingCoolingState.HEAT, validValues: [Characteristic.CurrentHeatingCoolingState.HEAT], }); if (config.feature.includes('burners')) { this.switchService = new Service.Switch(this.name, `switchService_${this.name}_${this.feature}`); this.switchService .getCharacteristic(Characteristic.On) .on('get', this.getBurnerStatus.bind(this)) .on('set', this.setBurnerStatus.bind(this)); } this.services = [this.temperatureService]; if (this.switchService) { this.services.push(this.switchService); } } getServices() { return this.services; } getTemperature(callback) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g; const url = `${this.apiEndpoint}/features/installations/${this.installationId}/gateways/${this.gatewaySerial}/devices/${this.deviceId}/features/${this.feature}`; this.log.debug(`Fetching temperature from ${url} ...`); try { const response = yield this.requestService.authorizedRequest(url); const body = (yield response.json()); if (!response.ok) { return yield this.requestService.checkForTokenExpiration(body, url); } const data = body.data || body; if (((_c = (_b = (_a = data.commands) === null || _a === void 0 ? void 0 : _a.setTemperature) === null || _b === void 0 ? void 0 : _b.params.targetTemperature) === null || _c === void 0 ? void 0 : _c.constraints.min) !== undefined) { const { min, max, stepping } = data.commands.setTemperature.params.targetTemperature.constraints; this.temperatureService.getCharacteristic(Characteristic.TargetTemperature).setProps({ minValue: Number(min), maxValue: this.maxTemp || Number(max), minStep: Number(stepping), }); } if (((_e = (_d = data.properties) === null || _d === void 0 ? void 0 : _d.value) === null || _e === void 0 ? void 0 : _e.value) !== undefined) { const temp = data.properties.value.value; callback(null, temp); } else if (((_g = (_f = data.properties) === null || _f === void 0 ? void 0 : _f.temperature) === null || _g === void 0 ? void 0 : _g.value) !== undefined) { const temp = data.properties.temperature.value; callback(null, temp); } else { throw new Error(`Unexpected response structure: ${JSON.stringify(data, null, 2)}`); } } catch (error) { this.log.error('Error fetching temperature:', error); callback(error); } }); } getBurnerStatus(callback) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const url = `${this.apiEndpoint}/features/installations/${this.installationId}/gateways/${this.gatewaySerial}/devices/${this.deviceId}/features/${this.feature}`; this.log.debug(`Fetching burner status from ${url} ...`); try { const response = yield this.requestService.authorizedRequest(url); const body = (yield response.json()); if (!response.ok) { return yield this.requestService.checkForTokenExpiration(body, url); } const data = body.data || body; if (((_b = (_a = data.properties) === null || _a === void 0 ? void 0 : _a.active) === null || _b === void 0 ? void 0 : _b.value) !== undefined) { const isActive = data.properties.active.value; callback(null, isActive); } else { this.log.error('Unexpected response structure:', data); callback(new Error('Unexpected response structure.')); } } catch (error) { this.log.error('Error fetching burner status:', error); callback(error); } }); } setBurnerStatus(_value, callback) { callback(null); } } //# sourceMappingURL=ViCareThermostatAccessory.js.map