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

Buttons & lights & relays providers refactoring #2414

Merged
merged 80 commits into from
Jan 14, 2021

Conversation

mcspr
Copy link
Collaborator

@mcspr mcspr commented Jan 3, 2021

  • gpio module now tracks the known providers (right now, hardware and mcp expander)
  • refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions
  • refactored button module to use gpio provider instead of referencing types itself
  • removed dual & stm code from buttons, migrate both to relay module
  • added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did)
  • relays runtime configuration keys
  • relay command now shows configured relays and current & target statuses
  • refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead
  • remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT
  • allow to bind rf codes to real relays
  • drop tuya-specific lights provider, remove tuya code from relays and lights modules
  • integrate tuya via relay listeners and providers, use lights custom provider
  • implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. total transition time / step time == number of steps, we need to figure out a correct time that serial comms could handle)
  • lights custom provider (global, not per-pin) and state listeners
  • remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT
  • lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT
  • refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing
  • transition time + step parameter for the lightUpdate
  • report mask parameter for the lightUpdate
  • minor fixes across the board

resolve #2222

- add light 'custom provider'
- autoconfigure stuff with the same methods
- drop broker support in favour of custom provider
- lowercase ns
- global dp filter (todo something better?)
- array can't be checked with `[1,2,3] in 1` since those check indices (...), need special method
- common func for schema
- autoscroll debug log only when already scrolled near the bottom
  (annoying with tuya, update now)
- update blobs
@davebuk
Copy link
Contributor

davebuk commented Jan 19, 2021

FYI, after more testing a transition setting of 2000/100ms hasn't always turned the light OFF after a cold boot. 2000/200ms seems to be OK. Its not as smooth but seems more reliable.

@mcspr
Copy link
Collaborator Author

mcspr commented Jan 19, 2021

Any logs?
One thing comes to mind - tuyaFilter is no longer a DPid, but a global boolean value. In case you still have it set to 2, it may accidentally read the remote ON state. Either setting to 1 or removing it (default is that already) should ignore the messages again.
Otherwise, I'd look at the Heartbeat timeout value. Maybe it needs slightly longer step (+50 or +100ms), see HeartbeatIncrement in the tuya.cpp

@davebuk
Copy link
Contributor

davebuk commented Jan 20, 2021

I have tuyaFilter 1 but also have tuyafilter 2 which I may have added with the lower case 'f' by accident? del tuyafilter comes up with an error. Would these two keys conflict with each other?

@mcspr
Copy link
Collaborator Author

mcspr commented Jan 20, 2021

Settings are case-sensitive, so the first key is selected.
ref. the second issue, are there really two keys when looking at eeprom.dump output?

@davebuk
Copy link
Contributor

davebuk commented Jan 20, 2021

For some reason, all webUI debug commands give an error saying "Command not found". I've tried a warm and cold reboot and it still doesn't work. I'll try re loading the .bin

@davebuk
Copy link
Contributor

davebuk commented Jan 20, 2021

OFF TOPIC - the version and commit version shown top left of the webUI are now merged e.g
was: 1.15.0-dev (2b2fb4f9) newer build 1.15.0-dev8e659c94 just looks less pretty :-)

I had to load a previous build, 1.15.0-dev (2b2fb4f) tuyafilter is there. I was able to delete it using del tuyafilter

