Skip to content

Commit

Permalink
ubisys J1: support for calibration
Browse files Browse the repository at this point in the history
  • Loading branch information
felixstorm committed Sep 16, 2019
1 parent d4e924c commit f306327
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 1 deletion.
134 changes: 134 additions & 0 deletions converters/toZigbee.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const options = {
sinope: {
manufacturerCode: 0x119C,
},
ubisys: {
manufacturerCode: 0x10f2,
},
};

function getTransition(message, options) {
Expand Down Expand Up @@ -1066,6 +1069,137 @@ const converters = {
},
},

// ubisys configuration / calibration converters
ubisys_configure_j1: {
key: ['configure_j1'],
convertSet: async (entity, key, value, meta) => {
const sleepSeconds = async (s) => {
return new Promise((resolve) => setTimeout(resolve, s * 1000));
};
const writeAttrFromJson = async (attr, jsonAttr = attr, converterFunc) => {
if (jsonAttr.startsWith('ubisys')) {
jsonAttr = jsonAttr.substring(6, 1).toLowerCase + jsonAttr.substring(7);
}
if (value.hasOwnProperty(jsonAttr)) {
let attrValue = value[jsonAttr];
if (converterFunc) {
attrValue = converterFunc(attrValue);
}
const attributes = {};
attributes[attr] = attrValue;
await entity.write('closuresWindowCovering', attributes, options.ubisys);
}
};
const delayCloseOpen = value.calibrate && value.calibrate.delay_close_open ?
value.calibrate.delay_close_open : 60;
const delayFirstOpen = value.calibrate && value.calibrate.delay_first_open ?
value.calibrate.delay_first_open : delayCloseOpen;
const stepsPerSecond = value.steps_per_second || 50;
const hasCalibrate = value.hasOwnProperty('calibrate');

if (hasCalibrate) {
console.log('Starting calibration run...');
// first of all, move to top position to not confuse calibration later
await entity.command('closuresWindowCovering', 'upOpen');
await sleepSeconds(delayFirstOpen);
// cancel any running calibration
await entity.write('closuresWindowCovering', {windowCoveringMode: 0});
await sleepSeconds(2);
}
if (await writeAttrFromJson('windowCoveringType')) {
await sleepSeconds(5);
}
if (hasCalibrate) {
// reset attributes
await entity.write('closuresWindowCovering', {
installedOpenLimitLiftCm: 0,
installedClosedLimitLiftCm: 240,
installedOpenLimitTiltDdegree: 0,
installedClosedLimitTiltDdegree: 900,
ubisysLiftToTiltTransitionSteps: 0xffff,
ubisysTotalSteps: 0xffff,
ubisysLiftToTiltTransitionSteps2: 0xffff,
ubisysTotalSteps2: 0xffff,
}, options.ubisys);
// enable calibration mode
await sleepSeconds(2);
await entity.write('closuresWindowCovering', {windowCoveringMode: 0x02});
await sleepSeconds(2);
// move down a bit and back up to detect upper limit
await entity.command('closuresWindowCovering', 'downClose');
await sleepSeconds(5);
await entity.command('closuresWindowCovering', 'stop');
await sleepSeconds(2);
await entity.command('closuresWindowCovering', 'upOpen');
await sleepSeconds(10);
// move down to count steps
await entity.command('closuresWindowCovering', 'downClose');
await sleepSeconds(delayCloseOpen);
// move up to count steps in the other direction
await entity.command('closuresWindowCovering', 'upOpen');
await sleepSeconds(delayCloseOpen);
}
// now write any attribute values present in JSON
await writeAttrFromJson('configStatus');
await writeAttrFromJson('installedOpenLimitLiftCm');
await writeAttrFromJson('installedClosedLimitLiftCm');
await writeAttrFromJson('installedOpenLimitTiltDdegree');
await writeAttrFromJson('installedClosedLimitTiltDdegree');
await writeAttrFromJson('ubisysTurnaroundGuardTime');
await writeAttrFromJson('ubisysLiftToTiltTransitionSteps');
await writeAttrFromJson('ubisysTotalSteps');
await writeAttrFromJson('ubisysLiftToTiltTransitionSteps2');
await writeAttrFromJson('ubisysTotalSteps2');
await writeAttrFromJson('ubisysAdditionalSteps');
await writeAttrFromJson('ubisysInactivePowerThreshold');
await writeAttrFromJson('ubisysStartupSteps');
// some convenience functions to not have to calculate
await writeAttrFromJson('ubisysTotalSteps', 'open_to_closed_s', (s) => s * stepsPerSecond);
await writeAttrFromJson('ubisysTotalSteps2', 'closed_to_open_s', (s) => s * stepsPerSecond);
await writeAttrFromJson('ubisysLiftToTiltTransitionSteps', 'lift_to_tilt_transition_ms',
(s) => s * stepsPerSecond / 1000);
await writeAttrFromJson('ubisysLiftToTiltTransitionSteps2', 'lift_to_tilt_transition_ms',
(s) => s * stepsPerSecond / 1000);
if (hasCalibrate) {
// disable calibration mode again
await sleepSeconds(2);
await entity.write('closuresWindowCovering', {windowCoveringMode: 0x00});
await sleepSeconds(2);
// re-read and dump all relevant attributes
console.log('Calibration run completed, will now read back the results.');
converters.ubisys_configure_j1.convertGet(entity);
}
},
convertGet: async (entity, key, meta) => {
console.log(await entity.read('closuresWindowCovering', [
'windowCoveringType',
'physicalClosedLimitLiftCm',
'physicalClosedLimitTiltDdegree',
'currentPositionLiftCm',
'currentPositionTiltDdegree',
'configStatus',
'currentPositionLiftPercentage',
'currentPositionTiltPercentage',
'operationalStatus',
'installedOpenLimitLiftCm',
'installedClosedLimitLiftCm',
'installedOpenLimitTiltDdegree',
'installedClosedLimitTiltDdegree',
'windowCoveringMode',
]));
console.log(await entity.read('closuresWindowCovering', [
'ubisysTurnaroundGuardTime',
'ubisysLiftToTiltTransitionSteps',
'ubisysTotalSteps',
'ubisysLiftToTiltTransitionSteps2',
'ubisysTotalSteps2',
'ubisysAdditionalSteps',
'ubisysInactivePowerThreshold',
'ubisysStartupSteps',
], options.ubisys));
},
},

/**
* Ignore converters
*/
Expand Down
2 changes: 1 addition & 1 deletion devices.js
Original file line number Diff line number Diff line change
Expand Up @@ -4902,7 +4902,7 @@ const devices = [
description: 'Shutter control J1',
supports: 'open, close, stop, position, tilt',
fromZigbee: [fz.closuresWindowCovering_report_pos_and_tilt],
toZigbee: [tz.cover_control, tz.cover_gotopercentage],
toZigbee: [tz.cover_control, tz.cover_gotopercentage, tz.ubisys_configure_j1],
},

// Lingan
Expand Down

0 comments on commit f306327

Please sign in to comment.