host.nfc_tool.battery_data
Battery data model for NFC tags.
Data is stored on tags as a single NDEF Text record with an INI-style section header ('bat:') followed by key=value lines.
1"""Battery data model for NFC tags. 2 3Data is stored on tags as a single NDEF Text record with an INI-style 4section header ('bat:') followed by key=value lines. 5""" 6 7from dataclasses import dataclass 8 9 10@dataclass 11class BatteryData: 12 """Parsed battery information from an NFC tag.""" 13 sn: str = '' 14 year: str = '' 15 note: str = '' 16 17 @classmethod 18 def from_text(cls, text): 19 """Parse bat: section with key=value lines. 20 21 Tolerant of unknown keys and missing bat: prefix. 22 """ 23 if 'bat:' in text: 24 text = text.split('bat:', 1)[1] 25 fields = {} 26 for line in text.strip().split('\n'): 27 if '=' in line: 28 k, v = line.split('=', 1) 29 fields[k.strip()] = v.strip() 30 return cls( 31 sn=fields.get('sn', ''), 32 year=fields.get('year', ''), 33 note=fields.get('note', ''), 34 ) 35 36 def to_text(self): 37 """Serialize to bat: section format for NDEF Text record.""" 38 lines = [f"bat:", f"sn={self.sn}", f"year={self.year}"] 39 if self.note: 40 lines.append(f"note={self.note}") 41 return '\n'.join(lines) 42 43 def is_valid(self): 44 """Battery data is valid if it has a serial number.""" 45 return bool(self.sn.strip()) 46 47 def is_battery_tag(self): 48 """Check if text was recognized as battery data.""" 49 return bool(self.sn)
@dataclass
class
BatteryData:
11@dataclass 12class BatteryData: 13 """Parsed battery information from an NFC tag.""" 14 sn: str = '' 15 year: str = '' 16 note: str = '' 17 18 @classmethod 19 def from_text(cls, text): 20 """Parse bat: section with key=value lines. 21 22 Tolerant of unknown keys and missing bat: prefix. 23 """ 24 if 'bat:' in text: 25 text = text.split('bat:', 1)[1] 26 fields = {} 27 for line in text.strip().split('\n'): 28 if '=' in line: 29 k, v = line.split('=', 1) 30 fields[k.strip()] = v.strip() 31 return cls( 32 sn=fields.get('sn', ''), 33 year=fields.get('year', ''), 34 note=fields.get('note', ''), 35 ) 36 37 def to_text(self): 38 """Serialize to bat: section format for NDEF Text record.""" 39 lines = [f"bat:", f"sn={self.sn}", f"year={self.year}"] 40 if self.note: 41 lines.append(f"note={self.note}") 42 return '\n'.join(lines) 43 44 def is_valid(self): 45 """Battery data is valid if it has a serial number.""" 46 return bool(self.sn.strip()) 47 48 def is_battery_tag(self): 49 """Check if text was recognized as battery data.""" 50 return bool(self.sn)
Parsed battery information from an NFC tag.
@classmethod
def
from_text(cls, text):
18 @classmethod 19 def from_text(cls, text): 20 """Parse bat: section with key=value lines. 21 22 Tolerant of unknown keys and missing bat: prefix. 23 """ 24 if 'bat:' in text: 25 text = text.split('bat:', 1)[1] 26 fields = {} 27 for line in text.strip().split('\n'): 28 if '=' in line: 29 k, v = line.split('=', 1) 30 fields[k.strip()] = v.strip() 31 return cls( 32 sn=fields.get('sn', ''), 33 year=fields.get('year', ''), 34 note=fields.get('note', ''), 35 )
Parse bat: section with key=value lines.
Tolerant of unknown keys and missing bat: prefix.
def
to_text(self):
37 def to_text(self): 38 """Serialize to bat: section format for NDEF Text record.""" 39 lines = [f"bat:", f"sn={self.sn}", f"year={self.year}"] 40 if self.note: 41 lines.append(f"note={self.note}") 42 return '\n'.join(lines)
Serialize to bat: section format for NDEF Text record.