Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!usr/bin/python3
- #A small Module that makes connecting to SmartThings easier for us
- import aiohttp
- import asyncio
- import pysmartthings
- #gets the api session using TOKEN
- async def getApi(TOKEN,SESSION):
- api = None
- api = pysmartthings.SmartThings(SESSION,TOKEN)
- if not api == None:
- return api
- #gets All Locations
- async def getLocations(api):
- locations = await api.locations()
- return locations
- #gets all the Devices
- async def getDevices(api):
- devices = await api.devices()
- return devices
- #getsDeviceByName
- async def getDeviceByName(devices, name):
- for device in devices:
- dname = device.label
- if dname != name:
- continue
- else:
- print(dname)
- return device
- #toggles the switch
- async def toggleSwitch(device):
- await device.status.refresh()
- status = device.status.switch
- print(status)
- if status:
- result = await device.switch_off()
- assert result == True
- else:
- result = await device.switch_on()
- assert result == True
- #Function only runs if __name__ == "__main__".
- #You must have the following code in all files using this module!
- async def run(loop):
- api = await getApi(token,session)
- locations = await getLocations(api)
- devices = await getDevices(api)
- closet = await getDeviceByName(devices,"Print Light")
- print(f" \r\r\n\n{api} \n\r connected to SmartThings! \r\n {locations} \r\n Recieved all locations \r\n Receieved all Devices")
- print("------------------------------------- \r\n")
- await toggleSwitch(closet)
- await session.close() #have in all files!
- return None
- if __name__ == "__main__":
- #have the following code in all files using this module
- token = "TOKEN THAT HAS BEEN REMOVED FOR SECURITY"
- session= aiohttp.ClientSession()
- loop = asyncio.get_event_loop()
- loop.run_until_complete(run(loop))
Add Comment
Please, Sign In to add comment