diff --git a/lib/logitech_receiver/settings.py b/lib/logitech_receiver/settings.py index c9b816d8..d717935d 100644 --- a/lib/logitech_receiver/settings.py +++ b/lib/logitech_receiver/settings.py @@ -13,6 +13,7 @@ ## You should have received a copy of the GNU General Public License along ## with this program; if not, write to the Free Software Foundation, Inc., ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +from __future__ import annotations import logging import math @@ -42,13 +43,15 @@ KIND = NamedInts( ) -def bool_or_toggle(current, new): +def bool_or_toggle(current: bool | str, new: bool | str) -> bool: if isinstance(new, bool): return new + try: return bool(int(new)) except (TypeError, ValueError): new = str(new).lower() + if new in ("true", "yes", "on", "t", "y"): return True if new in ("false", "no", "off", "f", "n"): diff --git a/tests/logitech_receiver/test_settings.py b/tests/logitech_receiver/test_settings.py new file mode 100644 index 00000000..5d50a3a4 --- /dev/null +++ b/tests/logitech_receiver/test_settings.py @@ -0,0 +1,25 @@ +import pytest + +from logitech_receiver import settings + + +@pytest.mark.parametrize( + "current, new, expected", + [ + (False, "toggle", True), + (True, "~", False), + ("don't care", True, True), + ("don't care", "true", True), + ("don't care", "false", False), + ("don't care", "no", False), + ("don't care", "off", False), + ("don't care", "True", True), + ("don't care", "yes", True), + ("don't care", "on", True), + ("anything", "anything", None), + ], +) +def test_bool_or_toggle(current, new, expected): + result = settings.bool_or_toggle(current=current, new=new) + + assert result == expected