From 2fb159a7db86ee971adfd5351543f55677214d32 Mon Sep 17 00:00:00 2001 From: gaosen <0x5e@sina.cn> Date: Fri, 17 Mar 2023 01:18:43 +0800 Subject: [PATCH] Add `zd` Vibration Sensor support. (#262) --- CHANGELOG.md | 1 + SUPPORTED_DEVICES.md | 2 +- src/accessory/AccessoryFactory.ts | 4 ++++ src/accessory/VibrationSensorAccessory.ts | 25 +++++++++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/accessory/VibrationSensorAccessory.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ef6f7f4..a7e671f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - Add Fingerbot support (`szjqr`). - Add Smart Lock support (`ms`, `jtmspro`). (#120) Thanks @pfgimutao for the contribution - Add Alarm Host support (`mal`). (#246) Thanks @bFollon for the contribution +- Add Vibration Sensor support (`zd`). ### Fixed diff --git a/SUPPORTED_DEVICES.md b/SUPPORTED_DEVICES.md index 225cc364..27bcadd7 100644 --- a/SUPPORTED_DEVICES.md +++ b/SUPPORTED_DEVICES.md @@ -114,7 +114,7 @@ Most category code is pinyin abbreviation of Chinese name. | Smoke Alarm | 烟雾报警传感器 | ywbj | Smoke Sensor | ✅ | [Documentation](https://developer.tuya.com/en/docs/iot/categoryywbj?id=Kaiuz3f6sf952) | | Temperature and Humidity Sensor | 温湿度传感器 | wsdcg | Temperature Sensor
Humidity Sensor | ✅ | [Documentation](https://developer.tuya.com/en/docs/iot/categorywsdcg?id=Kaiuz3hinij34) | | Contact Sensor | 门磁传感器 | mcs | Contact Sensor | ✅ | [Documentation](https://developer.tuya.com/en/docs/iot/categorymcs?id=Kaiuz3bnflmh2) | -| Vibration Sensor | 震动传感器 | zd | | | [Documentation](https://developer.tuya.com/en/docs/iot/categoryzd?id=Kaiuz3a5vrzno) | +| Vibration Sensor | 震动传感器 | zd | Motion Sensor | ✅ | [Documentation](https://developer.tuya.com/en/docs/iot/categoryzd?id=Kaiuz3a5vrzno) | | Water Detector | 水浸传感器 | sj | Leak Sensor | ✅ | [Documentation](https://developer.tuya.com/en/docs/iot/categorysj?id=Kaiuz3iub2sli) | | Luminance Sensor | 亮度传感器 | ldcg | Light Sensor | ✅ | [Documentation](https://developer.tuya.com/en/docs/iot/categoryldcg?id=Kaiuz3n7u69l8) | | Pressure Sensor | 压力传感器 | ylcg
ylcgq | | | [Documentation](https://developer.tuya.com/en/docs/iot/categoryylcg?id=Kaiuz3kc2e4gm) | diff --git a/src/accessory/AccessoryFactory.ts b/src/accessory/AccessoryFactory.ts index ea265468..955aaef0 100644 --- a/src/accessory/AccessoryFactory.ts +++ b/src/accessory/AccessoryFactory.ts @@ -38,6 +38,7 @@ import IRControlHubAccessory from './IRControlHubAccessory'; import IRGenericAccessory from './IRGenericAccessory'; import IRAirConditionerAccessory from './IRAirConditionerAccessory'; import SecuritySystemAccessory from './SecuritySystemAccessory'; +import VibrationSensorAccessory from './VibrationSensorAccessory'; export default class AccessoryFactory { @@ -142,6 +143,9 @@ export default class AccessoryFactory { case 'mcs': handler = new ContactSensorAccessory(platform, accessory); break; + case 'zd': + handler = new VibrationSensorAccessory(platform, accessory); + break; case 'rqbj': case 'jwbj': case 'sj': diff --git a/src/accessory/VibrationSensorAccessory.ts b/src/accessory/VibrationSensorAccessory.ts new file mode 100644 index 00000000..21b6e053 --- /dev/null +++ b/src/accessory/VibrationSensorAccessory.ts @@ -0,0 +1,25 @@ +import BaseAccessory from './BaseAccessory'; + +const SCHEMA_CODE = { + STATE: ['shock_state'], +}; + +export default class VibrationSensorAccessory extends BaseAccessory { + + requiredSchema() { + return [SCHEMA_CODE.STATE]; + } + + configureServices() { + const service = this.accessory.getService(this.Service.MotionSensor) + || this.accessory.addService(this.Service.MotionSensor); + + const schema = this.getSchema(...SCHEMA_CODE.STATE)!; + service.getCharacteristic(this.Characteristic.MotionDetected) + .onGet(() => { + const status = this.getStatus(schema.code)!; + return status.value !== 'normal'; + }); + } + +}