Skip to content

Commit 659eef7

Browse files
committed
fix ValueError: too many values to unpack when connecting to MQTT broker via IPv6
#42
1 parent 68fa589 commit 659eef7

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Fixed
9+
- `ValueError: too many values to unpack` when connecting to MQTT broker via IPv6
10+
(https://github.com/fphammerle/switchbot-mqtt/issues/42)
811

912
## [3.2.0] - 2022-04-18
1013
### Added

switchbot_mqtt/__init__.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with this program. If not, see <https://www.gnu.org/licenses/>.
1818

1919
import logging
20+
import socket
2021
import typing
2122

2223
import paho.mqtt.client
@@ -36,8 +37,15 @@ def _mqtt_on_connect(
3637
# pylint: disable=unused-argument; callback
3738
# https://github.com/eclipse/paho.mqtt.python/blob/v1.5.0/src/paho/mqtt/client.py#L441
3839
assert return_code == 0, return_code # connection accepted
39-
mqtt_broker_host, mqtt_broker_port = mqtt_client.socket().getpeername()
40-
_LOGGER.debug("connected to MQTT broker %s:%d", mqtt_broker_host, mqtt_broker_port)
40+
mqtt_broker_host, mqtt_broker_port, *_ = mqtt_client.socket().getpeername()
41+
# https://www.rfc-editor.org/rfc/rfc5952#section-6
42+
_LOGGER.debug(
43+
"connected to MQTT broker %s:%d",
44+
f"[{mqtt_broker_host}]"
45+
if mqtt_client.socket().family == socket.AF_INET6
46+
else mqtt_broker_host,
47+
mqtt_broker_port,
48+
)
4149
_ButtonAutomator.mqtt_subscribe(mqtt_client=mqtt_client, settings=userdata)
4250
_CurtainMotor.mqtt_subscribe(mqtt_client=mqtt_client, settings=userdata)
4351

tests/test_mqtt.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with this program. If not, see <https://www.gnu.org/licenses/>.
1818

1919
import logging
20+
import socket
2021
import typing
2122
import unittest.mock
2223

@@ -35,9 +36,23 @@
3536
# pylint: disable=too-many-arguments; these are tests, no API
3637

3738

38-
def test__mqtt_on_connect(caplog: _pytest.logging.LogCaptureFixture) -> None:
39+
@pytest.mark.parametrize(
40+
("socket_family", "peername", "peername_log"),
41+
[
42+
(socket.AF_INET, ("mqtt-broker.local", 1883), "mqtt-broker.local:1883"),
43+
# https://github.com/fphammerle/switchbot-mqtt/issues/42#issuecomment-1173909335
44+
(socket.AF_INET6, ("::1", 1883, 0, 0), "[::1]:1883"),
45+
],
46+
)
47+
def test__mqtt_on_connect(
48+
caplog: _pytest.logging.LogCaptureFixture,
49+
socket_family: int, # socket.AddressFamily,
50+
peername: typing.Tuple[typing.Union[str, int]],
51+
peername_log: str,
52+
) -> None:
3953
mqtt_client = unittest.mock.MagicMock()
40-
mqtt_client.socket().getpeername.return_value = ("mqtt-broker.local", 1883)
54+
mqtt_client.socket().family = socket_family
55+
mqtt_client.socket().getpeername.return_value = peername
4156
with caplog.at_level(logging.DEBUG):
4257
switchbot_mqtt._mqtt_on_connect(
4358
mqtt_client,
@@ -60,7 +75,7 @@ def test__mqtt_on_connect(caplog: _pytest.logging.LogCaptureFixture) -> None:
6075
(
6176
"switchbot_mqtt",
6277
logging.DEBUG,
63-
"connected to MQTT broker mqtt-broker.local:1883",
78+
"connected to MQTT broker " + peername_log,
6479
),
6580
(
6681
"switchbot_mqtt._actors.base",

0 commit comments

Comments
 (0)