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

Adding Fan controllers #148

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Adding Fan controllers #148

wants to merge 2 commits into from

Conversation

murch1
Copy link

@murch1 murch1 commented Jan 11, 2020

I have recently bought a Brilliant Wifi Ceiling Fan Controller.
The controller does both fan (power and 3 speeds) and light control (power and brightness).
I initially did the stand alone fan (SimpleFanAccessory), because I'm not using the light control (I have the lights wired straight to the power, as I already have Hue lights installed). The fan & light accessory (SimpleFanLightAccessory) was just to see if I could get it to work - which it seems to.

You will notice in the schema there are a few more functions, including:
* Mode
* Sleep timer
I have left these out, as there is no characteristic in the the Fan service to allow for them.

Schema:

{
    name: 'Ceiling Fan',
    id: 'xxx',
    key: 'yyy',
    def: {
      schema: '[{"mode":"rw","code":"switch","name":"\\u5f00\\u5173","property":{"type":"bool"},"id":1,"type":"obj","desc":""},{"mode":"rw","code":"mode","name":"\\u5de5\\u4f5c\\u6a21\\u5f0f","property":{"range":["Nature","Normal","Sleep","WakeUp"],"type":"enum"},"id":2,"type":"obj","desc":""},{"mode":"rw","code":"fan_speed","name":"\\u98ce\\u901f","property":{"range":["1","2","3"],"type":"enum"},"id":3,"type":"obj","desc":"\\u53ef\\u6839\\u636e\\u5b9e\\u9645\\u6863\\u4f4d\\u9700\\u6c42\\u6269\\u5c55"},{"mode":"rw","code":"countdown","name":"\\u5012\\u8ba1\\u65f6","property":{"range":["0","1","2","3","4","5","6"],"type":"enum"},"id":6,"type":"obj","desc":"0\\uff1a\\u53d6\\u6d88\\n1\\uff1a10\\u5206\\u949f\\n2\\uff1a30\\u5206\\u949f\\n3\\uff1a1\\u5c0f\\u65f6\\n4\\uff1a2\\u5c0f\\u65f6\\n5\\uff1a3\\u5c0f\\u65f6\\n6\\uff1a4\\u5c0f\\u65f6"},{"mode":"ro","code":"countdown_left","name":"\\u5012\\u8ba1\\u65f6\\u5269\\u4f59\\u65f6\\u95f4","property":{"unit":"\\u5206\\u949f","min":0,"max":360,"scale":0,"step":1,"type":"value"},"id":7,"type":"obj","desc":""},{"mode":"rw","code":"light","name":"\\u706f\\u5149","property":{"type":"bool"},"id":9,"type":"obj","desc":""},{"mode":"rw","code":"bright_value","name":"\\u706f\\u5149\\u4eae\\u5ea6","property":{"unit":"","min":10,"max":100,"scale":0,"step":1,"type":"value"},"id":10,"type":"obj","desc":""}]',
      extras: '[{"id":1,"inputStyle":"","inputType":"plain"},{"id":2,"inputStyle":"","inputType":"plain"},{"id":3,"inputStyle":"","inputType":"plain"},{"id":6,"inputStyle":"","inputType":" "},{"id":9,"inputStyle":"","inputType":"plain"},{"id":10,"inputStyle":"","inputType":" "}]',
    }

Sample config.json for Fanlight:

                           "name": "Ceiling Fan Light",
                           "type": "Fanlight",
                           "manufacturer": "Brilliant",
                           "model": "Wifi Ceiling Fan Controller",
                           "id": "xxx",
    		          "key": "yyy",
    		          "dpActive": 1,
    			  "dpRotationSpeed": 3,
    			  "dpLightOn": 9,
    			  "dpBrightness": 10

Sample config.json for Fan:

                           "name": "Ceiling Fan",
                           "type": "Fan",
                           "manufacturer": "Brilliant",
                           "model": "Wifi Ceiling Fan Controller",
                           "id": "xxx",
    		          "key": "yyy",
    		          "dpActive": 1,
    			  "dpRotationSpeed": 3

Added
    fan: SimpleFanAccessory (for fan only control)
    fanlight: SimpleFanLightAccessory (for fan controllers that also control a light)
Adjusted the code from SimpleHeaterAccessory.js
Tested on Brilliant Wifi Ceiling Fan Controller.
I initially only did the fan first, but then added in the light later.
For me, controller can control the light, but I have not wired that functionality it in, as I already have Hue lighting installed and didn't want to stuff that up.
@murch1
Copy link
Author

murch1 commented Jan 18, 2020

I forgot to add some other code changes I made to get this to work how I like it.
By default, HomeKit displays fan rotation speed by percentage. The fans in my house are only three speed, so a percentage is not very elegant. I have found the relevant files and created my own unit of measure - simply called "speed".
The code changes are not located in homebridge-tuya-lan, hence not included in this pull request.
Locate the files in: <location of your npm folder (e.g. ~/.npm-global)> /lib/node_modules/homebridge/node_modules/hap-nodejs/lib/Characteristics.js (around line 84)

// Known HomeKit unit types
Characteristic.Units = {
  // HomeKit only defines Celsius, for Fahrenheit, it requires iOS app to do the conversion.
  CELSIUS: 'celsius',
  PERCENTAGE: 'percentage',
  ARC_DEGREE: 'arcdegrees',
  LUX: 'lux',
  SECONDS: 'seconds',
  SPEED: 'speed'
}

Add an entry called SPEED.

/lib/node_modules/homebridge/node_modules/hap-nodejs/lib/gen/HomeKitTypes.js (around line 1696)

/**
 * Characteristic "Rotation Speed"
 */

Characteristic.RotationSpeed = function() {
  Characteristic.call(this, 'Rotation Speed', '00000029-0000-1000-8000-0026BB765291');
  this.setProps({
    format: Characteristic.Formats.INT,
    unit: Characteristic.Units.SPEED,
    maxValue: 3,
    minValue: 0,
    minStep: 1,
    perms: [Characteristic.Perms.READ, Characteristic.Perms.WRITE, Characteristic.Perms.NOTIFY]
  });
  this.value = this.getDefaultValue();
};

inherits(Characteristic.RotationSpeed, Characteristic);

Characteristic.RotationSpeed.UUID = '00000029-0000-1000-8000-0026BB765291';

Change unit to SPEED.
Update the maxValue to maximum speed of your fan.

Be aware that any updates you do to homebridge will reset this back to default and you'll have to change it back again.

Fan

@stibbzy
Copy link

stibbzy commented Jul 30, 2020

Hi @murch1

Just wanted to say thanks very much for making this update. I incorporated the two new files and one changed file into my setup, and my Brilliant fan controller is working great in HomeKit!

I haven’t changed the Characteristics.js just yet, but as a fan percentage control, it’s working really well.

@stibbzy
Copy link

stibbzy commented Aug 11, 2020

Hi again @murch1

Still using your update with my Brilliant controllers and its working perfectly. I’m wondering if there is any chance you could update it or give me some direction on how to get it working for these new Deta fan controllers: https://www.bunnings.com.au/deta-grid-connect-smart-fan-speed-controller-with-touch-light-switch_p0098815

I have my ID and Key, but using the updated files as-is gives a undefined response in the Homebridge logs. I’m assuming the schema must be different, but have no idea how to get the relevant schema or update your files to support it. I’d love to get these switches working, if you had any advice or could look into it, that’d be fantastic.

Mark.

@murch1
Copy link
Author

murch1 commented Aug 18, 2020

Hi @stibbzy ,
I retrieved the Brilliant fan schema back in the good ol' days when tuya-lan-find --schema worked... ahhhh the good ol' days.
I've tried to have a look at see if there are any easy ways to list the schema reference numbers for each function, but can't find anything. The tuya IoT developer portal (Instructions here) shows all my devices and lists the available functions on some of them, but doesn't tell you which function number it is.
That being said, I have some ideas...

  1. The controller doesn't seem to mention light brightness, so maybe try removing (or commenting out to start with) the brightness code from the SimpleFanLightAccessory.js file - basically from line 142 to line 162. With the code not trying to call on dpBrightness (10), it may not throw an error.
  2. With the fan speed MODE now seemingly absent, I wonder if the reference numbers for fan speed and light state have changed. Maybe play around with the dpRotationSpeed and dpLightOn settings in the homebridge config.json and setting them to something like:
    				"dpActive": 1,
    				"dpRotationSpeed": 2,
    				"dpLightOn": 3,
    				"dpUseLight": true

I hope that helps. Good luck.

@stibbzy
Copy link

stibbzy commented Aug 18, 2020

Hi @murch1

I've actually made some progress with this. I now have it semi-working, and managed to get the new schema, which looks very similar to the Brilliant one:
[
{
name: 'Fan Switch 6914HA 2',
id: '08145656f4cfa25d1838',
key: 'xxx',
def: {
schema: '[{"mode":"rw","code":"switch","name":"\u98ce\u6247\u5f00\u5173","property":{"type":"bool"},"iconname":"icon-power","id":1,"type":"obj","desc":""},{"mode":"rw","code":"fan_speed","name":"\u98ce\u901f\u6863\u4f4d","property":{"range":["1","2","3"],"type":"enum"},"iconname":"icon-a_fan_low","id":3,"type":"obj","desc":"1\u30012\u30013\u5206\u522b\u4ee3\u8868\u4f4e\u3001\u4e2d\u3001\u9ad8\u98ce\u901f\u6863"},{"mode":"rw","code":"light","name":"\u706f\u5f00\u5173","property":{"type":"bool"},"iconname":"icon-tcl_function_light","id":9,"type":"obj","desc":""},{"mode":"rw","code":"master","name":"\u603b\u5f00\u5173","property":{"type":"bool"},"id":101,"type":"obj","desc":"\u706f\u548c\u98ce\u6247\u7684\u603b\u5f00\u5173"},{"mode":"rw","code":"timer_fan","name":"\u98ce\u6247\u5012\u8ba1\u65f6","property":{"range":["1","2","3","4","5"],"type":"enum"},"id":102,"type":"obj","desc":"1 \u8868\u793a\u65e0\u6548\uff0c2\u30013\u30014\u30015 \u5206\u522b\u8868\u793a\u5012\u8ba1\u65f6 1h\u30012h\u30014h\u30018h"},{"mode":"rw","code":"timer_light","name":"\u706f\u5012\u8ba1\u65f6","property":{"range":["1","2","3","4","5"],"type":"enum"},"id":103,"type":"obj","desc":"1 \u8868\u793a\u65e0\u6548\uff0c2\u30013\u30014\u30015 \u5206\u522b\u8868\u793a\u5012\u8ba1\u65f6 1h\u30012h\u30014h\u30018h"}]',
extras: '[{"complexType":"","id":1,"inputStyle":"","inputType":""},{"complexType":"","id":3,"inputStyle":"","inputType":""},{"complexType":"","id":9,"inputStyle":"","inputType":""},{"complexType":"","id":101,"inputStyle":"","inputType":""},{"complexType":"","id":102,"inputStyle":"","inputType":""},{"complexType":"","id":103,"inputStyle":"","inputType":""}]',
},
},
]

The issue is, I have the issue described here: iRayanKhan/homebridge-tuya#118

That is, if you try to set the fan speed, it always runs at 100%. I've figured out it is because the script, when setting characteristic.On, sets dp.Active to 1, after setting dp.RotationSpeed to whatever value is requested. That was no issue with the Brilliant fan controller, but the Deta controller operates this way: Whenever the Fan On button is presses, speed 3 (100%) is selected. Then you press the speed button to select the speed. But it will always select speed 3 when the Fan On button is pressed.

So, when the code is running, the speed is selected (say 2), but then dp.Active is set true, which sets the Fan speed back to 3.

I can actually get the speed slider to work if I comment out the .on('set', this.setActive.bind(this)); line 37. But then you can't use the Home app tile to turn the fan on and off. I've tried doing things like this, but it always wants to set dp.Active true after I select the speed:

setActive(value, callback) {
const {Characteristic} = this.hap;
if (this.dpRotationSpeed !== 0) {
console.log("Did this work?");
} else {

    	return this.setState(this.dpActive, value, callback);
    	
    }

    callback();
}

I've never really coded anything before, so I have no real idea about what I am doing. It would be great to get this working though!

@qthatswho
Copy link

qthatswho commented Aug 26, 2020

The good news is, the settings from this thread seem to mostly work with my newly purchased TREATLIFE DS03 Fan/Light combo switch. The bad news is, I'm seeing some similar issues with fan speed control, and light dimming as mentioned here and elsewhere.

Light: Turning the light on/off in Home app works ok, as long as you have not manipulated the brightness level in Home app (so, using just the main button in the Room). If you adjust the brightness, no matter the value, the switch seems to always move to the minimum value. Wonder if this has something to do with the value range itself (maybe setting 1-10 for brightness rather than 1-100 or something like that). Setting the brightness to maximum never (visibly) changes the intensity level, and it must be corrected using the hardware switch).

