Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions appdaemon/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
from pytz import BaseTzInfo

from appdaemon.parse import parse_datetime
from appdaemon.version import __version__ # noqa: F401
from appdaemon.version import __version_comments__ # noqa: F401
from appdaemon.version import (
__version__, # noqa: F401
__version_comments__, # noqa: F401
)

from . import exceptions as ade
from .parse import parse_timedelta
Expand Down Expand Up @@ -1082,12 +1084,14 @@ def clean_kwargs(**kwargs: Any) -> Generator[tuple[str, Any]]:
- Mapping values (like dicts) are converted to dicts of cleaned key-value pairs
"""

def _clean_value(val: bool | datetime | Any) -> str:
def _clean_value(val: bool | datetime | Any) -> str | int | float | bool:
match val:
case bool():
return val
case str() | int() | float() | bool():
return val
case datetime():
return val.isoformat()
case bool():
return str(val).lower()
case _:
return str(val)

Expand All @@ -1097,7 +1101,7 @@ def _clean_value(val: bool | datetime | Any) -> str:
continue
case str():
# This case needs to be before the Iterable case because strings are iterable
yield key, _clean_value(val)
yield key, val
case Mapping():
# This case needs to be before the Iterable case because Mappings like dicts are iterable
yield key, dict(clean_kwargs(**val))
Expand All @@ -1111,9 +1115,13 @@ def clean_http_kwargs(**kwargs: Any) -> Generator[tuple[str, Any]]:
"""Recursively cleans the kwarg dict to prepare it for use in HTTP requests."""
for key, val in clean_kwargs(**kwargs):
match val:
case "false" | None:
# Filter out values that are False or None
case None:
continue # filter None values
case False | "false":
# Filter out values that are False
continue
case True:
yield key, "true"
case _:
yield key, val

Expand Down
13 changes: 5 additions & 8 deletions tests/unit/test_kwarg_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,22 @@

def test_clean_kwargs():
cleaned = dict(clean_kwargs(**BASE))
for v in cleaned.values():
assert isinstance(v, str), f"Value {v} should be a string after cleaning"
assert isinstance(cleaned["f"], str)

assert cleaned["e"] == "false"
assert cleaned["d"] is True
assert cleaned["e"] is False
assert "g" not in cleaned

kwargs = deepcopy(BASE)

kwargs["nested"] = deepcopy(BASE)
kwargs["nested"]["extra"] = deepcopy(BASE)
cleaned = dict(clean_kwargs(**kwargs))
for v in cleaned["nested"]["extra"].values():
assert isinstance(v, str), f"Value {v} should be a string after cleaning"
assert isinstance(cleaned["nested"]["extra"]["f"], str)


def test_clean_http_kwargs():
cleaned = dict(clean_http_kwargs(**BASE))
for v in cleaned.values():
assert isinstance(v, str), f"Value {v} should be a string after cleaning"

assert isinstance(cleaned["f"], str)
assert "e" not in cleaned
assert "g" not in cleaned
Loading