util.py 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. """Small helpers shared between the config flow and entry setup.
  2. Deliberately free of Home Assistant imports so the logic can be unit-tested on
  3. its own.
  4. """
  5. from __future__ import annotations
  6. from typing import Any
  7. from .const import WHITESPACE_SENSITIVE_FIELDS
  8. def normalise_config_fields(data: dict[str, Any]) -> dict[str, Any]:
  9. """Return a copy of *data* with whitespace trimmed from identifier fields.
  10. The Station ID, Device ID, account name, IOT token and time zone are all
  11. pasted or typed by hand. A trailing space picked up while copying an ID out
  12. of the browser's Network tab makes the upstream API reject the value, and the
  13. user only sees "cannot connect" with no hint as to why.
  14. Which fields are trimmed is defined once in WHITESPACE_SENSITIVE_FIELDS so
  15. the config-flow path and the entry-setup path cannot drift apart. The
  16. password is never trimmed — whitespace there may be intentional.
  17. Non-string values are passed through untouched, so this is safe to call on
  18. a mapping that also carries token-expiry timestamps and similar.
  19. """
  20. cleaned = dict(data)
  21. for key in WHITESPACE_SENSITIVE_FIELDS:
  22. value = cleaned.get(key)
  23. if isinstance(value, str):
  24. cleaned[key] = value.strip()
  25. return cleaned