From b2114e8cfcc0f42cfa13844b67a1eb1603bf8445 Mon Sep 17 00:00:00 2001 From: Alex Lobascio Date: Mon, 3 Jun 2024 00:51:08 -0700 Subject: [PATCH 1/2] add support of boolean settings values that cast to lowercase strings matching true/t/yes/enable/on or the number 1 will all be true, all other values will be treated as false --- mpf/config_spec.yaml | 2 +- mpf/core/utility_functions.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/mpf/config_spec.yaml b/mpf/config_spec.yaml index 759100af8..55a6b87fc 100644 --- a/mpf/config_spec.yaml +++ b/mpf/config_spec.yaml @@ -1573,7 +1573,7 @@ settings: label: single|str| sort: single|int| values: dict|str:str| - key_type: single|enum(str,float,int)|str + key_type: single|enum(str,float,int,bool)|str default: single|str| machine_var: single|str|None settingType: single|str|standard diff --git a/mpf/core/utility_functions.py b/mpf/core/utility_functions.py index 691216d93..ecabcffc9 100644 --- a/mpf/core/utility_functions.py +++ b/mpf/core/utility_functions.py @@ -57,6 +57,8 @@ def convert_to_type(value, type_name): return float(value) if type_name == "str": return str(value) + if type_name == "bool": + return str(value).lower() in ['true', 't', 'yes', 'enable', 'on', '1'] raise AssertionError("Unknown type {}".format(type_name)) @@ -847,4 +849,4 @@ def check_bit(hex_string, bit): bool: True if the bit is set, False otherwise """ num = int(hex_string, 16) - return bool(num & (1 << bit)) \ No newline at end of file + return bool(num & (1 << bit)) From 81681f6e8659bd829726f741ec4e615759bf9058 Mon Sep 17 00:00:00 2001 From: Alex Lobascio Date: Mon, 17 Feb 2025 23:15:15 -0800 Subject: [PATCH 2/2] add boolean setting test and backfill float setting test case --- .../machine_files/settings/config/config.yaml | 27 +++++++++++++++++++ mpf/tests/test_Settings.py | 16 +++++++++++ 2 files changed, 43 insertions(+) diff --git a/mpf/tests/machine_files/settings/config/config.yaml b/mpf/tests/machine_files/settings/config/config.yaml index 4f86ed11a..5d268e1b9 100644 --- a/mpf/tests/machine_files/settings/config/config.yaml +++ b/mpf/tests/machine_files/settings/config/config.yaml @@ -19,3 +19,30 @@ settings: zero: "Zero" one: "One" two: "Two" + custom_setting_float: + label: "Float Setting" + key_type: float + default: 0.1 + sort: 3 + values: + 0: "Zero" + 0.1: "Point One" + 1.0: "One" + 2.5: "Two Point Five" + custom_setting_bool_t: + label: "Boolean Setting Default True" + key_type: bool + default: true + sort: 4 + values: + true: "yes" + false: "off" + + custom_setting_bool_f: + label: "Boolean Setting Default False" + key_type: bool + default: false + sort: 5 + values: + true: "on" + false: "no" diff --git a/mpf/tests/test_Settings.py b/mpf/tests/test_Settings.py index d2fed7487..2507faaaa 100644 --- a/mpf/tests/test_Settings.py +++ b/mpf/tests/test_Settings.py @@ -24,3 +24,19 @@ def test_settings_values(self): self.assertEqual("one", self.machine.settings.custom_setting_str) self.machine.settings.set_setting_value("custom_setting_str", "zero") self.assertEqual("zero", self.machine.settings.custom_setting_str) + + self.assertEqual(0.1, self.machine.settings.custom_setting_float) + self.machine.settings.set_setting_value("custom_setting_float", 2.5) + self.assertEqual(2.5, self.machine.settings.custom_setting_float) + self.machine.settings.set_setting_value("custom_setting_float", 0) + self.assertEqual(0, self.machine.settings.custom_setting_float) + + self.assertEqual(True, self.machine.settings.custom_setting_bool_t) + self.machine.settings.set_setting_value("custom_setting_bool_t", False) + self.assertEqual(False, self.machine.settings.custom_setting_bool_t) + self.machine.settings.set_setting_value("custom_setting_bool_t", True) + self.assertEqual(True, self.machine.settings.custom_setting_bool_t) + + self.assertEqual(False, self.machine.settings.custom_setting_bool_f) + self.machine.settings.set_setting_value("custom_setting_bool_f", True) + self.assertEqual(True, self.machine.settings.custom_setting_bool_f)