nitaybz / homebridge-homeworks-serial Goto Github PK
View Code? Open in Web Editor NEWHomebridge plugin for Lutron Homeworks using Serial Port (RS232)
License: MIT License
Homebridge plugin for Lutron Homeworks using Serial Port (RS232)
License: MIT License
not working
for serial path in setup port settings have to put here:
Seconds to remove disconnected device
/dev/ttyUSB0
Time in seconds to remove a device if it has not being discovered (default is 10 minutes). set 0 to not remove accessories at all.
Log:
error: Error: connect ECONNREFUSED /tmp/app.HomeworksSerial
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '/tmp/app.HomeworksSerial'
}
[18/09/2022, 19:46:59] [HW serial] connection closed HomeworksSerial /tmp/app.HomeworksSerial Infinity tries remaining of Infinity
[18/09/2022, 19:46:59] [HW serial] IPC Client Disconnected from the Server
[18/09/2022, 19:47:00] [HW serial] requested connection to HomeworksSerial /tmp/app.HomeworksSerial
[18/09/2022, 19:47:00] [HW serial] Connecting client on Unix Socket : /tmp/app.HomeworksSerial
[18/09/2022, 19:47:00] [HW serial]
usb serial adapter is recognized in homebridge system
Hi there
First of all, thank you so much for your work to provide an optimized plugin.
The positioning of the louvers were not saved in Apple Homekit when using it on multiple devices. Also the manual measurement of the opening and close times of the louvers was not very accurate. So I tried to add this function. The automatic measurement of the open and close time of the louver will start only if the time in the homebridge configuration is set to 0. I was not able to test it by myself because of my setup, but I will as soon as possible. Until then maybe you could review my suggestions or even try them out and change it if you‘d like.
Regards, Koopa
let Characteristic, Service;
const stateManager = require('../lib/stateManager');
const OPENING = 0;
const CLOSING = 1;
const STOPPED = 2;
class WindowCovering {
constructor(device, platform) {
Service = platform.api.hap.Service;
Characteristic = platform.api.hap.Characteristic;
this.rs232 = platform.rs232;
this.log = platform.log;
this.api = platform.api;
this.cachedState = platform.cachedState;
this.address = device.address;
this.openButtonId = device.openButtonId;
this.closeButtonId = device.closeButtonId;
this.stopButtonId = device.stopButtonId;
this.louverButtonId = device.louverButtonId;
this.louverPosition = device.louverPosition || 20;
this.rawCommands = device.rawCommands;
this.rawStatus = device.rawStatus;
this.timeToOpen = device.timeToOpen || 0;
this.timeToClose = device.timeToClose || 0;
this.pressType = device.pressType || 'single';
this.id = `${device.address}.${device.name}`;
this.name = device.name || this.id;
this.serial = this.id;
this.type = device.type;
this.manufacturer = 'Lutron Homeworks';
this.model = 'Window Covering';
this.displayName = this.name;
this.louverExist = !!(this.louverButtonId || this.rawCommands.louver);
this.state = {
CurrentPosition: 0,
TargetPosition: 0,
PositionState: STOPPED,
LouverOn: false
};
this.processing = false;
this.UUID = this.api.hap.uuid.generate(this.id);
this.accessory = platform.accessories.find(accessory => accessory.UUID === this.UUID);
if (!this.accessory) {
this.log(`Creating New ${this.type} (${this.model}) Accessory: "${this.name}"`);
this.accessory = new this.api.platformAccessory(this.name, this.UUID);
this.accessory.context.type = this.type;
this.accessory.context.id = this.id;
this.accessory.context.lastState = this.state;
platform.accessories.push(this.accessory);
// register the accessory
this.api.registerPlatformAccessories(platform.PLUGIN_NAME, platform.PLATFORM_NAME, [this.accessory]);
} else {
this.log.easyDebug(`"${this.name}" is Connected!`);
this.state = this.accessory.context.lastState || {};
}
let informationService = this.accessory.getService(Service.AccessoryInformation);
if (!informationService) {
informationService = this.accessory.addService(Service.AccessoryInformation);
}
informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial);
this.addWindowCoveringService();
if (this.louverExist) {
this.addLouverSwitch();
} else {
this.removeLouverSwitch();
}
// Initiate automatic louver time measurement
if (this.timeToOpen === 0 || this.timeToClose === 0) {
this.measureLouverTimes();
}
}
// Measure the time it takes for the louver to fully open and fully close
measureLouverTimes() {
if (this.measurementInProgress) {
return;
}
this.measurementInProgress = true;
const timeout = 80000; // Time in milliseconds to wait for the louver to reach the desired position
const closeLouver = async () => {
this.toggleLouver();
await new Promise(resolve => setTimeout(resolve, timeout));
this.measureOpeningTime();
};
const openLouver = async () => {
this.toggleLouver();
await new Promise(resolve => setTimeout(resolve, timeout));
this.measureClosingTime();
};
closeLouver();
async function waitForLouverToReachPosition(targetPosition, callback) {
const checkPosition = () => {
if (this.state.LouverOn === targetPosition) {
callback();
} else {
setTimeout(checkPosition, timeout);
}
};
checkPosition();
}
function measureTimeToReachPosition(targetPosition, callback) {
const startTime = new Date().getTime();
waitForLouverToReachPosition.call(this, targetPosition, () => {
const endTime = new Date().getTime();
const elapsedTime = endTime - startTime;
callback(elapsedTime);
});
}
async function measureOpeningTime() {
measureTimeToReachPosition.call(this, true, openingTime => {
this.timeToOpen = openingTime;
this.log.easyDebug(`${this.name} - Time to fully open: ${this.timeToOpen}ms`);
openLouver();
});
}
async function measureClosingTime() {
measureTimeToReachPosition.call(this, false, closingTime => {
this.timeToClose = closingTime;
this.log.easyDebug(`${this.name} - Time to fully close: ${this.timeToClose}ms`);
this.measurementInProgress = false;
});
}
}
addWindowCoveringService() {
this.log.easyDebug(`Adding WindowCovering service for "${this.name}"`);
this.WindowCoveringService = this.accessory.getService(Service.WindowCovering) || this.accessory.addService(Service.WindowCovering, this.name, this.type);
this.WindowCoveringService.getCharacteristic(Characteristic.CurrentPosition)
.updateValue(this.state.CurrentPosition || 0);
this.WindowCoveringService.getCharacteristic(Characteristic.PositionState)
.updateValue(this.state.PositionState);
this.WindowCoveringService.getCharacteristic(Characteristic.TargetPosition)
.setProps({
minValue: 0,
maxValue: 100,
minStep: (this.timeToOpen && this.timeToClose && (this.stopButtonId || (this.rawCommands && this.rawCommands.stop))) ? 1 : 100
})
.onSet(stateManager.set.TargetPosition.bind(this, Characteristic))
.updateValue(this.state.TargetPosition || 0);
if (this.stopButtonId || (this.rawCommands && this.rawCommands.stop)) {
this.WindowCoveringService.getCharacteristic(Characteristic.HoldPosition)
.onSet(stateManager.set.HoldPosition.bind(this));
}
}
addLouverSwitch() {
this.log.easyDebug(`Adding Louver Switch Service for (${this.name})`);
const name = `${this.name} Louver`;
this.LouverSwitch = this.accessory.getService(name) || this.accessory.addService(Service.Switch, name, name);
this.LouverSwitch.getCharacteristic(Characteristic.On)
.onSet(stateManager.set.LouverOn.bind(this, Characteristic))
.updateValue(this.state.LouverOn || false);
}
removeLouverSwitch() {
// remove service
const LouverSwitch = this.accessory.getService(`${this.name} Louver`);
if (LouverSwitch) {
this.accessory.removeService(LouverSwitch);
}
}
toggleLouver() {
if (this.processing || this.louverProcessing) {
return;
}
this.state.LouverOn = !this.state.LouverOn;
this.updateValue('LouverSwitch', 'On', this.state.LouverOn);
}
updatePositionCommand(positionState) {
if (this.processing || this.louverProcessing) {
return;
}
this.log.easyDebug(`${this.name} - Detected "${positionState}" command`);
if (this.state.PositionState !== STOPPED && this.lastMove) {
clearTimeout(this.movingTimeout);
const timeInSecSinceLastMove = (new Date().getTime() - this.lastMove) / 1000;
const movedDistance = this.state.PositionState === OPENING ?
(this.timeToOpen ? Math.round(timeInSecSinceLastMove / this.timeToOpen * 100) : 0) :
(this.timeToClose ? Math.round(timeInSecSinceLastMove / this.timeToClose * 100) : 0);
this.state.CurrentPosition += movedDistance;
}
switch (positionState) {
case 'open':
this.lastMove = new Date().getTime();
this.state.TargetPosition = 100;
this.state.PositionState = OPENING;
break;
case 'close':
this.lastMove = new Date().getTime();
this.state.TargetPosition = 0;
this.state.PositionState = CLOSING;
break;
case 'stop':
this.lastMove = null;
this.state.TargetPosition = this.state.CurrentPosition;
this.state.PositionState = STOPPED;
break;
}
this.updateHomeKit(this.state);
// Set timeout to stop at position
if (this.state.PositionState !== STOPPED) {
const distance = Math.abs(this.state.TargetPosition - this.state.CurrentPosition);
const calcTime = this.state.PositionState === OPENING && this.timeToOpen ?
(distance * this.timeToOpen * 10) :
(this.state.PositionState === CLOSING && this.timeToClose ?
(distance * this.timeToClose * 10) :
2000);
this.log.easyDebug(`${this.name} - Stopping the blinds in ${calcTime / 1000} seconds`);
this.movingTimeout = setTimeout(async () => {
this.log(`${this.name} - Stopped`);
if (this.state.LouverOn) {
this.log(`${this.name} - Turning OFF Louver`);
this.state.LouverOn = false;
}
this.lastMove = null;
this.state.PositionState = STOPPED;
this.state.CurrentPosition = this.state.TargetPosition;
this.updateHomeKit(this.state);
}, calcTime);
}
}
updateHomeKit(newState) {
this.state = newState;
if (this.state.PositionState === STOPPED) {
this.updateValue('WindowCoveringService', 'CurrentPosition', this.state.CurrentPosition);
}
this.updateValue('WindowCoveringService', 'TargetPosition', this.state.TargetPosition);
this.updateValue('WindowCoveringService', 'PositionState', this.state.PositionState);
// cache last state to storage
this.accessory.context.lastState = this.state;
}
updateValue(serviceName, characteristicName, newValue) {
if (this[serviceName].getCharacteristic(Characteristic[characteristicName]).value !== newValue) {
this[serviceName].getCharacteristic(Characteristic[characteristicName]).updateValue(newValue);
this.log.easyDebug(`${this.name} (${this.id}) - Updated '${characteristicName}' for ${serviceName} with NEW VALUE: ${newValue}`);
}
}
}
module.exports = WindowCovering;
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.