Skip to content

Commit

Permalink
Add Scene Switch support (cjkg). (#140)
Browse files Browse the repository at this point in the history
* Add Scene Switch support (`cjkg`).

* Update docs
  • Loading branch information
0x5e authored Dec 9, 2022
1 parent b7d627e commit 7561bdf
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Add Wireless Switch support (`wxkg`).
- Add Solar Light support (`tyndj`).
- Add Dehumidifier support (`cs`).
- Add Scene Switch support (`wxkg`).

### Changed
- Support Ceiling Fan icon customize and Floor Fan `lock`, `swing` feature. (#131)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Fork version of official Tuya Homebridge plugin. Brings a lot of bug fix and new
- [Dimmer] Dual Dimmer (`tgq`)
- [Dimmer] Dual Dimmer Switch (`tgkg`)
- [Switch] Scene Light Socket (`qjdcz`)
- [Switch] Scene Switch (`cjkg`)
- [StatelessProgrammableSwitch] Wireless Switch (`wxkg`)
- [Fanv2] Ceiling Fan Light (`fsd`)
- [Window] Door and Window Controller (`mc`)
Expand Down
2 changes: 1 addition & 1 deletion SUPPORTED_DEVICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Most category code is pinyin abbreviation of Chinese name.
| Switch | 开关 | kg, tdq | Switch ||
| Socket | 插座 | cz | Outlet ||
| Power Strip | 排插 | pc | Outlet ||
| Scene Switch | 场景开关 | cjkg | | |
| Scene Switch | 场景开关 | cjkg | Switch | |
| Card Switch | 插卡取电开关 | ckqdkg | | |
| Curtain Switch | 窗帘开关 | clkg | Window Covering ||
| Garage Door Opener | 车库门控制器 | ckmkzq | Garage Door Opener ||
Expand Down
4 changes: 4 additions & 0 deletions src/accessory/AccessoryFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import DimmerAccessory from './DimmerAccessory';
import OutletAccessory from './OutletAccessory';
import SwitchAccessory from './SwitchAccessory';
import WirelessSwitchAccessory from './WirelessSwitchAccessory';
import SceneSwitchAccessory from './SceneSwitchAccessory';
import FanAccessory from './FanAccessory';
import GarageDoorAccessory from './GarageDoorAccessory';
import WindowAccessory from './WindowAccessory';
Expand Down Expand Up @@ -70,6 +71,9 @@ export default class AccessoryFactory {
case 'wxkg':
handler = new WirelessSwitchAccessory(platform, accessory);
break;
case 'cjkg':
handler = new SceneSwitchAccessory(platform, accessory);
break;
case 'fs':
case 'fsd':
case 'fskg':
Expand Down
48 changes: 48 additions & 0 deletions src/accessory/SceneSwitchAccessory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { TuyaDeviceSchema, TuyaDeviceSchemaType } from '../device/TuyaDevice';
import BaseAccessory from './BaseAccessory';

export default class SceneSwitchAccessory extends BaseAccessory {

configureServices() {
const schema = this.device.schema.filter((schema) => schema.code.startsWith('switch') && schema.type === TuyaDeviceSchemaType.Boolean);
for (const _schema of schema) {
const name = (schema.length === 1) ? this.device.name : _schema.code;
this.configureSwitch(_schema, name);
}
}

configureSwitch(schema: TuyaDeviceSchema, name: string) {
if (!schema) {
return;
}

const service = this.accessory.getService(schema.code)
|| this.accessory.addService(this.Service.Switch, name, schema.code);

service.setCharacteristic(this.Characteristic.Name, name);
if (!service.testCharacteristic(this.Characteristic.ConfiguredName)) {
service.addOptionalCharacteristic(this.Characteristic.ConfiguredName); // silence warning
service.setCharacteristic(this.Characteristic.ConfiguredName, name);
}

const suffix = schema.code.replace('switch', '');
const modeSchema = this.getSchema('mode' + suffix);
service.getCharacteristic(this.Characteristic.On)
.onGet(() => {
const status = this.getStatus(schema.code)!;
return status.value as boolean;
})
.onSet((value) => {
if (modeSchema) {
const mode = this.getStatus(modeSchema.code)!;
if ((mode.value as string).startsWith('scene')) {
this.sendCommands([{ code: schema.code, value: false }]);
return;
}
}

this.sendCommands([{ code: schema.code, value: value as boolean }]);
});
}

}

0 comments on commit 7561bdf

Please sign in to comment.