jalmeroth / homie-python

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

'localMac' referenced before assignment #43

Closed fabiosoft closed 6 years ago

fabiosoft commented 6 years ago

Something went wrong with the last update.

    self.publishLocalipAndMac()
  File "/Library/Python/2.7/site-packages/homie/main.py", line 349, in publishLocalipAndMac
    payload=localMac, retain=True)
UnboundLocalError: local variable 'localMac' referenced before assignment

I thought my code was wrong so i tested with demo example in readme just changing the config from file to dictionary. Same error. And worst, nothing is published to the broker.

Here is the code

import homie
import time

config = {
        "HOST": "localhost",
        "PORT": 1883,
        "KEEPALIVE": 10,
        "DEVICE_ID": "onid2",
        "DEVICE_NAME": "name",
        "TOPIC": "topic/services"
    }
Homie = homie.Homie(config)
temperatureNode = Homie.Node("temperature", "temperature")

def main():
    Homie.setFirmware("awesome-temperature", "1.0.0")
    temperatureNode.advertise("degrees")

    Homie.setup()

    while True:
        temperature = 22.0

        print("Temperature: {:0.2f} C".format(temperature))
        temperatureNode.setProperty("degrees").send(temperature)

        time.sleep(1)

if __name__ == '__main__':
    try:
        main()
    except (KeyboardInterrupt, SystemExit):
        logger.info("Quitting.")
jalmeroth commented 6 years ago

Hi @fabiosoft,

thanks for your report. Looks like there is a problem with enumerating your network interfaces. Can you share some more info on your setup?

(I think we will need to add a more verbose logging and especially a more granular try/catch-routine in this area...)

Best regards, Jan

fabiosoft commented 6 years ago

what info do you need? I experimented that: using localhost raises the exception while using iot.eclipse.org works. But of course my broker must be on localhost

jalmeroth commented 6 years ago

Which Hardware/OS/Python are you running? Any other specialities?

jalmeroth commented 6 years ago

Same error. And worst, nothing is published to the broker.

You should see at least some messages (HomieVersion, Fwname, Fwversion, Nodes) on your broker, don't you?

fabiosoft commented 6 years ago

Python 2.7, Mac OS high sierra during development but will be Debian in production. Mosquito broker. Nothing published. Only lwt online to false but nothing to true.

I already use homie on several esp8266 so I know what expect also in python and it worked really good before last update.

jalmeroth commented 6 years ago

On your Mac, can you try to publish a message like mosquitto_pub -h localhost -t "some/test" -m "hello"

I already use homie on several esp8266 so I know what expect also in python and it worked really good before last update.

They are probably not running on your dev-machine ;)

jalmeroth commented 6 years ago

Ok, I can replicate the issue on my machine now. Here's the full trace in case someone wants to fix it before I find the time :)

