diff --git a/Changelog b/Changelog index 4f29621..e757bf5 100644 --- a/Changelog +++ b/Changelog @@ -4,6 +4,7 @@ Version 0.7.0 unreleased * Move configuration into pyproject.toml for pytest, mypy & coverage. * Upgrade to gha-shared-workflows@v8 for Poetry v2 support. * Migrate from pendulum to arrow for dates (interface change). + * Fix bug in serializing BooleanSetting to and from YAML. * Update all dependencies and outdated constraints. Version 0.6.9 02 Jan 2025 diff --git a/src/smartapp/converter.py b/src/smartapp/converter.py index 5f03e95..51cfcdd 100644 --- a/src/smartapp/converter.py +++ b/src/smartapp/converter.py @@ -6,6 +6,7 @@ Converter to serialize and deserialize lifecycle objects to various formats. """ import json +from enum import Enum from typing import Any, Dict, Type, TypeVar import yaml @@ -15,6 +16,7 @@ from attrs import fields, has from cattrs import GenConverter from cattrs.gen import make_dict_structure_fn, make_dict_unstructure_fn, override +from yaml import SafeDumper from .interface import ( CONFIG_SETTING_BY_TYPE, @@ -41,6 +43,10 @@ T = TypeVar("T") # pylint: disable=invalid-name: +# Configure SafeDumper to handle Enum values +yaml.add_multi_representer(Enum, lambda d, e: d.represent_str(e.value), Dumper=SafeDumper) + + def serialize_datetime(datetime: Arrow) -> str: """Serialize an Arrow datetime to a string.""" # Note that we always use the full millisecond timestamp here and always convert to UTC diff --git a/tests/test_converter.py b/tests/test_converter.py index bd95839..1b6901a 100644 --- a/tests/test_converter.py +++ b/tests/test_converter.py @@ -106,7 +106,7 @@ def test_boolean(self, settings): name="True or false?", description="Tap to set", required=True, - default_value="true", + default_value=BooleanValue.TRUE, ) validate_json_roundtrip(json, expected, BooleanSetting) validate_yaml_roundtrip(None, expected, BooleanSetting)