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

Add debug mode to config flow #79

Closed
wants to merge 4 commits into from
Closed

Add debug mode to config flow #79

wants to merge 4 commits into from

Conversation

postlund
Copy link
Collaborator

This PR adds a "debug mode" to the config flow. When filling in the basic info, if ticking the "Enable debug mode", the following step is presented:

DDF01DC9-F0D7-45FD-B2F5-C229B04B479A

From here datapoints can be changed to arbitrary values (assuming supported), so it's great to try things out. It also displays current values of all datapoints in text to make it easier to copy.

Albeit, not my finest work... I think this will be pretty great when we ask for help from our users. Thoughts?

@postlund postlund added the enhancement New feature or request label Oct 13, 2020
@postlund postlund requested a review from rospogrigio October 13, 2020 17:44
@postlund
Copy link
Collaborator Author

@rospogrigio Did you try this out?

@postlund
Copy link
Collaborator Author

I can fix the conflict btw.

@rospogrigio
Copy link
Owner

Sorry no, didn't have the time these days. Will try it soon...

@rospogrigio
Copy link
Owner

rospogrigio commented Oct 21, 2020

I was testing this but I get "unknown error occurred". Here are the logs:

2020-10-21 10:51:38 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf89d2c3ba66cb83288njv] Sending command heartbeat (device type: type_0d)
2020-10-21 10:51:38 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf89d2c3ba66cb83288njv] Send payload: b'{}'
2020-10-21 10:51:38 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf89d2c3ba66cb83288njv] Waiting for sequence number -100
2020-10-21 10:51:38 DEBUG (MainThread) [custom_components.localtuya.pytuya] Dispatching message TuyaMessage(seqno=0, cmd=9, retcode=0, payload=b'', crc=2958142211)
2020-10-21 10:51:38 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf89d2c3ba66cb83288njv] Got heartbeat response
2020-10-21 10:51:38 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf89d2c3ba66cb83288njv] Decrypted payload: {}
2020-10-21 10:51:40 ERROR (MainThread) [custom_components.localtuya.config_flow] Unexpected exception
Traceback (most recent call last):
  File "/root/etc2/custom_components/localtuya/config_flow.py", line 270, in async_step_basic_info
    self.dps_strings = await validate_input(self.hass, user_input)
  File "/root/etc2/custom_components/localtuya/config_flow.py", line 213, in validate_input
    detected_dps = await _tuya_command(hass, data, "detect_available_dps")
  File "/root/etc2/custom_components/localtuya/config_flow.py", line 194, in _tuya_command
    interface = pytuya.TuyaInterface(
AttributeError: module 'custom_components.localtuya.pytuya' has no attribute 'TuyaInterface'

@postlund
Copy link
Collaborator Author

Are you missing parts of the exception?

@rospogrigio
Copy link
Owner

Obviously yes, sorry, I edited the previous post.

@postlund
Copy link
Collaborator Author

@rospogrigio Should be fixed now. Must have totally screwed up a rebase at some point...

@rospogrigio
Copy link
Owner

No luck @postlund , I push "+", enter LocalTuya but I get pushed back to the Integrations list, and cannot proceed. I also tried refreshing the cache, can you please check?

@postlund
Copy link
Collaborator Author

Hmm, I tried it yesterday and it worked fine. No errors at all?

@rospogrigio
Copy link
Owner

Nope...

@rospogrigio
Copy link
Owner

@postlund sorry, it was my fault, I had left my main HA instance running so it could not find devices (I believe it should have left the "..." only option, though, instead of going back to the integration list without any message).
Moreover, I cannot use the set_dp. I tried only one switch, a 0a device.
And, I get several of these errors:

2020-10-22 12:31:49 ERROR (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Heartbeat failed (), disconnecting
Traceback (most recent call last):
File "/root/etc2/custom_components/localtuya/pytuya/init.py", line 346, in heartbeat_loop
await self.heartbeat()
File "/root/etc2/custom_components/localtuya/pytuya/init.py", line 435, in heartbeat
return await self.exchange(HEARTBEAT)
File "/root/etc2/custom_components/localtuya/pytuya/init.py", line 407, in exchange
msg = await self.dispatcher.wait_for(seqno)
File "/root/etc2/custom_components/localtuya/pytuya/init.py", line 206, in wait_for
await asyncio.wait_for(self.listeners[seqno].acquire(), timeout=timeout)
File "/usr/lib/python3.7/asyncio/tasks.py", line 409, in wait_for
await waiter
concurrent.futures._base.CancelledError

Let me know...

@postlund
Copy link
Collaborator Author

Yeah, I noticed that as well and have a fix. Will look at the other issue as well.

@postlund
Copy link
Collaborator Author

@rospogrigio Try now.

@rospogrigio
Copy link
Owner

It worked once, but then I could not redo it, nor reload the DPs.
And I started getting these errors:


2020-10-22 13:43:13 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Dispatching sequence number 1
2020-10-22 13:43:13 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Decrypted payload: {"devId":"860687042462ab30b1e5","dps":{"1":false,"7":true,"9":0,"15":0,"18":0,"19":0,"20":2171,"26":0}}
2020-10-22 13:43:13 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Closing connection
2020-10-22 13:43:13 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Connection lost: None
2020-10-22 13:43:13 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Closing connection
2020-10-22 13:43:19 DEBUG (MainThread) [custom_components.localtuya.pytuya] [66058212d8f15bbaf0a8] Sending command heartbeat (device type: type_0a)
2020-10-22 13:43:19 DEBUG (MainThread) [custom_components.localtuya.pytuya] [66058212d8f15bbaf0a8] Send payload: b'{}'
2020-10-22 13:43:19 DEBUG (MainThread) [custom_components.localtuya.pytuya] [66058212d8f15bbaf0a8] Waiting for sequence number -100
2020-10-22 13:43:19 DEBUG (MainThread) [custom_components.localtuya.pytuya] Dispatching message TuyaMessage(seqno=0, cmd=9, retcode=0, payload=b'', crc=2958142211)
2020-10-22 13:43:19 DEBUG (MainThread) [custom_components.localtuya.pytuya] [66058212d8f15bbaf0a8] Got heartbeat response
2020-10-22 13:43:19 DEBUG (MainThread) [custom_components.localtuya.pytuya] [66058212d8f15bbaf0a8] Decrypted payload: {}
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Started heartbeat loop
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Sending command heartbeat (device type: type_0a)
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Send payload: b'{}'
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Waiting for sequence number -100
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Sending command set (device type: type_0a)
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Send payload: b'{"devId":"860687042462ab30b1e5","uid":"860687042462ab30b1e5","t":"1603367007","dps":{"1":true}}'
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Waiting for sequence number 1
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] Dispatching message TuyaMessage(seqno=0, cmd=9, retcode=0, payload=b'', crc=2958142211)
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Got heartbeat response
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Decrypted payload: {}
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] Dispatching message TuyaMessage(seqno=1, cmd=7, retcode=0, payload=b'', crc=2768611604)
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Dispatching sequence number 1
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Decrypted payload: {}
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Closing connection
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Connection lost: None
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Closing connection
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Started heartbeat loop
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Sending command heartbeat (device type: type_0a)
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Send payload: b'{}'
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Waiting for sequence number -100
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Sending command status (device type: type_0a)
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Send payload: b'{"gwId":"860687042462ab30b1e5","devId":"860687042462ab30b1e5"}'
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Waiting for sequence number 1
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] Dispatching message TuyaMessage(seqno=0, cmd=9, retcode=0, payload=b'', crc=2958142211)
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Got heartbeat response
2020-10-22 13:43:27 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Decrypted payload: {}
2020-10-22 13:43:28 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Connection lost: [Errno 104] Connection reset by peer
2020-10-22 13:43:28 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Closing connection
2020-10-22 13:43:28 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Wait was aborted for seqno 1
2020-10-22 13:43:28 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Closing connection
2020-10-22 13:43:39 DEBUG (MainThread) [custom_components.localtuya.pytuya] [66058212d8f15bbaf0a8] Sending command heartbeat (device type: type_0a)
2020-10-22 13:43:39 DEBUG (MainThread) [custom_components.localtuya.pytuya] [66058212d8f15bbaf0a8] Send payload: b'{}'
2020-10-22 13:43:39 DEBUG (MainThread) [custom_components.localtuya.pytuya] [66058212d8f15bbaf0a8] Waiting for sequence number -100
2020-10-22 13:43:39 DEBUG (MainThread) [custom_components.localtuya.pytuya] Dispatching message TuyaMessage(seqno=0, cmd=9, retcode=0, payload=b'', crc=2958142211)
2020-10-22 13:43:39 DEBUG (MainThread) [custom_components.localtuya.pytuya] [66058212d8f15bbaf0a8] Got heartbeat response
2020-10-22 13:43:39 DEBUG (MainThread) [custom_components.localtuya.pytuya] [66058212d8f15bbaf0a8] Decrypted payload: {}
2020-10-22 13:43:42 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Started heartbeat loop
2020-10-22 13:43:42 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Sending command heartbeat (device type: type_0a)
2020-10-22 13:43:42 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Send payload: b'{}'
2020-10-22 13:43:42 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Waiting for sequence number -100
2020-10-22 13:43:42 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Sending command status (device type: type_0a)
2020-10-22 13:43:42 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Send payload: b'{"gwId":"860687042462ab30b1e5","devId":"860687042462ab30b1e5"}'
2020-10-22 13:43:42 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Waiting for sequence number 1
2020-10-22 13:43:42 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Connection lost: [Errno 104] Connection reset by peer
2020-10-22 13:43:42 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Closing connection
2020-10-22 13:43:42 ERROR (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Heartbeat failed (), disconnecting
Traceback (most recent call last):
  File "/root/etc2/custom_components/localtuya/pytuya/__init__.py", line 346, in heartbeat_loop
    await self.heartbeat()
  File "/root/etc2/custom_components/localtuya/pytuya/__init__.py", line 435, in heartbeat
    return await self.exchange(HEARTBEAT)
  File "/root/etc2/custom_components/localtuya/pytuya/__init__.py", line 407, in exchange
    msg = await self.dispatcher.wait_for(seqno)
  File "/root/etc2/custom_components/localtuya/pytuya/__init__.py", line 206, in wait_for
    await asyncio.wait_for(self.listeners[seqno].acquire(), timeout=timeout)
  File "/usr/lib/python3.7/asyncio/tasks.py", line 409, in wait_for
    await waiter
concurrent.futures._base.CancelledError
2020-10-22 13:43:42 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Stopped heartbeat loop
2020-10-22 13:43:42 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Closing connection
2020-10-22 13:43:42 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Wait was aborted for seqno 1
2020-10-22 13:43:42 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Closing connection
2020-10-22 13:43:45 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Started heartbeat loop
2020-10-22 13:43:45 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Sending command heartbeat (device type: type_0a)
2020-10-22 13:43:45 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Send payload: b'{}'
2020-10-22 13:43:45 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Waiting for sequence number -100
2020-10-22 13:43:45 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Sending command status (device type: type_0a)
2020-10-22 13:43:45 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Send payload: b'{"gwId":"860687042462ab30b1e5","devId":"860687042462ab30b1e5"}'
2020-10-22 13:43:45 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Waiting for sequence number 1
2020-10-22 13:43:45 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Connection lost: [Errno 104] Connection reset by peer
2020-10-22 13:43:45 DEBUG (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Closing connection
2020-10-22 13:43:45 ERROR (MainThread) [custom_components.localtuya.pytuya] [860687042462ab30b1e5] Heartbeat failed (), disconnecting
Traceback (most recent call last):
  File "/root/etc2/custom_components/localtuya/pytuya/__init__.py", line 346, in heartbeat_loop
    await self.heartbeat()
  File "/root/etc2/custom_components/localtuya/pytuya/__init__.py", line 435, in heartbeat
    return await self.exchange(HEARTBEAT)
  File "/root/etc2/custom_components/localtuya/pytuya/__init__.py", line 407, in exchange
    msg = await self.dispatcher.wait_for(seqno)
  File "/root/etc2/custom_components/localtuya/pytuya/__init__.py", line 206, in wait_for
    await asyncio.wait_for(self.listeners[seqno].acquire(), timeout=timeout)
  File "/usr/lib/python3.7/asyncio/tasks.py", line 409, in wait_for
    await waiter
concurrent.futures._base.CancelledError

@postlund
Copy link
Collaborator Author

Since you get Connection reset by peer, are the plug used by somebody else?

@rospogrigio
Copy link
Owner

Nope, I have been viewing the effect with the Jinvoo app, but none of my devices has shown "single connection" needs...

@rospogrigio
Copy link
Owner

rospogrigio commented Oct 22, 2020

Put my phone in airplane mode and retried, with a different switch. The issues I see:

  1. after a set_dps, I always get the "concurrent.futures._base.CancelledError", both if successful and unsuccessful
  2. after a set_dps, the "current state" label above is empty, and also the DPs drop-down menu is empty, requiring to launch a reload_dps, which is annoying
  3. the lazy part of me (that you know quite well by now) would leave one Data type set by default, maybe int or bool since I believe they should be the most used 😉

@postlund
Copy link
Collaborator Author

So, I think I figured out what was wrong. I currently do "one-shot" commands, I.e. open a connection, do something and then close the connection. At some point I changed to always re-read the values of all datapoints after doing something. So if you for instance changed a datapoint it would then re-read the values, causing two connection attempts to the device within a very short time span. It seems that there should be a grace period of a second or so between connections. I believe this is the main reason for the problems you see. Now you have to reload manually every time unfortunately. But good enough for now.

I changed int to be default and to set precious values as default, to avoid getting empty boxes. Please give it another try!

@rospogrigio
Copy link
Owner

@postlund been trying this. It works, but here are my considerations:

  1. you have to manually reload after a set_dp . It's a bit annoying and confusing but if it's hard/long to fix I think we can live with this
  2. the lazy part of me appreciated much the fact that the last Data type used is kept
  3. sometimes, reload_dps works just once: if you repeat the command, you get a "User input malformed" error, until you make a set_dp and then it starts to work again. We can live with this as for 1), but it would be nice to fix it

Let me know your comments, bye!

@rospogrigio
Copy link
Owner

@postlund what do you think of my last considerations? do you want to fix those things or shall we merge?
Let me know

@postlund
Copy link
Collaborator Author

@rospogrigio Let me have a look at it tonight again, I'll let you know by then.

@postlund
Copy link
Collaborator Author

Ok, so now I have taken a looong time thinking about this. My conclusion is that I don't want to go this road anymore as it turns the config flow into a mess. I have another idea instead that I think is better in some sense.

When implementing #56, we can add all datapoints as state attribute instead. That makes them easy to copy, view and it's possible to extract datapoints and create complex templates that out sensor platforms doesn't support, etc.

To handle manual change of a datapoint, we can add a service instead (e.g. localtuya.set_dp). That way we can change datapoints from automations or Lovelace in general. Makes more sense to me. What do you think @rospogrigio?

@rospogrigio
Copy link
Owner

Looks like an excellent idea, in particular if you believe it is making the config flow messy (don't want you to suffer headaches!! 😆 ).
Feel free to close this PR then.
Bye!

@postlund
Copy link
Collaborator Author

Yeah, the config flow changes here are a nightmare... I will close this and come back later with another PR or two.

@postlund postlund closed this Oct 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants