314ma

SmartThings data

Apr 4th, 2020 (edited)
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.47 KB | None | 0 0
  1. # DEPENDENCIES: pip3 install aiohttp pysmartthings asyncio
  2. import aiohttp
  3. import pysmartthings
  4. import asyncio
  5. import requests
  6.  
  7. # INSTRUCTION: https://github.com/PiotrMachowski/Home-Assistant-custom-components-SmartThings-Soundbar#getting-api-key-and-device-id
  8. API_KEY = "API_KEY"
  9. DEVICE_ID = "DEVICE_ID"
  10.  
  11.  
  12. async def run():
  13.     async with aiohttp.ClientSession() as session:
  14.         api = pysmartthings.SmartThings(session, API_KEY)
  15.         device = (await api.devices(device_ids=[DEVICE_ID]))[0]
  16.         await device.status.refresh()
  17.         print("CAPABILITIES")
  18.         print(device.capabilities)
  19.         print("\nATTRIBUTES")
  20.         print(str(device.status.attributes).replace(DEVICE_ID, "DEVICE_ID"))
  21.         f1 = open("pysmartthings.txt", "w")
  22.         f1.write("CAPABILITIES\n")
  23.         f1.write(str(device.capabilities))
  24.         f1.write("\nATTRIBUTES\n")
  25.         f1.write(str(device.status.attributes).replace(DEVICE_ID, "DEVICE_ID"))
  26.         f1.close()
  27.  
  28. async def waiter():
  29.     await run()
  30.  
  31.  
  32. loop = asyncio.get_event_loop()
  33. loop.run_until_complete(waiter())
  34.  
  35. API_DEVICE_STATUS = "https://api.smartthings.com/v1/devices/" + DEVICE_ID + "/states"
  36. REQUEST_HEADERS = {"Authorization": "Bearer " + API_KEY}
  37. resp = requests.get(API_DEVICE_STATUS, headers=REQUEST_HEADERS)
  38.  
  39. print("\nSTATUS")
  40. print(str(resp.json()).replace(DEVICE_ID, "DEVICE_ID"))
  41. f2 = open("api.txt", "w")
  42. f2.write("STATUS\n")
  43. f2.write(str(resp.json()).replace(DEVICE_ID, "DEVICE_ID"))
  44. f2.close()
Add Comment
Please, Sign In to add comment