# Re-enable rtl_433_ESP decoders that are disabled by default in the library. # # Newer rtl_433_ESP releases mark some decoders with `.disabled = 1` (e.g. # `esa_energy` since v0.6.x) and there is no build flag to turn them back on. # This script patches the decoder definitions inside the downloaded library in # .pio/libdeps// before compilation. # # Usage (in the environment section): # extra_scripts = # ${env.extra_scripts} # scripts/enable_rtl433_decoders.py # custom_rtl_433_enable_decoders = esa_energy # # Must run as a normal (post) script: lib_deps are installed while the platform # builder script runs, so a "pre:" script would not find the library on a clean # build. Sources are only compiled after all scripts have been read, and SCons # recompiles patched files because their content changed. import glob import os import re Import("env") def enable_decoders(libdeps_dir, decoders): """Patch `.disabled = N` to `.disabled = 0` for the given r_device names. Returns a dict decoder -> status ("enabled", "already enabled", "not found"). """ device_files = glob.glob( os.path.join(libdeps_dir, "**", "src", "rtl_433", "devices", "*.c"), recursive=True, ) result = {name: "not found" for name in decoders} for path in device_files: with open(path, "r", encoding="utf-8", newline="") as f: content = f.read() changed = False for name in decoders: block_re = re.compile( r"(r_device\s+const\s+" + re.escape(name) + r"\s*=\s*\{)(.*?)(\};)", re.DOTALL, ) match = block_re.search(content) if not match: continue body = match.group(2) new_body, count = re.subn( r"(\.disabled\s*=\s*)[1-9]\d*", r"\g<1>0", body ) if count: content = content[: match.start(2)] + new_body + content[match.end(2):] changed = True result[name] = "enabled" else: result[name] = "already enabled" if changed: with open(path, "w", encoding="utf-8", newline="") as f: f.write(content) return result decoders = env.GetProjectOption("custom_rtl_433_enable_decoders", "").split() if decoders: libdeps_dir = os.path.join(env.subst("$PROJECT_LIBDEPS_DIR"), env.subst("$PIOENV")) status = enable_decoders(libdeps_dir, decoders) for name, state in status.items(): print("rtl_433_ESP decoder %s: %s" % (name, state)) missing = [name for name, state in status.items() if state == "not found"] if missing: print("Error: rtl_433_ESP decoder(s) not found in %s: %s" % (libdeps_dir, ", ".join(missing))) env.Exit(1)