Skip to content

Commit

Permalink
Reimplement GarageDoorAccessory
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5e committed Oct 23, 2022
1 parent 84f19e6 commit fe7754a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
- Fix access_token undefined error. (https://github.com/tuya/tuya-homebridge/issues/298#issuecomment-1278238870 by @Azukovskij )

### Accessory category specific
- Rewrite `BaseAccessory`, `SwitchAccessory`, `OutletAccessory`, `LightAccessory`, `ContactSensorAccessory`, `LeakSensorAccessory`, `SmokeSensorAccessory` reduce about 50% code size. Now writing a accessory class is much more simple.
- Rewrite `BaseAccessory`, `SwitchAccessory`, `OutletAccessory`, `LightAccessory`, `ContactSensorAccessory`, `LeakSensorAccessory`, `SmokeSensorAccessory`, `GarageDoorAccessory` reduce about 50% code size. Now writing a accessory class is much more simple.
- Legacy accessory codes moved to `src/accessory/legacy/` folder.
- [Light] Add `debounce` and command send queue, more stable during frequent operations on different characteristics.
- [Light] Fix wrong color temperature map. (https://github.com/tuya/tuya-homebridge/issues/136 by @XiaoTonyLuo and https://github.com/tuya/tuya-homebridge/pull/135 by @levidhuyvetter)
Expand Down
7 changes: 4 additions & 3 deletions src/accessory/AccessoryFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import BaseAccessory from './BaseAccessory';
import LightAccessory from './LightAccessory';
import OutletAccessory from './OutletAccessory';
import SwitchAccessory from './SwitchAccessory';
import GarageDoorAccessory from './GarageDoorAccessory';
import ContactSensorAccessory from './ContactSensorAccessory';
import LeakSensorAccessory from './LeakSensorAccessory';
import CarbonMonoxideSensorAccessory from './CarbonMonoxideSensorAccessory';
Expand Down Expand Up @@ -51,15 +52,15 @@ export default class AccessoryFactory {
case 'fskg':
// TODO Fanv2Accessory
break;
case 'ckmkzq':
handler = new GarageDoorAccessory(platform, accessory);
break;
case 'ywbj':
handler = new SmokeSensorAccessory(platform, accessory);
break;
case 'qn':
// TODO HeaterAccessory
break;
case 'ckmkzq':
// TODO GarageDoorAccessory
break;
case 'cl':
// TODO WindowCoveringAccessory
break;
Expand Down
48 changes: 48 additions & 0 deletions src/accessory/GarageDoorAccessory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { PlatformAccessory } from 'homebridge';
import { TuyaPlatform } from '../platform';
import BaseAccessory from './BaseAccessory';

export default class GarageDoorAccessory extends BaseAccessory {

constructor(platform: TuyaPlatform, accessory: PlatformAccessory) {
super(platform, accessory);

const service = this.accessory.getService(this.Service.GarageDoorOpener)
|| this.accessory.addService(this.Service.GarageDoorOpener);

service.getCharacteristic(this.Characteristic.CurrentDoorState)
.onGet(() => {
const currentStatus = this.device.getDeviceStatus('doorcontact_state')!;
const targetStatus = this.device.getDeviceStatus('switch_1')!;

if (currentStatus.value === true && targetStatus.value === true) {
return this.Characteristic.CurrentDoorState.OPEN;
} else if (currentStatus.value === false && targetStatus.value === false) {
return this.Characteristic.CurrentDoorState.CLOSED;
} else if (currentStatus.value === false && targetStatus.value === true) {
return this.Characteristic.CurrentDoorState.OPENING;
} else if (currentStatus.value === true && targetStatus.value === false) {
return this.Characteristic.CurrentDoorState.CLOSING;
}

return this.Characteristic.CurrentDoorState.STOPPED;

});

service.getCharacteristic(this.Characteristic.TargetDoorState)
.onGet(() => {
const status = this.device.getDeviceStatus('switch_1')!;
return status.value ?
this.Characteristic.TargetDoorState.OPEN :
this.Characteristic.TargetDoorState.CLOSED;
})
.onSet(value => {
this.deviceManager.sendCommands(this.device.id, [{
code: 'switch_1',
value: (value === this.Characteristic.TargetDoorState.OPEN) ? true : false,
}]);
});

}

}
4 changes: 0 additions & 4 deletions src/accessory/LegacyAccessoryFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import BaseAccessory from './BaseAccessory';
import AirPurifierAccessory from './legacy/air_purifier_accessory';
import Fanv2Accessory from './legacy/fanv2_accessory';
import HeaterAccessory from './legacy/heater_accessory';
import GarageDoorAccessory from './legacy/garagedoor_accessory';
import WindowCoveringAccessory from './legacy/window_covering_accessory';

class LegacyAccessoryWrapper {
Expand Down Expand Up @@ -49,9 +48,6 @@ export default class LegacyAccessoryFactory {
case 'qn':
handler = new HeaterAccessory(platform, accessory, device);
break;
case 'ckmkzq':
handler = new GarageDoorAccessory(platform, accessory, device);
break;
case 'cl':
handler = new WindowCoveringAccessory(platform, accessory, device);
break;
Expand Down

0 comments on commit fe7754a

Please sign in to comment.