Added WindowCovering

This commit is contained in:
Serge Wagener 2022-02-09 20:02:31 +01:00
parent 7c5f4dd552
commit 6eceb78843

View File

@ -4,6 +4,8 @@ import {
Service, Service,
Nullable, Nullable,
} from 'homebridge'; } from 'homebridge';
import { setMaxListeners } from 'process';
import { setFlagsFromString } from 'v8';
import { SmartHomeNGPlatform } from '../platform'; import { SmartHomeNGPlatform } from '../platform';
@ -12,11 +14,8 @@ export class WindowCovering implements AccessoryPlugin {
private readonly informationService: Service; private readonly informationService: Service;
public name: string; public name: string;
private currentPosition = 0; private currentPosition = 0; private currentPositionMin = 0; private currentPositionMax = 100; private currentPositionInverted = false;
private currentPositionMin = 0; private targetPosition = 0; private targetPositionMin = 0; private targetPositionMax = 100; private targetPositionInverted = false;
private currentPositionMax = 100;
private currentPositionInverted = false;
private targetPosition = 0;
private positionState = this.platform.Characteristic.PositionState.STOPPED; private positionState = this.platform.Characteristic.PositionState.STOPPED;
constructor(private readonly platform: SmartHomeNGPlatform, private readonly accessory) { constructor(private readonly platform: SmartHomeNGPlatform, private readonly accessory) {
@ -40,11 +39,15 @@ export class WindowCovering implements AccessoryPlugin {
.setCharacteristic(this.platform.Characteristic.Model, accessory.model) .setCharacteristic(this.platform.Characteristic.Model, accessory.model)
.setCharacteristic(this.platform.Characteristic.SerialNumber, accessory.currentposition); .setCharacteristic(this.platform.Characteristic.SerialNumber, accessory.currentposition);
this.platform.shng.addMonitor(accessory.currentposition, this.shngCallback.bind(this)); this.platform.shng.addMonitor(accessory.currentposition, this.shngCurrentPositionCallback.bind(this));
this.platform.shng.addMonitor(accessory.targetposition, this.shngTargetPositionCallback.bind(this));
this.currentPositionMax = accessory.currentpositionmax ? accessory.currentpositionmax : this.currentPositionMax; this.currentPositionMax = accessory.currentpositionmax ? accessory.currentpositionmax : this.currentPositionMax;
this.currentPositionMin = accessory.currentpositionmin ? accessory.currentpositionmin : this.currentPositionMin; this.currentPositionMin = accessory.currentpositionmin ? accessory.currentpositionmin : this.currentPositionMin;
this.currentPositionInverted = accessory.currentpositioninverted ? accessory.currentpositioninverted : this.currentPositionInverted; this.currentPositionInverted = accessory.currentpositioninverted ? accessory.currentpositioninverted : this.currentPositionInverted;
this.targetPositionMax = accessory.targetpositionmax ? accessory.targetpositionmax : this.targetPositionMax;
this.targetPositionMin = accessory.targetpositionmin ? accessory.targetpositionmin : this.targetPositionMin;
this.targetPositionInverted = accessory.targetpositioninverted ? accessory.targetpositioninverted : this.targetPositionInverted;
this.platform.log.info("WindowCovering '%s' created!", accessory.name); this.platform.log.info("WindowCovering '%s' created!", accessory.name);
} }
@ -67,7 +70,6 @@ export class WindowCovering implements AccessoryPlugin {
} }
getTargetPosition(): Nullable<CharacteristicValue> { getTargetPosition(): Nullable<CharacteristicValue> {
this.targetPosition = this.currentPosition;
this.platform.log.info('getTargetPosition:', this.accessory.name, 'is currently', this.targetPosition); this.platform.log.info('getTargetPosition:', this.accessory.name, 'is currently', this.targetPosition);
return this.targetPosition; return this.targetPosition;
} }
@ -75,11 +77,17 @@ export class WindowCovering implements AccessoryPlugin {
setTargetPosition(value: CharacteristicValue) { setTargetPosition(value: CharacteristicValue) {
this.targetPosition = value as number; this.targetPosition = value as number;
this.platform.log.info('SetOn:', this.accessory.name, 'was set to', this.targetPosition); this.platform.log.info('SetOn:', this.accessory.name, 'was set to', this.targetPosition);
//this.platform.shng.setItem(this.accessory.targetPosition, this.targetPosition); const transposedTarget = this.convertRange(
value as number,
0, 100,
this.targetPositionMin, this.targetPositionMax,
this.targetPositionInverted,
);
this.platform.shng.setItem(this.accessory.targetposition, transposedTarget);
} }
shngCallback(value: unknown): void { shngCurrentPositionCallback(value: unknown): void {
this.platform.log.debug('shngCallback:', this.accessory.name, '=', value, '(' + typeof value + ')'); this.platform.log.error('shngCurrentPositionCallback:', this.accessory.name, '=', value, '(' + typeof value + ')');
if (typeof value === 'number') { if (typeof value === 'number') {
this.currentPosition = this.convertRange( this.currentPosition = this.convertRange(
value as number, value as number,
@ -91,6 +99,34 @@ export class WindowCovering implements AccessoryPlugin {
} else { } else {
this.platform.log.warn('Unknown type ', typeof value, 'received for', this.accessory.name + ':', value); this.platform.log.warn('Unknown type ', typeof value, 'received for', this.accessory.name + ':', value);
} }
this.updateDirection();
}
shngTargetPositionCallback(value: unknown): void {
this.platform.log.error('shngTargetPositionCallback:', this.accessory.name, '=', value, '(' + typeof value + ')');
if (typeof value === 'number') {
this.targetPosition = this.convertRange(
value as number,
this.targetPositionMin, this.targetPositionMax,
0, 100,
this.targetPositionInverted,
);
this.deviceService.updateCharacteristic(this.platform.Characteristic.TargetPosition, this.targetPosition);
} else {
this.platform.log.warn('Unknown type ', typeof value, 'received for', this.accessory.name + ':', value);
}
this.updateDirection();
}
updateDirection() {
if (this.targetPosition < this.currentPosition) {
this.positionState = this.platform.Characteristic.PositionState.DECREASING;
} else if (this.targetPosition > this.currentPosition) {
this.positionState = this.platform.Characteristic.PositionState.INCREASING;
} else {
this.positionState = this.platform.Characteristic.PositionState.STOPPED;
}
} }
convertRange(value: number, oldmin: number, oldmax: number, newmin: number, newmax: number, inverted: boolean): number { convertRange(value: number, oldmin: number, oldmax: number, newmin: number, newmax: number, inverted: boolean): number {
@ -98,6 +134,13 @@ export class WindowCovering implements AccessoryPlugin {
if (inverted) { if (inverted) {
result = newmax - result; result = newmax - result;
} }
this.platform.log.warn(
'Transposing', value,
'from range', oldmin, '-', oldmax,
'to', newmin, '-', newmax,
'with inverted', inverted,
'=', result,
);
return result; return result;
} }
} }