0x0B70:  00 06 30 62 31 00 03 72 65 6C 61 79 42 6F 6F 74   0b1  relayBoot
0x0B80:  4D 61 73 6B 00 0D 31 00 01 64 62 67 4C 6F 67 42 Mask  1  dbgLogB
0x0B90:  75 66 00 09 31 00 01 74 75 79 61 43 68 61 6E 53 uf  1  tuyaChanS
0x0BA0:  74 61 74 65 00 0D 30 00 01 6D 69 72 65 64 73 00 tate  0  mireds 
0x0BB0:  06 32 35 35 00 03 62 72 69 67 68 74 6E 65 73 73  255  brightness
0x0BC0:  00 0A 32 00 01 74 75 79 61 43 68 61 6E 6E 65 6C   2  tuyaChannel
0x0BD0:  30 00 0C 30 00 01 74 75 79 61 53 77 69 74 63 68 0  0  tuyaSwitch
0x0BE0:  30 00 0B 54 55 59 41 5F 47 45 4E 45 52 49 43 5F 0  TUYA_GENERIC_
0x0BF0:  44 49 4D 4D 45 52 00 13 62 6F 61 72 64 4E 61 6D DIMMER  boardNam
0x0C00:  65 00 09 31 36 34 00 03 63 68 31 00 03 32 00 01 e  164  ch1  2  
0x0C10:  74 75 79 61 66 69 6C 74 65 72 00 0A 32 00 01 6C tuyafilter  2  l
0x0C20:  65 64 4D 6F 64 65 30 00 08 31 39 32 2E 31 36 38 edMode0  192.168
0x0C30:  2E 31 2E 31 00 0B 64 6E 73 30 00 04 31 32 38 00 .1.1  dns0  128 
0x0C40:  03 73 79 73 53 63 54 72 61 63 65 4D 61 78 00 0D  sysScTraceMax  
0x0C50:  31 00 01 6D 71 74 74 45 6E 61 62 6C 65 64 00 0B 1  mqttEnabled  
0x0C60:  6F 70 65 6E 68 61 62 69 61 6E 00 0A 6D 71 74 74 openhabian  mqtt
0x0C70:  55 73 65 72 00 08 30 00 01 6D 71 74 74 51 6F 53 User  0  mqttQoS
0x0C80:  00 07 30 00 01 75 73 65 52 47 42 00 06 30 00 01   0  useRGB  0  
0x0C90:  75 73 65 43 6F 6C 6F 72 00 08 30 00 01 72 65 6C useColor  0  rel
0x0CA0:  61 79 4C 61 73 74 53 63 68 30 00 0D 31 39 32 2E ayLastSch0 
0x0CB0:  31 36 38 2E 31 2E 32 32 32 00 0D 6D 71 74 74 53   mqttS
0x0CC0:  65 72 76 65 72 00 0A 44 69 6E 69 6E 67 20 4C 69 erver  Dining Li
0x0CD0:  67 68 74 00 0C 64 65 73 63 00 04 31 39 32 2E 31 ght  desc  
0x0CE0:  36 38 2E 31 2E 31 00 0B 67 77 30 00 03 31 39 32  gw0  
0x0CF0:  2E 31 36 38 2E 31 2E 34 33 00 0C 69 70 30 00 03   ip0  
0x0D00:  31 00 01 74 75 79 61 46 69 6C 74 65 72 00 0A 54 1  tuyaFilter

@mcspr
Copy link
Collaborator Author

mcspr commented Jan 20, 2021

What is the message log on boot though? No need to change transition time atm.

@davebuk
Copy link
Contributor

davebuk commented Jan 20, 2021

Built using standard TUYA environment settings with latest commit.

