jalmeroth / homie-python

A Python-implementation of the homie v2 convention.
https://github.com/marvinroger/homie
54 stars 15 forks source link

Error starting the library without local connection #11

Closed bodiroga closed 8 years ago

bodiroga commented 8 years ago

Hi @jalmeroth!

Many thanks for developing this awesome library, it works really great and simplifies the creation of homie nodes using a computer like a rasberry pi!

However, I have a little problem when the library calls the Homie.setup() function and the device doesn't have IP connectivity (the WiFi is not reliable at that moment or the script has started too early and the device is not connected to the router). This is error:

[Errno -2] Name or service not known

Is it possible to change the setup behavior to wait until the connection is done?

Thanks for your help and keep up with the great work!

jpmens commented 8 years ago

I think you should attempt to have your script pause or wait until you know that the machine has connectivity by, say, waiting a few seconds until you launch your program.

jalmeroth commented 8 years ago

Hi @bodiroga,

thanks for your feedback, I am glad you like it.

I think what @jpmens suggests is, what I have done with a systemd service unit so far:

[Unit]
Description=Homie Python
After=network.target

[Service]
Type=simple
User=%i
ExecStartPre=/usr/bin/sleep 10
ExecStart=/usr/local/bin/homeauto/homie_python.py
WorkingDirectory=/usr/local/bin/homeauto

[Install]
WantedBy=multi-user.target

So before starting the homie-python script systemd waits 10 seconds (which is also true when restarting the service meh).

I could imagine to implement a try/except-block to catch the error, sleep a bit, try again and finally fail if still not connected. Would that help in your case?

Best regards, Jan

jalmeroth commented 8 years ago

This works for me, what do you think?

bodiroga commented 8 years ago

Hi @jpmens and @jalmeroth!

Yeah, that is my current temporal solution, I have added a time.sleep(10) in the main() function before Homie.setup() is called, but it feels a bit hackish and the 10 seconds value is arbitrary, it is a bit trial and error.

Anyway, @jalmeroth, what you have added should work 99% of the time, so it is a nice addition! Many thanks for your support and your fast response, fantastic work!

Best regards,

Aitor

jalmeroth commented 8 years ago

The update has been released to PyPi now too. :)