Guest User

Untitled

a guest
Mar 6th, 2025
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. import asyncio
  2. from switchbot.discovery import GetSwitchbotDevices
  3. from switchbot import get_device
  4. import binascii
  5. import ast
  6. import re
  7.  
  8. # Import the specific parser function
  9. from switchbot.adv_parsers.meter import process_wosensorth
  10.  
  11. async def main():
  12. print("Starting discovery...")
  13.  
  14. discovery = GetSwitchbotDevices()
  15. devices = await discovery.discover()
  16.  
  17. print(f"Found {len(devices)} devices")
  18.  
  19. for device_addr in devices:
  20. print(f"\n=== Device: {device_addr} ===")
  21.  
  22. try:
  23. device_obj = await get_device(device_addr)
  24.  
  25. # Extract and prepare data for parsing
  26. if hasattr(device_obj, 'details'):
  27. details = device_obj.details if not callable(device_obj.details) else await device_obj.details()
  28.  
  29. manufacturer_data = None
  30. service_data = None
  31.  
  32. # Get Manufacturer Data
  33. if 'props' in details and 'ManufacturerData' in details['props']:
  34. mfg_data = details['props']['ManufacturerData']
  35. for key, value in mfg_data.items():
  36. print(f"Company ID: {key}")
  37. try:
  38. if isinstance(value, bytes) or isinstance(value, bytearray):
  39. manufacturer_data = value
  40. elif "bytearray(b'" in value:
  41. content = re.search(r"bytearray\(b'(.+?)'\)", value)
  42. if content:
  43. manufacturer_data = content.group(1).encode().decode('unicode_escape').encode('latin1')
  44. else:
  45. manufacturer_data = ast.literal_eval(value)
  46. except Exception as e:
  47. print(f"Error decoding manufacturer data: {e}")
  48.  
  49. # Now use the parser function to decode the data
  50. if manufacturer_data:
  51. try:
  52. # Call the parser function with the correct arguments
  53. # The function expects separate adv_data and mfr_data arguments
  54. adv_data = {} # Empty dict for adv_data
  55. result = process_wosensorth(adv_data, manufacturer_data)
  56.  
  57. print("\nDecoded WoSensorTH Data:")
  58. print(f"Temperature: {result.get('temperature')}°C")
  59. print(f"Humidity: {result.get('humidity')}%")
  60. print(f"Battery: {result.get('battery')}%")
  61. print(f"All data: {result}")
  62. except Exception as e:
  63. print(f"Error parsing data: {e}")
  64. import traceback
  65. traceback.print_exc()
  66.  
  67. except Exception as e:
  68. print(f"Error processing device: {e}")
  69.  
  70. if __name__ == "__main__":
  71. asyncio.run(main())
Advertisement
Add Comment
Please, Sign In to add comment