[005842] [WEBSOCKET] #1 connected, ip: 192.168. url: /ws
[011338] [TUYA] =>: 55aa00000000ff
[011362] [TUYA] <=: 55aa000000010101
[020258] [WEBSOCKET] Requested action: dbgcmd
[020260] [DEBUG] Buffer size: 4081 / 4096 bytes
[000118] [TELNET] Listening on port 23
[000120] [WEBSERVER] Webserver running on port 80
[000125] [LIGHT] Provider: CUSTOM
[000148] [RELAY] Number of relays: 1, boot mask: 0b1
[000149] [RELAY] #0 set to OFF
[000175] [BUTTON] Number of buttons: 1
[000181] [LED] Number of leds: 1
[000182] [MQTT] AsyncMqttClient, SSL DISABLED, Autoconnect ENABLED, Buffer size 1024 bytes
[000182] [MQTT] Client DISABLED, DISCONNECTED
[000202] [NTP] Startup delay: 3s, Update delay: 2760s
[000215] [THINGSPEAK] Async ENABLED, SSL DISABLED
[000309] [TUYA] =>: 55aa00000000ff
[000329] [WIFI] Scanning
[000515] [TUYA] =>: 55aa00000000ff
[000918] [TUYA] =>: 55aa00000000ff
[000950] [TUYA] <=: 55aa0007000501010001010f
[000961] [TUYA] <=: 55aa0007000802020004000000ff15
[001150] [RELAY] Relay mask: 0b0
[001526] [TUYA] =>: 55aa00000000ff
[001548] [TUYA] <=: 55aa000000010000
[001548] [TUYA] Attempting to configure the board ...
[001562] [LIGHT] Number of channels: 1
[001583] [TUYA] =>: 55aa0006000501010001000d
[001615] [TUYA] <=: 55aa0007000501010001000e
[001626] [TUYA] <=: 55aa0007000501010001000e
[001637] [TUYA] <=: 55aa0007000802020004000000ff15
[001659] [TUYA] <=: 55aa0007000501010001000e
[001670] [TUYA] <=: 55aa00070008020200040000001026
[002334] [TUYA] =>: 55aa00000000ff
[002355] [TUYA] <=: 55aa000000010101
[003515] [NTP] Updating `ntpServer` setting from DHCP: (IP unset)
[003524] [WIFI] Captive portal disabled
[003524] [WIFI] ------------------------------------- MODE STA
[003524] [WIFI] SSID 
[003525] [WIFI] IP   
[003525] [WIFI] MAC   
[003525] [WIFI] GW    192.168.1.1
[003526] [WIFI] DNS   192.168.1.1
[003526] [WIFI] MASK  255.255.255.0
[003526] [WIFI] HOST  http://ESPURNA-A6CCBD.local
[003526] [WIFI] BSSID 
[003527] [WIFI] CH    1
[003527] [WIFI] RSSI  -64
[003527] [WIFI] ----------------------------------------------
[003529] [MDNS] OK
[004530] [MQTT] MQTT brokers found: 0
[005001] [MQTT] Connecting to broker at 
[005001] [MQTT] Client ID: TUYA-001
[005002] [MQTT] QoS: 0
[005002] [MQTT] Retain flag: 0
[005002] [MQTT] Keepalive time: 300s
[005002] [MQTT] Will topic: TUYA001/status
[005002] [MQTT] Connecting as user
[005011] [MQTT] Connected!
[005012] [MQTT] Unsubscribing to # (PID 1)
[005013] [MQTT] Subscribing to TUYA001/brightness/set (PID 2)
[005014] [MQTT] Subscribing to TUYA001/transition/set (PID 3)
[005014] [MQTT] Subscribing to TUYA001/channel/+/set (PID 4)
[005014] [MQTT] Subscribing to TUYA001/relay/+/set (PID 5)
[005015] [MQTT] Subscribing to TUYA001/pulse/+/set (PID 6)
[005016] [MQTT] Subscribing to TUYA001/led/+/set (PID 7)
[005017] [MQTT] Subscribing to TUYA001/action/set (PID 8)
[005241] [MQTT] Subscribe ACK for PID 2
[005241] [MQTT] Subscribe ACK for PID 3
[005241] [MQTT] Subscribe ACK for PID 4
[005241] [MQTT] Subscribe ACK for PID 5
[005241] [MQTT] Subscribe ACK for PID 6
[005242] [MQTT] Subscribe ACK for PID 7
[005242] [MQTT] Subscribe ACK for PID 8
[020340] [TUYA] =>: 55aa00000000ff
[020364] [TUYA] <=: 55aa000000010101

Seems to be working again after deleting that key!

@davebuk
Copy link
Contributor

davebuk commented Jan 20, 2021

In case its important, the version I was running where I lost access to terminal commands was built using:

#elif defined(DB_TUYA_DIMMER)

    // Info
    #define MANUFACTURER        "TUYA"
    #define DEVICE              "GENERIC_DIMMER"

    //My config
    #define ALEXA_SUPPORT           0
    #define DOMOTICZ_SUPPORT        0
    #define HOMEASSISTANT_SUPPORT   0
    #define THINGSPEAK_SUPPORT      0
    #define NTP_TIMEZONE			TZ_Europe_London
	
    #define TUYA_SUPPORT        1
    #define LIGHT_PROVIDER      LIGHT_PROVIDER_CUSTOM
    #define BUTTON_MQTT_SEND_ALL_EVENTS     1

