From 06e32a100bf85a39bfc18c3f20abb3c6fa18bd1d Mon Sep 17 00:00:00 2001 From: puddly <32534428+puddly@users.noreply.github.com> Date: Tue, 30 Nov 2021 14:32:10 -0500 Subject: [PATCH] Allow unquoted `on`/`off` YAML booleans in LED mode config --- README.md | 5 ++--- tests/api/test_request.py | 2 +- tests/application/test_startup.py | 11 ++++++++--- zigpy_znp/config.py | 15 ++++++++++++++- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 17e5927a..b383dc94 100644 --- a/README.md +++ b/README.md @@ -60,9 +60,8 @@ zha: tx_power: # Only if your stick has a controllable LED (the CC2531) - # If set, must be one of: "off", "on", blink, flash, toggle - # Note: "off" and "on" must be quoted! - led_mode: "off" + # If set, must be one of: off, on, blink, flash, toggle + led_mode: off ### Internal configuration, there's no reason to touch these values diff --git a/tests/api/test_request.py b/tests/api/test_request.py index 4127c618..2a02b64d 100644 --- a/tests/api/test_request.py +++ b/tests/api/test_request.py @@ -262,7 +262,7 @@ async def test_znp_sreq_srsp(connected_znp, event_loop): # Each SREQ must have a corresponding SRSP, so this will fail with pytest.raises(asyncio.TimeoutError): - with async_timeout.timeout(0.5): + async with async_timeout.timeout(0.5): await znp.request(c.SYS.Ping.Req()) # This will work diff --git a/tests/application/test_startup.py b/tests/application/test_startup.py index bd0b0edd..24478160 100644 --- a/tests/application/test_startup.py +++ b/tests/application/test_startup.py @@ -205,11 +205,12 @@ async def test_tx_power(device, succeed, make_application): await app.shutdown() +@pytest.mark.parametrize("led_mode", ["off", False, "on", True]) @pytest.mark.parametrize("device", FORMED_DEVICES) -async def test_led_mode(device, make_application): +async def test_led_mode(device, led_mode, make_application): app, znp_server = make_application( server_cls=device, - client_config={conf.CONF_ZNP_CONFIG: {conf.CONF_LED_MODE: "off"}}, + client_config={conf.CONF_ZNP_CONFIG: {conf.CONF_LED_MODE: led_mode}}, ) # Z-Stack just does not respond to this command if HAL_LED is not enabled @@ -224,7 +225,11 @@ async def test_led_mode(device, make_application): await app.startup(auto_form=False) led_req = await set_led_mode - assert led_req.Mode == c.util.LEDMode.OFF + if led_mode in ("off", False): + assert led_req.Mode == c.util.LEDMode.OFF + else: + assert led_req.Mode == c.util.LEDMode.ON + assert led_req.LED == 0xFF await app.shutdown() diff --git a/zigpy_znp/config.py b/zigpy_znp/config.py index 3d432195..0ba472fe 100644 --- a/zigpy_znp/config.py +++ b/zigpy_znp/config.py @@ -62,6 +62,19 @@ def validator(config): return validator +def bool_to_upper_str(value: typing.Any) -> str: + """ + Converts the value into an uppercase string, including unquoted YAML booleans. + """ + + if value is True: + return "ON" + elif value is False: + return "OFF" + else: + return str(value).upper() + + CONF_ZNP_CONFIG = "znp_config" CONF_TX_POWER = "tx_power" CONF_LED_MODE = "led_mode" @@ -89,7 +102,7 @@ def validator(config): ): VolPositiveNumber, vol.Optional(CONF_SKIP_BOOTLOADER, default=True): cv_boolean, vol.Optional(CONF_LED_MODE, default=LEDMode.OFF): vol.Any( - None, EnumValue(LEDMode, lambda v: str(v).upper()) + None, EnumValue(LEDMode, transformer=bool_to_upper_str) ), vol.Optional(CONF_MAX_CONCURRENT_REQUESTS, default="auto"): vol.Any( "auto", VolPositiveNumber