Refactor contact addition logic in MessageHandler for improved compatibility

- Enhanced the `add_contact` method to handle variations in parameter requirements across device bindings, ensuring robust functionality.
- Avoided passing unnecessary `contact_data` fields that could lead to issues with unsigned integer handling.
- Implemented a fallback mechanism for `add_contact` to accommodate signature mismatches, improving error handling.
This commit is contained in:
agessaman
2026-03-19 20:24:06 -07:00
parent e5d0804069
commit 469d818e83
+14 -6
View File
@@ -2943,12 +2943,20 @@ class MessageHandler:
# Add companion to device contact list
try:
# Only pass the fields required by `add_contact` (name + optional public_key).
# `contact_data` may include RSSI/SNR values (often negative in dBm) which some
# device APIs treat as unsigned integers.
if public_key:
result = await self.bot.meshcore.commands.add_contact(contact_name, public_key)
else:
# Avoid passing `contact_data` wholesale: it can include RSSI/SNR values
# (often negative in dBm). Some device bindings cast those to unsigned
# ints and fail.
#
# The meshcore binding for `add_contact` is not consistent across versions,
# so we try (name, public_key) first, but fall back to (name) on
# signature mismatches.
try:
if public_key:
result = await self.bot.meshcore.commands.add_contact(contact_name, public_key)
else:
result = await self.bot.meshcore.commands.add_contact(contact_name)
except TypeError:
# Binding expects a single positional arg: `add_contact(name)`
result = await self.bot.meshcore.commands.add_contact(contact_name)
if hasattr(result, 'type') and result.type.name == 'OK':
self.logger.info(f"✅ Companion {contact_name} added to device contacts")