Skip to content

Commit

Permalink
add command-line option --mqtt-disable-tls (for upward compatibility;…
Browse files Browse the repository at this point in the history
… enabled by default)
  • Loading branch information
fphammerle committed Apr 2, 2022
1 parent 4ffe0c4 commit 3431242
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- command-line option `--mqtt-enable-tls`
- command-line option `--mqtt-disable-tls` (enabled by default)

### Deprecated
- invocation without `--mqtt-enable-tls` and `--mqtt-disable-tls`

## [3.0.0] - 2022-02-05
### Added
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ $ pip3 install --user --upgrade switchbot-mqtt
## Usage

```sh
$ switchbot-mqtt --mqtt-host HOSTNAME_OR_IP_ADDRESS
$ switchbot-mqtt --mqtt-host HOSTNAME_OR_IP_ADDRESS --mqtt-enable-tls
# or
$ switchbot-mqtt --mqtt-host HOSTNAME_OR_IP_ADDRESS --mqtt-disable-tls
```

Use `sudo hcitool lescan`
Expand Down
14 changes: 13 additions & 1 deletion switchbot_mqtt/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import logging
import os
import pathlib
import warnings

import switchbot

Expand All @@ -44,11 +45,15 @@ def _main() -> None:
type=int,
help=f"default {_MQTT_DEFAULT_PORT} ({_MQTT_DEFAULT_TLS_PORT} with --mqtt-enable-tls)",
)
argparser.add_argument(
mqtt_tls_argument_group = argparser.add_mutually_exclusive_group()
mqtt_tls_argument_group.add_argument(
"--mqtt-enable-tls",
action="store_true",
help="TLS will be enabled by default in the next major release",
)
mqtt_tls_argument_group.add_argument( # for upward compatibility
"--mqtt-disable-tls", action="store_true", help="Currently enabled by default"
)
argparser.add_argument("--mqtt-username", type=str)
password_argument_group = argparser.add_mutually_exclusive_group()
password_argument_group.add_argument("--mqtt-password", type=str)
Expand Down Expand Up @@ -110,6 +115,13 @@ def _main() -> None:
mqtt_port = _MQTT_DEFAULT_TLS_PORT
else:
mqtt_port = _MQTT_DEFAULT_PORT
if not args.mqtt_enable_tls and not args.mqtt_disable_tls:
warnings.warn(
"In switchbot-mqtt's next major release, TLS will be enabled by default"
" (--mqtt-enable-tls)."
" Please add --mqtt-disable-tls to your command for upward compatibility.",
UserWarning, # DeprecationWarning ignored by default
)
if args.mqtt_password_path:
# .read_text() replaces \r\n with \n
mqtt_password = args.mqtt_password_path.read_bytes().decode()
Expand Down
30 changes: 29 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def test__main(
) -> None:
with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
"sys.argv", argv
):
), pytest.warns(UserWarning, match=r"Please add --mqtt-disable-tls\b"):
switchbot_mqtt._cli._main()
run_mock.assert_called_once_with(
mqtt_host=expected_mqtt_host,
Expand Down Expand Up @@ -234,6 +234,21 @@ def test__main_device_password_file(
}


def test__main_mqtt_disable_tls_implicit() -> None:
with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
"sys.argv", ["", "--mqtt-host", "mqtt.local"]
), pytest.warns(UserWarning, match=r"Please add --mqtt-disable-tls\b"):
switchbot_mqtt._cli._main()
run_mock.assert_called_once_with(
**{
**_RUN_DEFAULT_KWARGS,
"mqtt_host": "mqtt.local",
"mqtt_disable_tls": True,
"mqtt_port": 1883,
}
)


def test__main_mqtt_enable_tls() -> None:
with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
"sys.argv", ["", "--mqtt-host", "mqtt.local", "--mqtt-enable-tls"]
Expand All @@ -255,6 +270,19 @@ def test__main_mqtt_enable_tls_overwrite_port() -> None:
)


def test__main_mqtt_tls_collision(capsys: _pytest.capture.CaptureFixture) -> None:
with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
"sys.argv",
["", "--mqtt-host", "mqtt.local", "--mqtt-enable-tls", "--mqtt-disable-tls"],
), pytest.raises(SystemExit):
switchbot_mqtt._cli._main()
run_mock.assert_not_called()
assert (
"error: argument --mqtt-disable-tls: not allowed with argument --mqtt-enable-tls\n"
in capsys.readouterr()[1]
)


def test__main_fetch_device_info() -> None:
with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
"sys.argv",
Expand Down

0 comments on commit 3431242

Please sign in to comment.