Skip to content

Instantly share code, notes, and snippets.

@nbeirne
Created November 8, 2024 22:41
Show Gist options
  • Select an option

  • Save nbeirne/3de4ec791c3060e3dd53589ca2699e06 to your computer and use it in GitHub Desktop.

Select an option

Save nbeirne/3de4ec791c3060e3dd53589ca2699e06 to your computer and use it in GitHub Desktop.
Homeassisant TLV fix
#!/usr/bin/env python3
import binascii
import json
import sys
# This is a temporary fix for this github issue: https://github.com/home-assistant/core/issues/127601
# Instructions:
# 1. MAKE A BACKUP OF YOUR HOMEASSISTANT. This changes system files.
# 2. Share your credentials using the ios app. You should make the apple thread credentials your preferred network.
# 3. Log in via ssh, and go into /config/.storage/
# 4. Open the thread.datasets file, find your dataset and the "tlv" value. Copy the tlv value somewhere safe.
# 5. Run this script like so: `./fix_tlv.py [tlv]`.
# 6. Delete The thread.datasets file and reboot homeassistant.
# 7. Go into the thread integration and hit "Add Dataset from TLV"
# 8. Paste in the new TLV value.
def get_tlv_data(tlv_str):
DATA = {}
tlv_data = binascii.a2b_hex(tlv_str)
pos = 0
while pos < len(tlv_data):
tag = tlv_data[pos]
pos += 1
_len = tlv_data[pos]
pos += 1
val = tlv_data[pos:pos+_len]
pos += _len
#DATA[tag] = MeshcopTLVItem(tag, data=bytes.fromhex("%s" % val.hex()))
DATA[tag] = "%s" % val.hex()
return DATA
def fix_tlv_data(data):
data.pop(74) # this is the bad key. OT_MESHCOP_TLV_WAKEUP_CHANNEL
def create_tlv_str(data):
import python_otbr_api
from python_otbr_api import PENDING_DATASET_DELAY_TIMER, tlv_parser
from python_otbr_api.pskc import compute_pskc
from python_otbr_api.tlv_parser import MeshcopTLVType, MeshcopTLVItem
tlv_dict = {
key: MeshcopTLVItem(key,data=bytes.fromhex(value)) for key,value in data.items()
}
tlv = tlv_parser.encode_tlv(tlv_dict)
return tlv
if __name__ == '__main__':
bad_tlv = sys.argv[1]
data = get_tlv_data(bad_tlv)
fix_tlv_data(data)
fixed_str = create_tlv_str(data)
print(fixed_str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment