Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Curtain Switch with no position feature. #27

Merged
merged 1 commit into from
Oct 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 55 additions & 15 deletions src/accessory/WindowCoveringAccessory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PlatformAccessory } from 'homebridge';
import { TuyaDeviceStatus } from '../device/TuyaDevice';
import { TuyaPlatform } from '../platform';
import BaseAccessory from './BaseAccessory';

Expand All @@ -12,19 +13,25 @@ export default class WindowCoveringAccessory extends BaseAccessory {
constructor(platform: TuyaPlatform, accessory: PlatformAccessory) {
super(platform, accessory);

if (this.getWorkState()) {
this.configurePositionState();
}

if (this.getTargetPosition()) {
this.configureCurrentPosition();
this.configureTargetPosition();
}
this.configurePositionState();
this.configureCurrentPosition();
this.configureTargetPosition();
}

configureCurrentPosition() {
this.mainService().getCharacteristic(this.Characteristic.CurrentPosition)
.onGet(() => {
if (!this.positionSupported()) {
const control = this.device.getDeviceStatus('control');
if (control?.value === 'close') {
return 0;
} else if (control?.value === 'stop') {
return 50;
} else if (control?.value === 'open') {
return 100;
}
}

const state = this.getCurrentPosition()
|| this.getTargetPosition();
let value = Math.max(0, state?.value as number);
Expand All @@ -36,14 +43,18 @@ export default class WindowCoveringAccessory extends BaseAccessory {
configurePositionState() {
this.mainService().getCharacteristic(this.Characteristic.PositionState)
.onGet(() => {
const state = this.getWorkState();
if (!state) {
return this.Characteristic.PositionState.STOPPED;
}

const current = this.getCurrentPosition();
const target = this.getTargetPosition();
if (current?.value === target?.value) {
return this.Characteristic.PositionState.STOPPED;
}

const state = this.getWorkState();
return (state?.value === 'opening') ?
return (state.value === 'opening') ?
this.Characteristic.PositionState.INCREASING :
this.Characteristic.PositionState.DECREASING;
});
Expand All @@ -52,17 +63,41 @@ export default class WindowCoveringAccessory extends BaseAccessory {
configureTargetPosition() {
this.mainService().getCharacteristic(this.Characteristic.TargetPosition)
.onGet(() => {
if (!this.positionSupported()) {
const control = this.device.getDeviceStatus('control');
if (control?.value === 'close') {
return 0;
} else if (control?.value === 'stop') {
return 50;
} else if (control?.value === 'open') {
return 100;
}
}

const state = this.getTargetPosition();
let value = Math.max(0, state?.value as number);
value = Math.min(100, value);
return value;
})
.onSet(value => {
const state = this.getTargetPosition();
this.deviceManager.sendCommands(this.device.id, [{
code: state!.code,
value: value as number,
}]);
const commands: TuyaDeviceStatus[] = [];
if (!this.positionSupported()) {
const control = this.device.getDeviceStatus('control');
if (value === 0) {
commands.push({ code: control!.code, value: 'close' });
} else if (value === 100) {
commands.push({ code: control!.code, value: 'open' });
} else {
commands.push({ code: control!.code, value: 'stop' });
}
} else {
const state = this.getTargetPosition();
commands.push({ code: state!.code, value: value as number });
}
this.deviceManager.sendCommands(this.device.id, commands);
})
.setProps({
minStep: this.positionSupported() ? 1 : 50,
});
}

Expand All @@ -79,6 +114,11 @@ export default class WindowCoveringAccessory extends BaseAccessory {
return this.device.getDeviceStatus('work_state'); // opening, closing
}

positionSupported() {
// return false;
return this.getCurrentPosition() || this.getTargetPosition();
}

/*
isMotorReversed() {
const state = this.device.getDeviceStatus('control_back_mode')
Expand Down