Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- from switchbot.discovery import GetSwitchbotDevices
- from switchbot import get_device
- import binascii
- import ast
- import re
- # Import the specific parser function
- from switchbot.adv_parsers.meter import process_wosensorth
- async def main():
- print("Starting discovery...")
- discovery = GetSwitchbotDevices()
- devices = await discovery.discover()
- print(f"Found {len(devices)} devices")
- for device_addr in devices:
- print(f"\n=== Device: {device_addr} ===")
- try:
- device_obj = await get_device(device_addr)
- # Extract and prepare data for parsing
- if hasattr(device_obj, 'details'):
- details = device_obj.details if not callable(device_obj.details) else await device_obj.details()
- manufacturer_data = None
- service_data = None
- # Get Manufacturer Data
- if 'props' in details and 'ManufacturerData' in details['props']:
- mfg_data = details['props']['ManufacturerData']
- for key, value in mfg_data.items():
- print(f"Company ID: {key}")
- try:
- if isinstance(value, bytes) or isinstance(value, bytearray):
- manufacturer_data = value
- elif "bytearray(b'" in value:
- content = re.search(r"bytearray\(b'(.+?)'\)", value)
- if content:
- manufacturer_data = content.group(1).encode().decode('unicode_escape').encode('latin1')
- else:
- manufacturer_data = ast.literal_eval(value)
- except Exception as e:
- print(f"Error decoding manufacturer data: {e}")
- # Now use the parser function to decode the data
- if manufacturer_data:
- try:
- # Call the parser function with the correct arguments
- # The function expects separate adv_data and mfr_data arguments
- adv_data = {} # Empty dict for adv_data
- result = process_wosensorth(adv_data, manufacturer_data)
- print("\nDecoded WoSensorTH Data:")
- print(f"Temperature: {result.get('temperature')}°C")
- print(f"Humidity: {result.get('humidity')}%")
- print(f"Battery: {result.get('battery')}%")
- print(f"All data: {result}")
- except Exception as e:
- print(f"Error parsing data: {e}")
- import traceback
- traceback.print_exc()
- except Exception as e:
- print(f"Error processing device: {e}")
- if __name__ == "__main__":
- asyncio.run(main())
Advertisement
Add Comment
Please, Sign In to add comment