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

Feature Request: Color Bulb Support #151

Open
donavanbecker opened this issue Sep 21, 2022 · 2 comments
Open

Feature Request: Color Bulb Support #151

donavanbecker opened this issue Sep 21, 2022 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed pinned

Comments

@donavanbecker
Copy link
Collaborator

Problem

Support for Color Bulb

Solution

Add Support for Color Bulb

Alternatives

Use OpenAPI for now

Additional context

No response

@donavanbecker donavanbecker added the enhancement New feature or request label Sep 21, 2022
@donavanbecker donavanbecker self-assigned this Sep 21, 2022
@donavanbecker donavanbecker pinned this issue Sep 21, 2022
@donavanbecker
Copy link
Collaborator Author

@cocoabox anyway you can help with this?

@donavanbecker
Copy link
Collaborator Author

@bdraco

An insight you can give on Color Bulbs to make them better for this module?

Advertisement:

_parseServiceDataForWoBulb(buf, onlog) {
if (buf.length !== 13) {
if (onlog && typeof onlog === "function") {
onlog(
`[_parseServiceDataForWoBulb] Buffer length ${buf.length} !== 13!`
);
}
return null;
}
const byte9 = buf.readUInt8(9); // byte9: color bulb state; 0x00=off, 0x80=on & lightLevel: 1~100%
//const byte10 = buf.readUInt8(10); // byte10: bit0: 0=no delay,1=delay, bit1:0=no timer, 1=timer; bit2:0=no sync time, 1=sync'ed time
//const byte11 = buf.readUInt8(11); // byte11: wifi rssi
//const byte12 = buf.readUInt8(12); // byte12: bit7: overload?
//const byte13 = buf.readUInt8(13); // byte12[bit0~6] + byte13: current power value
const state = byte9 === 0x01 ? "off" : byte9 === 0x80 ? "on" : null;
const lightLevel = byte9 & 0b00000011;
//const delay = !!(byte10 & 0b00000001);
//const networkStatus = !!(byte10 & 0b00000001);
//const statePreset = !!(byte10 & 0b00000010);
//const lightState = !!(byte10 & 0b00000100);
//const wifiRssi = byte11;
//const dynamicRate = !!(byte12 & 0b10000000);
//const loopIndex = (((byte12 & 0b01111111) << 8) + byte13) / 10; // in watt
const data = {
model: "u",
modelName: "WoBulb",
state: state,
};
/* const data = {
model: "u",
modelName: "WoBulb",
state: state,
lightLevel: lightLevel,
delay: delay,
networkStatus: networkStatus,
statePreset: statePreset,
lightState: lightState,
wifiRssi: wifiRssi,
dynamicRate: dynamicRate,
loopIndex: loopIndex,
};*/
return data;
}

Push Changes:

class SwitchbotDeviceWoBulb extends SwitchbotDevice {
/**
* @returns {Promise<boolean>} resolves with a boolean that tells whether the plug in ON(true) or OFF(false)
*/
readState() {
return this._operateBot([0x57, 0x0f, 0x48, 0x01]);
}
/**
* @private
*/
_setState(reqByteArray) {
const base = [0x57, 0x0f, 0x47, 0x01];
return this._operateBot([].concat(base, reqByteArray));
}
/**
* @returns {Promise<boolean>} resolves with a boolean that tells whether the plug in ON(true) or OFF(false)
*/
turnOn() {
return this._setState([0x01, 0x01]);
}
/**
* @returns {Promise<boolean>} resolves with a boolean that tells whether the plug in ON(true) or OFF(false)
*/
turnOff() {
return this._setState([0x01, 0x02]);
}
/**
* @returns {Promise<boolean>} resolves with a boolean that tells whether the plug in ON(true) or OFF(false)
*/
toggle() {
return this._setState([0x02, 0x03]);
}
/**
* @private
*/
_operateBot(bytes) {
const req_buf = Buffer.from(bytes);
return new Promise((resolve, reject) => {
this._command(req_buf)
.then((res_bytes) => {
const res_buf = Buffer.from(res_bytes);
if (res_buf.length === 2) {
let code = res_buf.readUInt8(1);
if (code === 0x00 || code === 0x80) {
const is_on = code === 0x80;
resolve(is_on);
} else {
reject(
new Error(
"The device returned an error: 0x" + res_buf.toString("hex")
)
);
}
} else {
reject(
new Error(
"Expecting a 2-byte response, got instead: 0x" +
res_buf.toString("hex")
)
);
}
})
.catch((error) => {
reject(error);
});
});
}
}

This was referenced Oct 27, 2022
@donavanbecker donavanbecker added the help wanted Extra attention is needed label Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed pinned
Projects
None yet
Development

No branches or pull requests

8 participants