Also, the NTP server setting has been deleted in the eeprom by defining the NTP_TIMEZONE only. I think I'll just leave the option out in future and just fill in the timezone details in the webUI once the device is running.

@mcspr
Copy link
Collaborator Author

mcspr commented Jan 20, 2021

[003515] [NTP] Updating ntpServer setting from DHCP: (IP unset)

This looks like a separate issue with the DHCP packets coming from the router running the DHCP server or the way those are parsed, it should contain some valid data and not IP unset

@davebuk
Copy link
Contributor

davebuk commented Jan 21, 2021

1.15.0-dev.git660493a1

I'm still having issues with DEBUG commands in the webUI when building with the settings below. Commenting out the NTP stuff doesn't make any difference. CRASH, INFO etc brings up an error and debug.buffer gives: -ERROR: Command line buffer overflow. The standard environment works fine and other devices using the same 'My config' settings works fine, so it must be a TUYA issue. I'm trying to see the keys to check the NTP data.

#elif defined(DB_TUYA_DIMMER)

    // Info
    #define MANUFACTURER        "TUYA"
    #define DEVICE              "GENERIC_DIMMER"

    //My config
    #define ALEXA_SUPPORT           0
    #define DOMOTICZ_SUPPORT        0
    #define HOMEASSISTANT_SUPPORT   0
    #define THINGSPEAK_SUPPORT      0
    #define NTP_TIMEZONE			TZ_Europe_London
	
    #define TUYA_SUPPORT        1
    #define LIGHT_PROVIDER      LIGHT_PROVIDER_CUSTOM

@mcspr
Copy link
Collaborator Author

mcspr commented Jan 21, 2021

#define DEBUG_SERIAL_SUPPORT 0?

@davebuk
Copy link
Contributor

davebuk commented Jan 21, 2021

FIXED!
Why would that work where as the standard environment was fine!

@davebuk
Copy link
Contributor

davebuk commented Jan 21, 2021

Except I've lost LED from the webUI and the green LED has stopped coming ON. 😂

@davebuk
Copy link
Contributor

davebuk commented Jan 21, 2021

Just looked and 1.15.0-devacf29958, standard tuya build has also lost LED. Only ledMode0 is in the keys.

@mcspr
Copy link
Collaborator Author

mcspr commented Jan 21, 2021

Discovery never gets to run in the latest build when config is present. Unless manually set, both btnGPIO0 (0) and ledGPIO0 (14) won't show up
You can just add them build flags though?

@davebuk
Copy link
Contributor

davebuk commented Jan 21, 2021

I was just trying that, yep adding those keys has returned the LED to webUI and GREEN LED is working again.

@davebuk
Copy link
Contributor

davebuk commented Jan 21, 2021

The button thing is another issue I was going to mention. I have only recently noticed, the physical button turns the device ON/OFF but only to 100% and doesn't show the correct state in the webUI. It brings ON the RED LED only. I guess its bypassing the ESP and controlling the MCU directly? I was trying to get the BUTTON_ACTION to dim UP/DOWN with double and long press, but that doesn't work from build definitions.

@davebuk
Copy link
Contributor

davebuk commented Jan 23, 2021

After doing the factory reset on this device I had to re run: set tuyaChanState 1, set tuyaSwitch0 0 and set tuyaChannel0 2. LED had gone again even though tuya.show listed it and the button GPIO. tuya.save and a reboot has restored the LED in webUI without having to set the keys. Both LED and BUTTON GPIOs are also listed in the keys. The physical button turns the light ON/OFF but doesn't show it's state in the webUI or MQTT.

@davebuk
Copy link
Contributor

davebuk commented Jan 23, 2021

Debug data when pressing the button from an OFF state, the light then now turns ON at a dim level (webUI shows 102) and then pressing again which returns to OFF. The RED LED goes ON/OFF but the GREEN doesn't.

