File: utils.py

package info (click to toggle)
pyswitchbot 0.72.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 876 kB
  • sloc: python: 12,717; makefile: 2
file content (24 lines) | stat: -rw-r--r-- 706 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""Utility functions for switchbot."""

from functools import lru_cache


@lru_cache(maxsize=512)
def format_mac_upper(mac: str) -> str:
    """Format the mac address string to uppercase with colons."""
    to_test = mac

    if len(to_test) == 17 and to_test.count(":") == 5:
        return to_test.upper()

    if len(to_test) == 17 and to_test.count("-") == 5:
        to_test = to_test.replace("-", "")
    elif len(to_test) == 14 and to_test.count(".") == 2:
        to_test = to_test.replace(".", "")

    if len(to_test) == 12:
        # no : included
        return ":".join(to_test.upper()[i : i + 2] for i in range(0, 12, 2))

    # Not sure how formatted, return original
    return mac.upper()