1
0

utils.py 706 B

123456789101112131415161718192021222324
  1. """Utility functions for switchbot."""
  2. from functools import lru_cache
  3. @lru_cache(maxsize=512)
  4. def format_mac_upper(mac: str) -> str:
  5. """Format the mac address string to uppercase with colons."""
  6. to_test = mac
  7. if len(to_test) == 17 and to_test.count(":") == 5:
  8. return to_test.upper()
  9. if len(to_test) == 17 and to_test.count("-") == 5:
  10. to_test = to_test.replace("-", "")
  11. elif len(to_test) == 14 and to_test.count(".") == 2:
  12. to_test = to_test.replace(".", "")
  13. if len(to_test) == 12:
  14. # no : included
  15. return ":".join(to_test.upper()[i : i + 2] for i in range(0, 12, 2))
  16. # Not sure how formatted, return original
  17. return mac.upper()