2018-05-29 00:01:34,374 MainThread isIdFormat DEBUG isIdFormat
2018-05-29 00:01:34,374 MainThread generateDeviceId DEBUG generateDeviceId
2018-05-29 00:01:34,381 MainThread deviceId WARNING Invalid deviceId specified. Using 'xxxxxxxxxxxx' instead.
2018-05-29 00:01:34,381 MainThread _initAttrs DEBUG ca_certs: 
2018-05-29 00:01:34,381 MainThread isIdFormat DEBUG isIdFormat
2018-05-29 00:01:34,382 MainThread _initAttrs DEBUG deviceId: xxxxxxxx
2018-05-29 00:01:34,382 MainThread _initAttrs DEBUG deviceName: xxxxxxxx
2018-05-29 00:01:34,382 MainThread _initAttrs DEBUG host: localhost
2018-05-29 00:01:34,382 MainThread _initAttrs DEBUG keepalive: 10
2018-05-29 00:01:34,382 MainThread _initAttrs DEBUG password: 
2018-05-29 00:01:34,382 MainThread _initAttrs DEBUG port: 1883
2018-05-29 00:01:34,383 MainThread _initAttrs DEBUG qos: 1
2018-05-29 00:01:34,383 MainThread _initAttrs DEBUG subscribe_all: False
2018-05-29 00:01:34,383 MainThread _initAttrs DEBUG baseTopic: homie
2018-05-29 00:01:34,383 MainThread _initAttrs DEBUG username: 
2018-05-29 00:01:34,384 MainThread setFirmware DEBUG relay-switch: 1.0.0
2018-05-29 00:01:34,384 MainThread isIdFormat DEBUG isIdFormat
2018-05-29 00:01:34,384 MainThread subscribe DEBUG subscribe: homie/xxxxxxxx/switch/on/set 1
2018-05-29 00:01:34,394 MainThread on_log DEBUG Log: Sending CONNECT (u0, p0, wr1, wq0, wf1, c1, k10) client_id=b'Homie-xxxxxxxx'
2018-05-29 00:01:34,395 uptimeTimer publish WARNING Not connected.
2018-05-29 00:01:34,396 Thread-1 on_log DEBUG Log: Received CONNACK (0, 0)
2018-05-29 00:01:34,396 signalTimer publishSignal DEBUG [Errno 2] No such file or directory: '/proc/net/wireless'
2018-05-29 00:01:34,396 Thread-1 mqtt_connected DEBUG connected: True
2018-05-29 00:01:34,396 signalTimer publish WARNING Not connected.
2018-05-29 00:01:34,396 Thread-1 _subscribe DEBUG Subscriptions: [('homie/xxxxxxxx/switch/on/set', 1)]
2018-05-29 00:01:34,397 Thread-1 on_log DEBUG Log: Sending SUBSCRIBE (d0) [(b'homie/xxxxxxxx/switch/on/set', 1)]
2018-05-29 00:01:34,397 Thread-1 on_log DEBUG Log: Sending PUBLISH (d0, q1, r1, m2), 'b'homie/xxxxxxxx/$online'', ... (4 bytes)
2018-05-29 00:01:34,397 Thread-1 publish DEBUG 2 > homie/xxxxxxxx/$online true True
2018-05-29 00:01:34,397 Thread-1 on_log DEBUG Log: Sending PUBLISH (d0, q1, r1, m3), 'b'homie/xxxxxxxx/$name'', ... (8 bytes)
2018-05-29 00:01:34,397 Thread-1 publish DEBUG 3 > homie/xxxxxxxx/$name xxxxxxxx True
2018-05-29 00:01:34,397 Thread-1 on_log DEBUG Log: Sending PUBLISH (d0, q1, r1, m4), 'b'homie/xxxxxxxx/$homie'', ... (5 bytes)
2018-05-29 00:01:34,397 Thread-1 publish DEBUG 4 > homie/xxxxxxxx/$homie 2.0.1 True
2018-05-29 00:01:34,398 Thread-1 on_log DEBUG Log: Sending PUBLISH (d0, q1, r1, m5), 'b'homie/xxxxxxxx/$fw/name'', ... (12 bytes)
2018-05-29 00:01:34,398 Thread-1 publish DEBUG 5 > homie/xxxxxxxx/$fw/name relay-switch True
2018-05-29 00:01:34,398 Thread-1 on_log DEBUG Log: Sending PUBLISH (d0, q1, r1, m6), 'b'homie/xxxxxxxx/$fw/version'', ... (5 bytes)
2018-05-29 00:01:34,398 Thread-1 publish DEBUG 6 > homie/xxxxxxxx/$fw/version 1.0.0 True
2018-05-29 00:01:34,398 Thread-1 on_log DEBUG Log: Sending PUBLISH (d0, q1, r1, m7), 'b'homie/xxxxxxxx/$nodes'', ... (6 bytes)
2018-05-29 00:01:34,398 Thread-1 publish DEBUG 7 > homie/xxxxxxxx/$nodes switch True
2018-05-29 00:01:34,399 Thread-1 on_log DEBUG Log: Sending PUBLISH (d0, q1, r1, m8), 'b'homie/xxxxxxxx/switch/$type'', ... (6 bytes)
2018-05-29 00:01:34,399 Thread-1 publish DEBUG 8 > homie/xxxxxxxx/switch/$type switch True
2018-05-29 00:01:34,399 Thread-1 on_log DEBUG Log: Sending PUBLISH (d0, q1, r1, m9), 'b'homie/xxxxxxxx/switch/$properties'', ... (11 bytes)
2018-05-29 00:01:34,399 Thread-1 publish DEBUG 9 > homie/xxxxxxxx/switch/$properties on:settable True
2018-05-29 00:01:34,403 Thread-1 publishLocalipAndMac WARNING 18
2018-05-29 00:01:34,403 Thread-1 on_log DEBUG Log: Sending PUBLISH (d0, q1, r1, m10), 'b'homie/xxxxxxxx/$localip'', ... (9 bytes)
2018-05-29 00:01:34,403 Thread-1 publish DEBUG 10 > homie/xxxxxxxx/$localip 127.0.0.1 True
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/Users/jan/Applications/homebrew/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/Users/jan/Applications/homebrew/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/jan/.venvs/homie-python/lib/python3.6/site-packages/paho/mqtt/client.py", line 2650, in _thread_main
    self.loop_forever(retry_first_connection=True)
  File "/Users/jan/.venvs/homie-python/lib/python3.6/site-packages/paho/mqtt/client.py", line 1481, in loop_forever
    rc = self.loop(timeout, max_packets)
  File "/Users/jan/.venvs/homie-python/lib/python3.6/site-packages/paho/mqtt/client.py", line 1003, in loop
    rc = self.loop_read(max_packets)
  File "/Users/jan/.venvs/homie-python/lib/python3.6/site-packages/paho/mqtt/client.py", line 1284, in loop_read
    rc = self._packet_read()
  File "/Users/jan/.venvs/homie-python/lib/python3.6/site-packages/paho/mqtt/client.py", line 1849, in _packet_read
    rc = self._packet_handle()
  File "/Users/jan/.venvs/homie-python/lib/python3.6/site-packages/paho/mqtt/client.py", line 2311, in _packet_handle
    return self._handle_connack()
  File "/Users/jan/.venvs/homie-python/lib/python3.6/site-packages/paho/mqtt/client.py", line 2372, in _handle_connack
    self.on_connect(self, self._userdata, flags_dict, result)
  File "/Users/jan/R-D/homie-python/homie/main.py", line 201, in _connected
    self.publishLocalipAndMac()
  File "/Users/jan/R-D/homie-python/homie/main.py", line 349, in publishLocalipAndMac
    payload=localMac, retain=True)
UnboundLocalError: local variable 'localMac' referenced before assignment
fabiosoft commented 6 years ago

of course mosquitto_pub command works.