{"record":{"id":"b25a1719af74e9fb","repo":"home-assistant/core","slug":"invalid-mac-address-b25a17","errorCode":null,"errorMessage":"Invalid MAC address","messagePattern":"Invalid MAC address","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"homeassistant/components/broadlink/helpers.py","lineNumber":29,"sourceCode":"\ndef data_packet(value):\n    \"\"\"Decode a data packet given for a Broadlink remote.\"\"\"\n    value = cv.string(value)\n    extra = len(value) % 4\n    if extra > 0:\n        value = value + (\"=\" * (4 - extra))\n    return b64decode(value)\n\n\ndef mac_address(mac):\n    \"\"\"Validate and convert a MAC address to bytes.\"\"\"\n    mac = cv.string(mac)\n    if len(mac) == 17:\n        mac = \"\".join(mac[i : i + 2] for i in range(0, 17, 3))\n    elif len(mac) == 14:\n        mac = \"\".join(mac[i : i + 4] for i in range(0, 14, 5))\n    elif len(mac) != 12:\n        raise ValueError(\"Invalid MAC address\")\n    return bytes.fromhex(mac)\n\n\ndef format_mac(mac):\n    \"\"\"Format a MAC address.\"\"\"\n    return \":\".join([format(octet, \"02x\") for octet in mac])\n\n\ndef import_device(hass, host):\n    \"\"\"Create a config flow for a device.\"\"\"\n    configured_hosts = {\n        entry.data.get(CONF_HOST) for entry in hass.config_entries.async_entries(DOMAIN)\n    }\n\n    if host not in configured_hosts:\n        task = hass.config_entries.flow.async_init(\n            DOMAIN,\n            context={\"source\": config_entries.SOURCE_IMPORT},","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/home-assistant/core/blob/58a3fdb3ea0538617f0a07efcfba6294de64fd59/homeassistant/components/broadlink/helpers.py#L11-L47","documentation":"Plain ValueError('Invalid MAC address') from the mac_address helper: it accepts MACs of length 17 (colon/dash separated pairs), 14, or 12 hex chars, normalizes them, then calls bytes.fromhex. Anything else — wrong length or non-hex content — raises. It is used to parse device MACs (e.g. from config entries or discovery) before .hex() unique-id generation.","triggerScenarios":"mac_address() receives a string shorter/longer than 12/14/17 characters, or containing non-hex characters (e.g. 'ZZ:...', or a 16-char truncated MAC, or empty string). Callers include import_device / device setup paths that parse user-supplied or discovery-supplied hosts.","commonSituations":"Manual config with a typo'd MAC, discovery payload with an atypical separator pattern whose stripped length is not 12/14/17, copy-paste including whitespace, or a None/empty value passed by a broken caller.","solutions":["Re-enter the MAC exactly as printed on the device label, e.g. AA:BB:CC:DD:EE:FF (17 chars) or 12 bare hex digits.","Strip whitespace/newlines before passing the value.","Verify length: 12, 14, or 17 characters only; separators must be consistent so normalization yields valid hex.","If it comes from discovery data, capture the raw payload and open a core issue for the device model."],"exampleFix":"// before\nmac_bytes = mac_address(\"AA:BB:CC:DD:EE\")  # too short\n\n// after\nmac_bytes = mac_address(\"AA:BB:CC:DD:EE:FF\")","handlingStrategy":"validation","validationCode":"import re\n\ndef is_valid_broadlink_mac(mac: str) -> bool:\n    \"\"\"True for 12/14/17-char strings whose stripped form is pure hex.\"\"\"\n    if not isinstance(mac, str):\n        return False\n    stripped = re.sub(r\"[:.\\-\\s]\", \"\", mac)\n    return len(mac) in (12, 14, 17) and re.fullmatch(r\"[0-9a-fA-F]+\", stripped) is not None and len(stripped) == 12","typeGuard":"def is_mac_string(value: object) -> bool:\n    \"\"\"Narrow arbitrary config input to an acceptable MAC string.\"\"\"\n    return isinstance(value, str) and bool(re.fullmatch(r\"[0-9A-Fa-f:.\\-\\s]{12,17}\", value))","tryCatchPattern":"try:\n    mac_bytes = mac_address(user_input[CONF_MAC])\nexcept ValueError:\n    errors[CONF_MAC] = \"invalid_mac\"  # show field error in the form instead of crashing","preventionTips":["Copy MACs from the device label and strip whitespace before use.","Wrap mac_address() calls in form validation so users get field-level feedback.","Reject empty/None early at the UI boundary."],"tags":["home-assistant","broadlink","validation","user-input"],"backgroundTag":null,"analyzedSha":"58a3fdb3ea0538617f0a07efcfba6294de64fd59","analyzedAt":"2026-08-14T20:54:38.818Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}