@@ -10,11 +10,23 @@ import { Buffer } from 'node:buffer'
1010import { SwitchbotDevice } from '../device.js'
1111import { SwitchBotBLEModel , SwitchBotBLEModelFriendlyName , SwitchBotBLEModelName } from '../types/types.js'
1212
13+ const HUMIDIFIER_COMMAND_HEADER = '5701'
14+ const TURN_ON_KEY = `${ HUMIDIFIER_COMMAND_HEADER } 0101`
15+ const TURN_OFF_KEY = `${ HUMIDIFIER_COMMAND_HEADER } 0102`
16+ const INCREASE_KEY = `${ HUMIDIFIER_COMMAND_HEADER } 0103`
17+ const DECREASE_KEY = `${ HUMIDIFIER_COMMAND_HEADER } 0104`
18+ const SET_AUTO_MODE_KEY = `${ HUMIDIFIER_COMMAND_HEADER } 0105`
19+ const SET_MANUAL_MODE_KEY = `${ HUMIDIFIER_COMMAND_HEADER } 0106`
20+
1321/**
1422 * Class representing a WoHumi device.
1523 * @see https://github.com/OpenWonderLabs/SwitchBotAPI-BLE/tree/latest/devicetypes
1624 */
1725export class WoHumi extends SwitchbotDevice {
26+ constructor ( peripheral : NobleTypes [ 'peripheral' ] , noble : NobleTypes [ 'noble' ] ) {
27+ super ( peripheral , noble )
28+ }
29+
1830 /**
1931 * Parses the service data for WoHumi.
2032 * @param {Buffer } serviceData - The service data buffer.
@@ -51,17 +63,12 @@ export class WoHumi extends SwitchbotDevice {
5163 return data
5264 }
5365
54- constructor ( peripheral : NobleTypes [ 'peripheral' ] , noble : NobleTypes [ 'noble' ] ) {
55- super ( peripheral , noble )
56- }
57-
5866 /**
5967 * Sends a command to the humidifier.
60- * @param {number[] } bytes - The command bytes .
68+ * @param {Buffer } reqBuf - The command buffer .
6169 * @returns {Promise<void> }
6270 */
63- public async operateHumi ( bytes : number [ ] ) : Promise < void > {
64- const reqBuf = Buffer . from ( bytes )
71+ protected async operateHumi ( reqBuf : Buffer ) : Promise < void > {
6572 const resBuf = await this . command ( reqBuf )
6673 const code = resBuf . readUInt8 ( 0 )
6774
@@ -71,42 +78,63 @@ export class WoHumi extends SwitchbotDevice {
7178 }
7279
7380 /**
74- * Presses the humidifier button .
81+ * Turns on the humidifier.
7582 * @returns {Promise<void> }
7683 */
77- async press ( ) : Promise < void > {
78- await this . operateHumi ( [ 0x57 , 0x01 , 0x00 ] )
84+ public async turnOn ( ) : Promise < void > {
85+ await this . operateHumi ( Buffer . from ( TURN_ON_KEY , 'hex' ) )
7986 }
8087
8188 /**
82- * Turns on the humidifier.
89+ * Turns off the humidifier.
8390 * @returns {Promise<void> }
8491 */
85- async turnOn ( ) : Promise < void > {
86- await this . operateHumi ( [ 0x57 , 0x01 , 0x01 ] )
92+ public async turnOff ( ) : Promise < void > {
93+ await this . operateHumi ( Buffer . from ( TURN_OFF_KEY , 'hex' ) )
8794 }
8895
8996 /**
90- * Turns off the humidifier.
97+ * Increases the humidifier setting .
9198 * @returns {Promise<void> }
9299 */
93- async turnOff ( ) : Promise < void > {
94- await this . operateHumi ( [ 0x57 , 0x01 , 0x02 ] )
100+ public async increase ( ) : Promise < void > {
101+ await this . operateHumi ( Buffer . from ( INCREASE_KEY , 'hex' ) )
95102 }
96103
97104 /**
98105 * Decreases the humidifier setting.
99106 * @returns {Promise<void> }
100107 */
101- async down ( ) : Promise < void > {
102- await this . operateHumi ( [ 0x57 , 0x01 , 0x03 ] )
108+ public async decrease ( ) : Promise < void > {
109+ await this . operateHumi ( Buffer . from ( DECREASE_KEY , 'hex' ) )
103110 }
104111
105112 /**
106- * Increases the humidifier setting.
113+ * Sets the humidifier to auto mode.
114+ * @returns {Promise<void> }
115+ */
116+ public async setAutoMode ( ) : Promise < void > {
117+ await this . operateHumi ( Buffer . from ( SET_AUTO_MODE_KEY , 'hex' ) )
118+ }
119+
120+ /**
121+ * Sets the humidifier to manual mode.
107122 * @returns {Promise<void> }
108123 */
109- async up ( ) : Promise < void > {
110- await this . operateHumi ( [ 0x57 , 0x01 , 0x04 ] )
124+ public async setManualMode ( ) : Promise < void > {
125+ await this . operateHumi ( Buffer . from ( SET_MANUAL_MODE_KEY , 'hex' ) )
126+ }
127+
128+ /**
129+ * Sets the humidifier level.
130+ * @param {number } level - The level to set (0-100).
131+ * @returns {Promise<void> }
132+ */
133+ public async percentage ( level : number ) : Promise < void > {
134+ if ( level < 0 || level > 100 ) {
135+ throw new Error ( 'Level must be between 0 and 100' )
136+ }
137+ const levelKey = `${ HUMIDIFIER_COMMAND_HEADER } 0107${ level . toString ( 16 ) . padStart ( 2 , '0' ) } `
138+ await this . operateHumi ( Buffer . from ( levelKey , 'hex' ) )
111139 }
112140}
0 commit comments