{"record":{"id":"54f6c71942b4b679","repo":"home-assistant/core","slug":"invalid-code-code-r","errorCode":null,"errorMessage":"Invalid code: {code!r}","messagePattern":"Invalid code: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"homeassistant/components/broadlink/remote.py","lineNumber":165,"sourceCode":"            else:\n                if device is None:\n                    raise ValueError(\"You need to specify a device\")\n\n                try:\n                    codes = self._codes[device][cmd]\n                except KeyError as err:\n                    raise ValueError(f\"Command not found: {cmd!r}\") from err\n\n                if isinstance(codes, list):\n                    codes = codes[:]\n                else:\n                    codes = [codes]\n\n            for idx, code in enumerate(codes):\n                try:\n                    codes[idx] = data_packet(code)\n                except ValueError as err:\n                    raise ValueError(f\"Invalid code: {code!r}\") from err\n\n            code_list.append(codes)\n        return code_list\n\n    @callback\n    def _get_codes(self):\n        \"\"\"Return a dictionary of codes.\"\"\"\n        return self._codes\n\n    @callback\n    def _get_flags(self):\n        \"\"\"Return a dictionary of toggle flags.\n\n        A toggle flag indicates whether the remote should send an\n        alternative code.\n        \"\"\"\n        return self._flags\n","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/home-assistant/core/blob/58a3fdb3ea0538617f0a07efcfba6294de64fd59/homeassistant/components/broadlink/remote.py#L147-L183","documentation":"ValueError(f\"Invalid code: {code!r}\") from _extract_codes: the stored or supplied code string failed data_packet() decoding, which base64-decodes and validates the Broadlink packet (padding fix-up included in helpers). It means the code exists but its content is not a valid base64 Broadlink data packet.","triggerScenarios":"A learned code string got corrupted (truncated, whitespace/newlines, bad base64 chars), or a hand-copied 'b64:'-style code from the internet is malformed. codes[idx] = data_packet(code) raises ValueError and it is re-raised with the offending code shown.","commonSituations":"Copy-paste of codes from forums with smart quotes/line wraps, YAML mangling of long base64 strings, partial writes to the storage file, editing .storage by hand.","solutions":["Re-learn the command so a fresh valid code is stored.","If supplying b64: codes manually, validate the base64 payload (length % 4 padding, no whitespace) before sending.","Keep long base64 strings on one line in YAML or use proper quoting/folding.","Avoid hand-editing the broadlink codes storage; use learn/delete services."],"exampleFix":"# before (broken paste with newline)\ncommand: \"b64:JgBQAAABKU5RExERExERExE\nKU5Q...\"\n\n# after (single line, valid base64)\ncommand: \"b64:JgBQAAABKU5RExERExERExEKU5Q==\"","handlingStrategy":"validation","validationCode":"import base64, binascii\n\ndef is_valid_b64_packet(code: str) -> bool:\n    \"\"\"True if the string is clean base64 that decodes to a Broadlink packet.\"\"\"\n    try:\n        base64.b64decode(code, validate=True)\n        return True\n    except (binascii.Error, ValueError):\n        return False","typeGuard":"def is_decodable_code(value: object) -> bool:\n    \"\"\"Narrow to non-empty, whitespace-free base64 strings.\"\"\"\n    return (\n        isinstance(value, str)\n        and value != \"\"\n        and not any(ch.isspace() for ch in value)\n    )","tryCatchPattern":"try:\n    codes = remote._extract_codes(commands, device)\nexcept ValueError as err:\n    if \"Invalid code\" in str(err):\n        # drop the corrupt entry and re-learn it later\n        commands = [c for c in commands if c not in str(err)]\n    else:\n        raise","preventionTips":["Never hand-edit learned codes; re-learn instead.","When pasting b64 codes, keep them on a single line and strip whitespace before saving.","Validate base64 in scripts that import codes from external sources."],"tags":["home-assistant","broadlink","remote","validation"],"backgroundTag":null,"analyzedSha":"58a3fdb3ea0538617f0a07efcfba6294de64fd59","analyzedAt":"2026-08-14T20:54:38.818Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}