Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't run: ValueError: too many values to unpack #42

Closed
enlight3d opened this issue Jul 29, 2021 · 13 comments
Closed

Can't run: ValueError: too many values to unpack #42

enlight3d opened this issue Jul 29, 2021 · 13 comments
Labels
bug Something isn't working

Comments

@enlight3d
Copy link

Hello, I first tried running the program using docker but no luck (PermissionError: Operation not permitted error from python in the container) so I thought I could try the normal way, but upon launching it, I'm getting this error:

pi@raspberrypi:~ $ switchbot-mqtt --mqtt-host localhost
2021-07-29T14:34:35+0200:INFO:switchbot_mqtt:connecting to MQTT broker localhost:1883
Traceback (most recent call last):
  File "/home/pi/.local/bin/switchbot-mqtt", line 10, in <module>
    sys.exit(_main())
  File "/home/pi/.local/lib/python3.7/site-packages/switchbot_mqtt/__init__.py", line 374, in _main
    device_passwords=device_passwords,
  File "/home/pi/.local/lib/python3.7/site-packages/switchbot_mqtt/__init__.py", line 313, in _run
    mqtt_client.loop_forever()
  File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 1779, in loop_forever
    rc = self.loop(timeout, max_packets)
  File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 1181, in loop
    rc = self.loop_read(max_packets)
  File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 1572, in loop_read
    rc = self._packet_read()
  File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 2310, in _packet_read
    rc = self._packet_handle()
  File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 2942, in _packet_handle
    return self._handle_connack()
  File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 3030, in _handle_connack
    self, self._userdata, flags_dict, result)
  File "/home/pi/.local/lib/python3.7/site-packages/switchbot_mqtt/__init__.py", line 285, in _mqtt_on_connect
    mqtt_broker_host, mqtt_broker_port = mqtt_client.socket().getpeername()
ValueError: too many values to unpack (expected 2)

I did have a mosquitto client running on docker when I tried this command.

I'm running this on a RPI3B+ using raspbian buster.

@fphammerle
Copy link
Owner

Hi, I am surprised that getpeername() returns more than (host, port).
Could you edit /home/pi/.local/lib/python3.7/site-packages/switchbot_mqtt/__init__.py, add print(mqtt_client.socket().getpeername()) before line 285, and post the output of print ?

@fphammerle fphammerle added the bug Something isn't working label Jul 29, 2021
@enlight3d
Copy link
Author

here you go:

pi@raspberrypi:~ $ switchbot-mqtt --mqtt-host localhost
2021-07-29T15:35:45+0200:INFO:switchbot_mqtt:connecting to MQTT broker localhost:1883
('::1', 1883, 0, 0)

@enlight3d
Copy link
Author

hum, that's strange, I can't get the same error now as I installed mosquitto on raspbian, not using it in a container anymore. But it stays on : INFO:switchbot_mqtt:connecting to MQTT broker localhost:1883

@enlight3d
Copy link
Author

If you have discord maybe we can try to debug this together ?

@fphammerle
Copy link
Owner

But it stays on : INFO:switchbot_mqtt:connecting to MQTT broker localhost:1883

Could you verify that mosquitto is fully accessible at localhost:1883?
E.g. via nc -z -v localhost 1883 (should indicate Connection to localhost (127.0.0.1) 1883 port [tcp/*] succeeded!) or mosquitto_pub

If you have discord maybe we can try to debug this together ?

I would prefer discussing the issue here (for future reference and because asynchronous communication is easier for me)

@fphammerle
Copy link
Owner

closing due to inactivity

@rursache
Copy link

rursache commented Jul 4, 2022

i had the same issue as @enlight3d.
yes, running nc -z -v localhost 1883 (after sudo apt install netcat -y) will return Connection to localhost (::1) 1883 port [tcp/*] succeeded!

i fixed the issue by editing the file at /home/pi/.local/lib/python3.9/site-packages/switchbot_mqtt/__init__.py like this:

line 39:

mqtt_broker_host, mqtt_broker_port = mqtt_client.socket().getpeername()

became

peerTuple = mqtt_client.socket().getpeername()
mqtt_broker_host, mqtt_broker_port = (peerTuple[0], peerTuple[1])

@fphammerle if you have time maybe this should get pushed into the lib. my python skills are minimal and i'm not confident this is the best way to fix this

@fphammerle fphammerle reopened this Jul 4, 2022
@fphammerle
Copy link
Owner

Hi @rursache, as previously said, I didn't expect getpeername() to return more than (host, port).
Could you maybe add a print(peerTuple) between your code and share the output? (just for my understanding)
The docs are a bit vague unfortunately:

socket.getpeername()
Return the remote address to which the socket is connected. This is useful to find out the port number of a remote IPv4/v6 socket, for instance. (The format of the address returned depends on the address family — see above.) On some systems this function is not supported.

https://docs.python.org/3/library/socket.html#socket.socket.getpeername
What operating system are you on?

@rursache
Copy link

rursache commented Jul 4, 2022

the output is ('::1', 1883, 0, 0) as @enlight3d stated above

@fphammerle
Copy link
Owner

Thanks! Would mqtt_broker_host, mqtt_broker_port = mqtt_client.socket().getpeername()[:2] work?

@rursache
Copy link

rursache commented Jul 4, 2022

can't check right now but there's no reason not to, it's just another way to unwrap the tuple. i would argue that

mqtt_broker_host, mqtt_broker_port, *_ = mqtt_client.socket().getpeername()

looks better.

i think that the 0, 0 is related to having docker installed but i'm not sure as i don't have a pi without docker installed to check.

@fphammerle
Copy link
Owner

Thanks! I pushed a fix to the main branch.

@fphammerle
Copy link
Owner

fix released as v3.2.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants