From fcd0af1addc8724fbc9eda6591d7d5a32d7461d6 Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Thu, 18 Jan 2024 08:49:25 -0700 Subject: [PATCH] Make boolean config validator more efficient (#855) --- ecowitt2mqtt/config.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ecowitt2mqtt/config.py b/ecowitt2mqtt/config.py index d839dfed..ee0d9894 100644 --- a/ecowitt2mqtt/config.py +++ b/ecowitt2mqtt/config.py @@ -51,6 +51,9 @@ CONF_HASS_DISCOVERY, ) +TRUTHY_VALUES = {"1", "true", "yes", "on", "enable"} +FALSEY_VALUES = {"0", "false", "no", "off", "disable"} + class ConfigError(EcowittError): """Define an error related to bad configuration.""" @@ -74,9 +77,9 @@ def validate_boolean(value: bool | str | Number) -> bool: return value if isinstance(value, str): value = value.lower().strip() - if value in ("1", "true", "yes", "on", "enable"): + if value in TRUTHY_VALUES: return True - if value in ("0", "false", "no", "off", "disable"): + if value in FALSEY_VALUES: return False elif isinstance(value, Number): # type ignore: https://github.com/python/mypy/issues/3186