[912598] [TUYA] <=: 55aa0007000501010001010f
[912624] [TUYA] <=: 55aa00070008020200040000001026
[912636] [TUYA] <=: 55aa0007000501010001010f
[912648] [TUYA] <=: 55aa00070008020200040000003a50
[915380] [TUYA] =>: 55aa00000000ff
[915406] [TUYA] <=: 55aa000000010101
[916667] [TUYA] <=: 55aa0007000501010001000e
[916681] [TUYA] <=: 55aa00070008020200040000003a50
[916694] [TUYA] <=: 55aa0007000501010001000e
[916707] [TUYA] <=: 55aa00070008020200040000001026

@timmeh87
Copy link

I am using exact same generic tasmota inline dimmer device that davebuk has (at least the one pictured in #1729)

I flashed the latest commit of espurna onto it (this is literally my first attempt ever at flashing this particular device).

Without "debug_serial_support" set to zero, I got one slider and one switch, and it did not work. After disabling debug serial support, I now get two switches and two sliders, one slider is called "channel #0" and one is called "brightness", and between these two switches and two sliders, they all seem to affect the state of the dimmer, but in a really random way. I cant even really describe it. Sometimes the either switch might work to either turn the output fully on, or fully off, but sometimes they also dont do anything. Depending on the switches state, either slider might be controlling the actual brightness while the other one has no effect, or vice versa. after playing with one control or the other, the effect other controls have seems to change like a game of whack-a-mole.

I figure theres gotta be a bug here somewhere, let me know if I can help somehow

@davebuk
Copy link
Contributor

davebuk commented Jan 25, 2021

@mcspr Is the expert but I can say what I've done. I believe the TUYA code makes an output/relay using some code from a discovery routine. I have then had to use the following commands in the webUI DEBUG console one at a time. set tuyaChanState 1, set tuyaSwitch0 0 and set tuyaChannel0 2 followed by tuya.save. Reboot and you should have just one switch and two sliders. Set brightness to 255 and leave it there. Brightness is controlled by the channel 0 slider. Give it a go and see what you get. There is a separate issue with the physical button but Max is working his magic. 😁

@timmeh87
Copy link

Oh true. I did manage to put them into the console per your previous message but I never saved or rebooted. duh! Ill try this tonight. Thank you!

@timmeh87
Copy link

I can report back that your instructions worked (After doing a "factory reset"), I see the one button and 2 sliders as you said. I also have noticed that changing the slider from a high value to any value to 0 - 16 results in no change until a value greater than 16 is selected. So for example if the power is 100 and you click on "9" on the slider, the power will still be 100. I think I saw a change recently that ignores values 0-16, but I think it would be good to set the value to 16 still, so its still close to what the slider says instead of up to 84 points off.

Ok well let it be known that I have this hardware and am running the latest code on it and anyone can message me if they need me to try something out.

@davebuk
Copy link
Contributor

davebuk commented Jan 26, 2021

I don't think I tried values that low since the original testing when the device was first added to the code base.

It could be worth you making sure that after a cold boot, mains power OFF/ON, the output ends up at OFF. You may see the output flick ON then OFF but it should end up after boot to OFF.

You could also try turning transitions ON under the lights menu. I'm currently running 1600ms time with 100ms steps. With transitions working try a soft and cold reboot and check the output ends up at OFF again.

@mcspr
Copy link
Collaborator Author

mcspr commented Jan 26, 2021

I also have noticed that changing the slider from a high value to any value to 0 - 16 results in no change until a value greater than 16 is selected. So for example if the power is 100 and you click on "9" on the slider, the power will still be 100.

fwiw transitions actually help with the case since they do channel changes step-by-step instead of an immediate jump to the value.

if (rounded <= 0x10) {

but this part may be changed to replace the value to 16 instead of just returning

I'd open a new issue though, github pagination is the worst and this discussion relates to the already-merged code, not the code in the PR

mcspr added a commit that referenced this pull request Jan 26, 2021
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.

TUYA Dimmer behaves erratically (1.14.1, 1.14.2-dev)
3 participants