Fan: Adjusting the speed mostly works, but, there are 4 speeds on the switch, and when you adjust the speed % using the app, it will only ever go to a maximum level of 3 on the switch (it actually shows that it goes to 4 briefly with the indicator lights on the switch, but settles on 3 in the end). Only way to get the fan back to full speed is to set it back to speed 4 using the hardware switch.

I'm an intermediate user of Homebridge, but very new to these Tuya devices. Been doing lots of spelunking on google, but still feeling very novice and uninformed as to the specifics of manipulating these devices via Homebridge config.json.

config.json:

"platform": "TuyaLan", "devices": [ { "name": "Front Room Fan", "type": "FanLight", "manufacturer": "TREATLIFE", "model": "Smart Dimmer Switch - DS03", "id": "abcd1234", "key": "...", "dpActive": 1, "dpRotationSpeed": 3, "dpLightOn": 9, "dpBrightness": 10 }

@tbelcher
Copy link

tbelcher commented Sep 7, 2020

For what its worth, this has been my experience with the TreatLife DS-03 ceiling fans. I've been able to install them and get them working with the device id's and keys. They have 4 speeds (if controlled by the Tuya app or the wall switch), but HomeKit only shows 3 speeds (33%, 66% and 100%). There is no way of accessing the highest speed through the Home app.

I've updated the following files based on @murch1 instructions.
Changed units to 'speed' in /usr/local/lib/node_modules/homebridge/node_modules/hap-nodejs/dist/lib/gen/Characteristic.js and this is now reflected in the Home app interface.

I couldn't find the HomeKitTypes.js file anywhere but this file seems to have the same contents: /usr/local/lib/node_modules/homebridge/node_modules/hap-nodejs/dist/lib/HomeKit.js
I changed minValue to 1 and maxValue to 4 as this matched what the Tuya website showed for this fan:

fan_speed | Enum | {"range":["1","2","3","4"]}

However, I can still only access 3 speeds through the Home app.

Any ideas?

Thanks.

@mvgilpatrick
Copy link

Thanks for doing your work on this! I'm wondering how easy it would be change the fan light to display as a switch and not a dimmer?

@jayband
Copy link

jayband commented Dec 30, 2020

Hi @murch1

I've actually made some progress with this. I now have it semi-working, and managed to get the new schema, which looks very similar to the Brilliant one:
[
{
name: 'Fan Switch 6914HA 2',
id: '08145656f4cfa25d1838',
key: 'xxx',
def: {
schema: '[{"mode":"rw","code":"switch","name":"\u98ce\u6247\u5f00\u5173","property":{"type":"bool"},"iconname":"icon-power","id":1,"type":"obj","desc":""},{"mode":"rw","code":"fan_speed","name":"\u98ce\u901f\u6863\u4f4d","property":{"range":["1","2","3"],"type":"enum"},"iconname":"icon-a_fan_low","id":3,"type":"obj","desc":"1\u30012\u30013\u5206\u522b\u4ee3\u8868\u4f4e\u3001\u4e2d\u3001\u9ad8\u98ce\u901f\u6863"},{"mode":"rw","code":"light","name":"\u706f\u5f00\u5173","property":{"type":"bool"},"iconname":"icon-tcl_function_light","id":9,"type":"obj","desc":""},{"mode":"rw","code":"master","name":"\u603b\u5f00\u5173","property":{"type":"bool"},"id":101,"type":"obj","desc":"\u706f\u548c\u98ce\u6247\u7684\u603b\u5f00\u5173"},{"mode":"rw","code":"timer_fan","name":"\u98ce\u6247\u5012\u8ba1\u65f6","property":{"range":["1","2","3","4","5"],"type":"enum"},"id":102,"type":"obj","desc":"1 \u8868\u793a\u65e0\u6548\uff0c2\u30013\u30014\u30015 \u5206\u522b\u8868\u793a\u5012\u8ba1\u65f6 1h\u30012h\u30014h\u30018h"},{"mode":"rw","code":"timer_light","name":"\u706f\u5012\u8ba1\u65f6","property":{"range":["1","2","3","4","5"],"type":"enum"},"id":103,"type":"obj","desc":"1 \u8868\u793a\u65e0\u6548\uff0c2\u30013\u30014\u30015 \u5206\u522b\u8868\u793a\u5012\u8ba1\u65f6 1h\u30012h\u30014h\u30018h"}]',
extras: '[{"complexType":"","id":1,"inputStyle":"","inputType":""},{"complexType":"","id":3,"inputStyle":"","inputType":""},{"complexType":"","id":9,"inputStyle":"","inputType":""},{"complexType":"","id":101,"inputStyle":"","inputType":""},{"complexType":"","id":102,"inputStyle":"","inputType":""},{"complexType":"","id":103,"inputStyle":"","inputType":""}]',
},
},
]

The issue is, I have the issue described here: iRayanKhan/homebridge-tuya#118

That is, if you try to set the fan speed, it always runs at 100%. I've figured out it is because the script, when setting characteristic.On, sets dp.Active to 1, after setting dp.RotationSpeed to whatever value is requested. That was no issue with the Brilliant fan controller, but the Deta controller operates this way: Whenever the Fan On button is presses, speed 3 (100%) is selected. Then you press the speed button to select the speed. But it will always select speed 3 when the Fan On button is pressed.

So, when the code is running, the speed is selected (say 2), but then dp.Active is set true, which sets the Fan speed back to 3.

I can actually get the speed slider to work if I comment out the .on('set', this.setActive.bind(this)); line 37. But then you can't use the Home app tile to turn the fan on and off. I've tried doing things like this, but it always wants to set dp.Active true after I select the speed:

setActive(value, callback) {
const {Characteristic} = this.hap;
if (this.dpRotationSpeed !== 0) {
console.log("Did this work?");
} else {

    	return this.setState(this.dpActive, value, callback);
    	
    }

    callback();
}

I've never really coded anything before, so I have no real idea about what I am doing. It would be great to get this working though!

How do you get the schema nowadays when tuya-lan-find --schema is not working, I was able to get ID/Keys from iot.tuya.com

@stibbzy
Copy link

stibbzy commented Dec 30, 2020

T uya-lan-find --schema seems to have worked fine for me, at least when I was working on this a few months ago.

In the end, I flashed the units with Tasmota and loaded them up into HomeKit via Home Assistant.

@AndrewJ1990
Copy link

Here is the schema for the deta fan controller

schema: '[{"mode":"rw","code":"switch","name":"\u98ce\u6247\u5f00\u5173","property":{"type":"bool"},"iconname":"icon-power","id":1,"type":"obj","desc":""},{"mode":"rw","code":"fan_speed","name":"\u98ce\u901f\u6863\u4f4d","property":{"range":["1","2","3"],"type":"enum"},"iconname":"icon-a_fan_low","id":3,"type":"obj","desc":"1\u30012\u30013\u5206\u522b\u4ee3\u8868\u4f4e\u3001\u4e2d\u3001\u9ad8\u98ce\u901f\u6863"},{"mode":"rw","code":"light","name":"\u706f\u5f00\u5173","property":{"type":"bool"},"iconname":"icon-tcl_function_light","id":9,"type":"obj","desc":""},{"mode":"rw","code":"master","name":"\u603b\u5f00\u5173","property":{"type":"bool"},"id":101,"type":"obj","desc":"\u706f\u548c\u98ce\u6247\u7684\u603b\u5f00\u5173"},{"mode":"rw","code":"timer_fan","name":"\u98ce\u6247\u5012\u8ba1\u65f6","property":{"range":["1","2","3","4","5"],"type":"enum"},"id":102,"type":"obj","desc":"1 \u8868\u793a\u65e0\u6548\uff0c2\u30013\u30014\u30015 \u5206\u522b\u8868\u793a\u5012\u8ba1\u65f6 1h\u30012h\u30014h\u30018h"},{"mode":"rw","code":"timer_light","name":"\u706f\u5012\u8ba1\u65f6","property":{"range":["1","2","3","4","5"],"type":"enum"},"id":103,"type":"obj","desc":"1 \u8868\u793a\u65e0\u6548\uff0c2\u30013\u30014\u30015 \u5206\u522b\u8868\u793a\u5012\u8ba1\u65f6 1h\u30012h\u30014h\u30018h"}]',
extras: '[{"complexType":"","id":1,"inputStyle":"","inputType":""},{"complexType":"","id":3,"inputStyle":"","inputType":""},{"complexType":"","id":9,"inputStyle":"","inputType":""},{"complexType":"","id":101,"inputStyle":"","inputType":""},{"complexType":"","id":102,"inputStyle":"","inputType":""},{"complexType":"","id":103,"inputStyle":"","inputType":""}]',
},

