I noticed that HA is not trying to reconnect the MySensors Ethernet gateway when it is temporary disconnected from the network.
Maybe this is not happening very often when using the Ethernet gateway. But with the new MySensors ESP8266 Wifi gateway it might happen from time to time. And it is a pity to have to restart HA to get it connected again.
Is this the normal behaviour or something is failing in my setup? I have to restart HA every time that the Gateway is temporary disconnected from the LAN.
Iām interested in this also. My Ethernet gateway is connected to a āhome plugā powerline connector that drops out every now and thenā¦ most of the time everything keeps on working but every so often it stops updating sensors and doesnāt send commands from hass
This is a known limitation in the pymysensors package that is used in home assistant to interface with mysensors. Itās on my todo but a PR is also welcome.
I would like to help but I donāt know where to start. Iām not an expert programmer but I know some Perl and Python.
I can have a look and try to contribute to this fix.
Iām not having experience on this type of ācollaborative developmentā.
Check out the dev branch of pymysensors using git from the repo at github.
You should add a way of knowing if the socket connection is still alive or not in theTCPGateway class. You could check this by requesting the version of the gateway or similar using an internal message. Probably add a method that includes this logic and returns True or False. You could also extend the connect method of the TCPGateway class.
Use the method in the run loop. If return value is false, reconnect.
Ok. Iām doing some progress and learning at the same time.
I managed to create a new method (reusing the set_child_value) to send internal messages. Now, Iām working to start a thread every X minutes to send an internal message to the gateway in order to test the tcp connection. If the test fails, then a reconnect will be done.
Am I going on the good direction?
However, there is something that I donāt understandā¦
The code currently handle an exception on the recv_timeout and sent methods if the connections is broken when trying to send or receive. That is working fine on the pymysensors and then the main thread tries to connect every 10 seconds.
Why in HA it is not working like that? HA is never trying to reconnect even if there are messages to send.
You shouldnāt need to start a new thread just create a new socket. This should happen in the connect method I think. But only do this if you think the connection is down. Not by default.
Canāt say more about reconnecting in home assistant. When I tested it, it worked. Need to have a look at logs or test it myself to say more.
Btw, I still donāt know how to use git very wellā¦
This is what I did:
Modify the is_sensor and set_child_value to allow to send internal messages (id=255)
After connect() start checking the connection every 10 seconds, sending a HEARTBEAT message to the gateway.
If the sent() is not successful then the socket will be closed and the main thread will try to create a new connection every 10 seconds.
The disconnect() will stop the tcp_check because at this moment we know that the socket is closed.
Probably there are other ways to do it but Iām still learning
Now, is it possible to test this version of pymysensors with my HA setup?
Please checkout a new branch with your changes and rebase that on the dev branch. Right now youāve made changes to the master branch. Then make a pull request to pymysensors dev branch. Then I can more easily comment.
See CONTRIBUTING.md in the repo for git instructions. Itāll be something like this if we assume you have commited all changes to master:
git fetch upstream dev
git checkout dev
git checkout -b tcp-reconnect master
git rebase dev
# solve any conflicts, by editing and saving the correct state, then add the saved files and continue rebase (git rebase --continue)
git push origin HEAD
You can simplify the rebase by first squashing your commits into one commit. See how to use interactive mode when rebasing.
I believe I just ran into the same issue after building an ethernet gateway, and I was wondering what the status of the PR is? @alfredofenoglio: I read through the PRās history and it looks as if you are almost there with fixing the issue. Do you think you would be able to finalise the work anytime soon? Do you need any help with anything? Thanks.
PR is finished and accepted by @martinhjelmare.
I have to admit that I learned a lot with this small contribution. I was following @martinhjelmare recommendations all the time what allowed me to learn python, mysensors and GitHub.
Great. I just tested your patch with HA 0.38.3, and a simple MySensors 2.1.1 Ethernet gateway, and can confirm that it is working as expected - HA is successfully reconnecting after the gateway has been reset.
Canāt wait for the official release. Thanks for your contribution @alfredofenoglio.