jasonacox / tuyapower

Python module to read status and energy monitoring data from Tuya based WiFi smart devices. This includes state (on/off), current (mA), voltage (V), and power (wattage).
MIT License
140 stars 22 forks source link

python error: local variable 'sw' referenced before assignment #11

Closed ricorico94 closed 3 years ago

ricorico94 commented 3 years ago

Hi, When I tried to launch plugjson.py on a smart plug being switched off, I received a python error message on line 175. I'm unfortunately not an expert in python, but I think tehre might be indeed a line missing between rows 174 and 175 to initiate variable sw (entry missing like: sw = False ?). Instead of if watchdog > RETRY: log.info( "NO POWER DATA: Response from plug %s [%s] missing power data." % (deviceid, ip) ) return (sw, w, mA, V, "Missing Power Data") maybe we should have: if watchdog > RETRY: log.info( "NO POWER DATA: Response from plug %s [%s] missing power data." % (deviceid, ip) ) sw = False return (sw, w, mA, V, "Missing Power Data") At least, I don't have the error anymore that way. Let me know if that's the way it should be or if I solved just by chance.. br, Ricorico94

jasonacox commented 3 years ago

Great catch, Ricorico94! Your fix would work, but I updated the code to initialize sw with the rest of the variables. Pull latest and see if that fixes the error.

# default polling response for error condition
_DEFAULTS = (False, -99, -99, -99)
sw, w, mA, V = _DEFAULTS