Reconnect MySensors Ethernet Gateway when it is back

Hello,

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.

Any suggestion? Can this be improved?

Thank you.

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ā€.

Any recommendation on how can I get started?

Thanks

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.

This is a good start :slight_smile:
I will have a look and let you know if I have any doubt (probably yes :blush:)

We keep in touch.

1 Like

Great. Please read the contributions instructions in the pymysensors repo. It will help you with the PR.

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.

Thanks for your advice.

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.

Ok. I did something that is doing the job but I donā€™t know if that is the way to do it.

You can see it here:
https://github.com/afeno/pymysensors/commit/adc3dfbe62c68ae860aa60f6f416263f32db8cf3

Btw, I still donā€™t know how to use git very wellā€¦

This is what I did:

  1. Modify the is_sensor and set_child_value to allow to send internal messages (id=255)
  2. 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.
  3. 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 :slight_smile:

Now, is it possible to test this version of pymysensors with my HA setup?

Regards,
Alfredo.

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.

Ok. More to learn I believeā€¦ I Will try to do all that. :worried:

I submitted the PR.
Iā€™m not sure if I did it ok or not.

Letā€™s see.

1 Like

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.

I was out for a couple of months really disconnected from everything. The fix is almost ready so I will try to finalize it next week.

2 Likes

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.

My esp8266 gateway is reconnecting to HA now. :slight_smile:

2 Likes

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.

1 Like
1 Like

Hi

Can someone just give me a step by step guide how to apply and run this update. I am fairly new to this.

Probably easiest to wait until tomorrow sometime when the release (0.42) usually goes out.