Skip to content

Commit

Permalink
Allow unquoted on/off YAML booleans in LED mode config
Browse files Browse the repository at this point in the history
  • Loading branch information
puddly committed Nov 30, 2021
1 parent 41931c4 commit 06e32a1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/api/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions tests/application/test_startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
15 changes: 14 additions & 1 deletion zigpy_znp/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 06e32a1

Please sign in to comment.