Browse Source

`--fetch-device-info` can now alternatively be enabled by setting env var `FETCH_DEVICE_INFO`

https://github.com/fphammerle/switchbot-mqtt/issues/50#issuecomment-946191077
Fabian Peter Hammerle 2 years ago
parent
commit
ef51646189
4 changed files with 30 additions and 9 deletions
  1. 2 0
      CHANGELOG.md
  2. 1 0
      README.md
  3. 8 2
      switchbot_mqtt/__init__.py
  4. 19 7
      tests/test_cli.py

+ 2 - 0
CHANGELOG.md

@@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 ### Added
+- `--fetch-device-info` can alternatively be enabled by assigning a non-empty value
+  to the environment variable `FETCH_DEVICE_INFO`
 - battery level of button automators will additionally be reported on topic
   `homeassistant/switch/switchbot/MAC_ADDRESS/battery-percentage`
   (old topic kept for downward compatibility)

+ 1 - 0
README.md

@@ -156,6 +156,7 @@ services:
     - MQTT_PORT=1883
     #- MQTT_USERNAME=username
     #- MQTT_PASSWORD=password
+    #- FETCH_DEVICE_INFO=yes
     restart: unless-stopped
 ```
 

+ 8 - 2
switchbot_mqtt/__init__.py

@@ -22,6 +22,7 @@ import collections
 import enum
 import json
 import logging
+import os
 import pathlib
 import queue
 import re
@@ -549,7 +550,9 @@ def _main() -> None:
         f" {_CurtainMotor.get_mqtt_battery_percentage_topic(mac_address='MAC_ADDRESS')}"
         " after every command. Additionally report curtain motors' position on"
         f" topic {_CurtainMotor.get_mqtt_position_topic(mac_address='MAC_ADDRESS')}"
-        " after executing stop commands.",
+        " after executing stop commands."
+        " This option can also be enabled by assigning a non-empty value to the"
+        " environment variable FETCH_DEVICE_INFO.",
     )
     argparser.add_argument("--debug", action="store_true")
     args = argparser.parse_args()
@@ -582,5 +585,8 @@ def _main() -> None:
         mqtt_password=mqtt_password,
         retry_count=args.retry_count,
         device_passwords=device_passwords,
-        fetch_device_info=args.fetch_device_info,
+        fetch_device_info=args.fetch_device_info
+        # > In formal language theory, the empty string, [...], is the unique string of length zero.
+        # https://en.wikipedia.org/wiki/Empty_string
+        or bool(os.environ.get("FETCH_DEVICE_INFO")),
     )

+ 19 - 7
tests/test_cli.py

@@ -25,6 +25,7 @@ import pytest
 
 import switchbot_mqtt
 
+# pylint: disable=protected-access; tests
 # pylint: disable=too-many-arguments; these are tests, no API
 
 
@@ -93,7 +94,6 @@ def test__main(
     with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
         "sys.argv", argv
     ):
-        # pylint: disable=protected-access
         switchbot_mqtt._main()
     run_mock.assert_called_once_with(
         mqtt_host=expected_mqtt_host,
@@ -138,7 +138,6 @@ def test__main_mqtt_password_file(
             str(mqtt_password_path),
         ],
     ):
-        # pylint: disable=protected-access
         switchbot_mqtt._main()
     run_mock.assert_called_once_with(
         mqtt_host="localhost",
@@ -167,7 +166,6 @@ def test__main_mqtt_password_file_collision(capsys):
         ],
     ):
         with pytest.raises(SystemExit):
-            # pylint: disable=protected-access
             switchbot_mqtt._main()
     out, err = capsys.readouterr()
     assert not out
@@ -197,7 +195,6 @@ def test__main_device_password_file(tmpdir, device_passwords):
             str(device_passwords_path),
         ],
     ):
-        # pylint: disable=protected-access
         switchbot_mqtt._main()
     run_mock.assert_called_once_with(
         mqtt_host="localhost",
@@ -219,7 +216,6 @@ def test__main_fetch_device_info():
             "localhost",
         ],
     ):
-        # pylint: disable=protected-access
         switchbot_mqtt._main()
     default_kwargs = dict(
         mqtt_host="localhost",
@@ -234,7 +230,24 @@ def test__main_fetch_device_info():
         "sys.argv",
         ["", "--mqtt-host", "localhost", "--fetch-device-info"],
     ):
-        # pylint: disable=protected-access
+        switchbot_mqtt._main()
+    run_mock.assert_called_once_with(fetch_device_info=True, **default_kwargs)
+    with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
+        "sys.argv",
+        ["", "--mqtt-host", "localhost"],
+    ), unittest.mock.patch.dict("os.environ", {"FETCH_DEVICE_INFO": "21"}):
+        switchbot_mqtt._main()
+    run_mock.assert_called_once_with(fetch_device_info=True, **default_kwargs)
+    with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
+        "sys.argv",
+        ["", "--mqtt-host", "localhost"],
+    ), unittest.mock.patch.dict("os.environ", {"FETCH_DEVICE_INFO": ""}):
+        switchbot_mqtt._main()
+    run_mock.assert_called_once_with(fetch_device_info=False, **default_kwargs)
+    with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
+        "sys.argv",
+        ["", "--mqtt-host", "localhost"],
+    ), unittest.mock.patch.dict("os.environ", {"FETCH_DEVICE_INFO": " "}):
         switchbot_mqtt._main()
     run_mock.assert_called_once_with(fetch_device_info=True, **default_kwargs)
 
@@ -260,7 +273,6 @@ def test__main_log_config(
     ) as logging_basic_config_mock, unittest.mock.patch(
         "switchbot_mqtt._run"
     ):
-        # pylint: disable=protected-access
         switchbot_mqtt._main()
     logging_basic_config_mock.assert_called_once_with(
         level=root_log_level, format=log_format, datefmt="%Y-%m-%dT%H:%M:%S%z"