homebridge-syntex-tuya
Version:
A webhook plugin for Tuya devices
82 lines (64 loc) • 2.02 kB
JavaScript
const { LightBulbService } = require('homebridge-syntex-dynamic-platform');
module.exports = class SynTexLightBulbService extends LightBulbService
{
constructor(homebridgeAccessory, deviceConfig, serviceConfig, manager)
{
super(homebridgeAccessory, deviceConfig, serviceConfig, manager);
this.DeviceManager = manager.DeviceManager;
this.changeHandler = (state) => {
if(state.value != null)
{
this.setState(state.value,
() => this.service.getCharacteristic(this.Characteristic.On).updateValue(state.value));
}
};
}
getState(callback)
{
super.getState((value) => {
if(super.hasState('value'))
{
this.value = value;
this.logger.log('read', this.id, this.letters, '%read_state[0]% [' + this.name + '] %read_state[1]% [' + value + '] ( ' + this.id + ' )');
callback(null, value);
}
else
{
this.DeviceManager.getState(this).then((state) => {
if(state.value != null)
{
this.value = state.value;
super.setState(state.value,
() => this.logger.log('read', this.id, this.letters, '%read_state[0]% [' + this.name + '] %read_state[1]% [' + state.value + '] ( ' + this.id + ' )'));
}
callback(null, this.value);
});
}
});
}
setState(value, callback)
{
this.DeviceManager.setState(this, value).then((success) => {
if(success)
{
this.value = value;
super.setState(value, () => callback(), true);
this.AutomationSystem.LogikEngine.runAutomation(this, { value });
}
else
{
callback(new Error('Offline'));
}
});
}
updateState(state)
{
if(state.value != null && !isNaN(state.value) && (!super.hasState('value') || this.value != state.value))
{
this.value = state.value;
super.setState(state.value,
() => this.service.getCharacteristic(this.Characteristic.On).updateValue(state.value), true);
}
this.AutomationSystem.LogikEngine.runAutomation(this, state);
}
}