From 469d818e83b60bbc0120231dc262ee2f5454bf85 Mon Sep 17 00:00:00 2001 From: agessaman Date: Thu, 19 Mar 2026 20:24:06 -0700 Subject: [PATCH] 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. --- modules/message_handler.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/modules/message_handler.py b/modules/message_handler.py index 0774bd9..6980619 100644 --- a/modules/message_handler.py +++ b/modules/message_handler.py @@ -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")