@jayband
Copy link

jayband commented Dec 30, 2020

I am attempting to get DS03 dimming function working. With the tuya-lan branch + ID/Key from iot, I can get everything shows up in Homekit, only issue is both fan/light in binary, no dimmer/speed capability. But at least I am super happy that light and fan showing up. Previous tuya-web on the DS03 only has fan capability only...

@burraga
Copy link

burraga commented Jan 3, 2021

Here is the schema for the deta fan controller

schema: '[{"mode":"rw","code":"switch","name":"\u98ce\u6247\u5f00\u5173","property":{"type":"bool"},"iconname":"icon-power","id":1,"type":"obj","desc":""},{"mode":"rw","code":"fan_speed","name":"\u98ce\u901f\u6863\u4f4d","property":{"range":["1","2","3"],"type":"enum"},"iconname":"icon-a_fan_low","id":3,"type":"obj","desc":"1\u30012\u30013\u5206\u522b\u4ee3\u8868\u4f4e\u3001\u4e2d\u3001\u9ad8\u98ce\u901f\u6863"},{"mode":"rw","code":"light","name":"\u706f\u5f00\u5173","property":{"type":"bool"},"iconname":"icon-tcl_function_light","id":9,"type":"obj","desc":""},{"mode":"rw","code":"master","name":"\u603b\u5f00\u5173","property":{"type":"bool"},"id":101,"type":"obj","desc":"\u706f\u548c\u98ce\u6247\u7684\u603b\u5f00\u5173"},{"mode":"rw","code":"timer_fan","name":"\u98ce\u6247\u5012\u8ba1\u65f6","property":{"range":["1","2","3","4","5"],"type":"enum"},"id":102,"type":"obj","desc":"1 \u8868\u793a\u65e0\u6548\uff0c2\u30013\u30014\u30015 \u5206\u522b\u8868\u793a\u5012\u8ba1\u65f6 1h\u30012h\u30014h\u30018h"},{"mode":"rw","code":"timer_light","name":"\u706f\u5012\u8ba1\u65f6","property":{"range":["1","2","3","4","5"],"type":"enum"},"id":103,"type":"obj","desc":"1 \u8868\u793a\u65e0\u6548\uff0c2\u30013\u30014\u30015 \u5206\u522b\u8868\u793a\u5012\u8ba1\u65f6 1h\u30012h\u30014h\u30018h"}]',
extras: '[{"complexType":"","id":1,"inputStyle":"","inputType":""},{"complexType":"","id":3,"inputStyle":"","inputType":""},{"complexType":"","id":9,"inputStyle":"","inputType":""},{"complexType":"","id":101,"inputStyle":"","inputType":""},{"complexType":"","id":102,"inputStyle":"","inputType":""},{"complexType":"","id":103,"inputStyle":"","inputType":""}]',
},

I'd like to try this schema. I new to using Homebridge and I'm actually using a Hoobs device. How to I add this schema so that my Deta Fan Switch will work correctly in HomeKit?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants