homebridge-zaptec
Version:
Control your Zaptec EV chargers
139 lines • 6.83 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChargingStationAccessory = void 0;
const uuid = __importStar(require("uuid"));
var ChargerOperationMode;
(function (ChargerOperationMode) {
ChargerOperationMode["NOT_CONNECTED"] = "1";
ChargerOperationMode["REQUESTING_TO_CHARGE"] = "2";
ChargerOperationMode["CHARGING"] = "3";
ChargerOperationMode["CHARGING_CURRENT"] = "4";
ChargerOperationMode["FINISHED"] = "5";
})(ChargerOperationMode || (ChargerOperationMode = {}));
/**
* Platform Accessory
* An instance of this class is created for each accessory your platform registers
* Each accessory may expose multiple services of different service types.
*/
class ChargingStationAccessory {
constructor(platform, accessory) {
var _a, _b, _c;
this.platform = platform;
this.accessory = accessory;
this.chargerState = {
chargerOperationMode: ChargerOperationMode.NOT_CONNECTED,
chargeCurrentSet: "32",
totalChargePower: "0",
sourceVersion: "unknown",
};
this.fetchChargeState = async () => {
const state = (await this.platform.zaptec.getChargerState(this.chargerId));
this.chargerState = state;
if (this.platform.config.debug) {
this.platform.log.info("State", state);
}
this.populateServices();
};
this.populateServices = () => {
const opMode = this.chargerState.chargerOperationMode;
this.pluggedContactSensor.updateCharacteristic(this.platform.Characteristic.ContactSensorState, opMode !== ChargerOperationMode.NOT_CONNECTED);
this.temperatureSensor.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.temperature);
this.chargerSwitch.updateCharacteristic(this.platform.Characteristic.On, opMode === ChargerOperationMode.CHARGING);
};
const { charger } = this.accessory.context;
// Accessory information
this.accessory
.getService(this.platform.Service.AccessoryInformation)
.setCharacteristic(this.platform.Characteristic.Manufacturer, "Zaptec")
.setCharacteristic(this.platform.Characteristic.Model, (_a = charger.installationName) !== null && _a !== void 0 ? _a : "N/A")
.setCharacteristic(this.platform.Characteristic.SerialNumber, (_b = charger.deviceId) !== null && _b !== void 0 ? _b : "N/A")
.setCharacteristic(this.platform.Characteristic.FirmwareRevision, this.chargerState.sourceVersion);
const UUID_PLUGGED_CONTACT_SENSOR = uuid.v5("plugged-contact", this.accessory.UUID);
const UUID_TEMPERATURE_SENSOR = uuid.v5("temperature", this.accessory.UUID);
const UUID_CHARGER_SWITCH = uuid.v5("charger-switch", this.accessory.UUID);
// Setup services
this.pluggedContactSensor =
this.accessory.getService("Plugged sensor") ||
this.accessory.addService(this.platform.Service.ContactSensor, "Plugged sensor", UUID_PLUGGED_CONTACT_SENSOR);
this.temperatureSensor =
this.accessory.getService("Temperature sensor") ||
this.accessory.addService(this.platform.Service.TemperatureSensor, "Temperature sensor", UUID_TEMPERATURE_SENSOR);
this.chargerSwitch =
this.accessory.getService("Charging Switch") ||
this.accessory.addService(this.platform.Service.Switch, "Charging Switch", UUID_CHARGER_SWITCH);
// On switching on charger
this.chargerSwitch
.getCharacteristic(this.platform.Characteristic.On)
.onSet(async (value) => {
if (value === false) {
await this.platform.zaptec.api.chargersSendCommandCreate(this.chargerId, 506);
this.chargerSwitch.updateCharacteristic(this.platform.Characteristic.On, 0);
}
else if (value === true) {
await this.platform.zaptec.api.chargersSendCommandCreate(this.chargerId, 507);
this.chargerSwitch.updateCharacteristic(this.platform.Characteristic.On, 1);
}
});
// 60s interval fetch state by default
setInterval(this.fetchChargeState, ((_c = this.platform.config.refreshInterval) !== null && _c !== void 0 ? _c : 60) * 1000);
this.fetchChargeState();
}
get chargerId() {
return this.accessory.context.charger.id;
}
get chargingState() {
const mode = this.chargerState.chargerOperationMode;
if (mode === ChargerOperationMode.CHARGING) {
return this.platform.Characteristic.ChargingState.CHARGING;
}
if (mode === ChargerOperationMode.NOT_CONNECTED) {
return this.platform.Characteristic.ChargingState.NOT_CHARGEABLE;
}
return this.platform.Characteristic.ChargingState.NOT_CHARGING;
}
get temperature() {
const { temperatureInternal5, temperatureEMeterPhase1, temperatureEMeterPhase2, temperatureEMeterPhase3, temperatureTM, temperatureTM2, } = this.chargerState;
if (temperatureEMeterPhase1) {
if (temperatureEMeterPhase2 && temperatureEMeterPhase3) {
return ((Number(temperatureEMeterPhase1) +
Number(temperatureEMeterPhase2) +
Number(temperatureEMeterPhase3)) /
3);
}
else {
return Number(temperatureEMeterPhase1);
}
}
else if (temperatureTM) {
return Number(temperatureTM);
}
else if (temperatureTM2) {
return Number(temperatureTM);
}
else if (temperatureInternal5) {
return Number(temperatureInternal5);
}
return 0;
}
}
exports.ChargingStationAccessory = ChargingStationAccessory;
//# sourceMappingURL=charging-station-accessory.js.map