homebridge-nibe
Version:
Homebridge plugin for Nibe services
92 lines (91 loc) • 3.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccessoryDefinition = void 0;
class AccessoryDefinition {
constructor(name, version, locale, serviceResolver, log) {
this.name = name;
this.version = version;
this.locale = locale;
this.serviceResolver = serviceResolver;
this.log = log;
}
buildIdentifier(data) {
var _a;
return `${((_a = data === null || data === void 0 ? void 0 : data.device) === null || _a === void 0 ? void 0 : _a.id) || 'undefined'}::${this.name}`;
}
buildName(data) {
return `${this.name}::${Date.now()}`;
}
isCurrentVersion(platformAccessory) {
return platformAccessory.context.version >= this.version;
}
update(platformAccessory, data) {
platformAccessory.context.lastUpdate = Date.now();
}
create(platformAccessory, data) {
platformAccessory.context.accessoryId = this.buildIdentifier(data);
platformAccessory.context.version = this.version;
platformAccessory.context.systemId = data.system.systemId;
platformAccessory.context.systemName = data.system.name;
platformAccessory.context.deviceId = data.device.id;
platformAccessory.context.deviceName = data.device.name;
const accessoryInformationService = this.getOrCreateService('AccessoryInformation', platformAccessory);
accessoryInformationService.updateCharacteristic(this.serviceResolver.resolveCharacteristic('Manufacturer'), 'Nibe');
accessoryInformationService.updateCharacteristic(this.serviceResolver.resolveCharacteristic('Model'), `${data.device.name} (${data.system.name})`);
accessoryInformationService.updateCharacteristic(this.serviceResolver.resolveCharacteristic('SerialNumber'), data.device.serialNumber);
this.update(platformAccessory, data);
}
getOrCreateService(type, platformAccessory) {
const resolvedType = this.serviceResolver.resolveService(type);
return platformAccessory.getService(resolvedType) || platformAccessory.addService(resolvedType);
}
removeService(type, platformAccessory) {
const resolvedType = this.serviceResolver.resolveService(type);
const service = platformAccessory.getService(resolvedType);
if (service) {
platformAccessory.removeService(service);
}
}
updateCharacteristic(service, name, value, props = undefined) {
const characteristic = this.serviceResolver.resolveCharacteristic(name);
service.updateCharacteristic(characteristic, value);
if (props) {
service.getCharacteristic(characteristic).setProps(props);
}
}
getCharacteristicValue(service, name) {
const characteristic = this.serviceResolver.resolveCharacteristic(name);
return service.getCharacteristic(characteristic).value;
}
findParameters(parameterIds, data) {
return parameterIds
.map(id => this.findParameter(id, data))
.filter(p => p !== undefined);
}
findParameter(parameterId, data) {
if (!data || !data.parameters) {
return undefined;
}
return data.parameters.find(p => parameterId === p.id);
}
getText(key) {
return this.locale.text(key, '') || '';
}
isManageEnabled(data) {
var _a;
return ((_a = data.system.premiumSubscriptions) === null || _a === void 0 ? void 0 : _a.includes('manage')) || false;
}
putData(platformAccessory, key, data) {
if (!platformAccessory.context.data) {
platformAccessory.context.data = [];
}
platformAccessory.context.data[key] = data;
}
getData(platformAccessory, key) {
if (!platformAccessory.context.data) {
return undefined;
}
return platformAccessory.context.data[key];
}
}
exports.AccessoryDefinition = AccessoryDefinition;