Package: pysmartthings
pysmartthingsA python library for interacting with the SmartThings cloud API build withasyncioandaiohttp.FeaturesThe package is still in beta, but the following features are available:Locations: List, GetRooms: List, Get, Create, Update, DeletegitDevices: List, Get, Command, StatusApps: List, Get, Create, Update, Delete, Settings Get & Update, OAuth: Get, Update, & GenerateInstalledApps: List, Get, DeleteSubscriptions: List, Get, Create, Delete, Delete AllScenes: List, ExecuteOAuth: Generate refresh/access token pairInstallationpip install pysmartthingsorpip install --use-wheel pysmartthingsUsageInitializationThe SmartThings class encapsulates the API operations and the constructor accepts the aiohttpWebSessionand yourpersonal access token.import aiohttp
import pysmartthings

token = 'PERSONAL_ACCESS_TOKEN'

async with aiohttp.ClientSession() as session:
    api = pysmartthings.SmartThings(session, token)
    # ...LocationsA list of locations in SmartThings can be retrieved by invoking the coroutinelocations().locations = await api.locations()
    print(len(locations))

    location = locations[0]
    print(location.name)
    print(location.location_id)Outputs:2
'Test Home'
'5c03e518-118a-44cb-85ad-7877d0b302e4'DevicesA list of devices can be retrieved by invoking the coroutinedevices(location_ids=None, capabilities=None, device_ids=None). The optional parameters allow filtering the returned list.devices = await api.devices()
    print(len(devices))

    device = devices[0]
    print(device.device_id)
    print(device.name)
    print(device.label)
    print(device.capabilities)Outputs:19
'0d38d5ca-705f-44f7-89bd-36a8cf73678d'
'GE In-Wall Smart Dimmer'
'Back Patio Light'
['switch', 'switchLevel', 'refresh', 'indicator', 'button', 'sensor', 'actuator', 'healthCheck', 'light']The current status of the device is populated when the coroutinestatus.refresh()is called. The DeviceStatus class represents the current values of the capabilities and provides several normalized property accessors.await device.status.refresh()
    print(device.status.values)
    print(device.status.switch)
    print(device.status.level)Outputs:{'button': 'pressed', 'numberOfButtons': None, 'supportedButtonValues': None, 'indicatorStatus': 'when off', 'switch': 'on', 'checkInterval': 1920, 'healthStatus': None, 'DeviceWatch-DeviceStatus': None, 'level': 100}
True
100Device CommandsYou can execute a command on a device by calling the coroutinecommand(component_id, capability, command, args=None)function. Thecomponent_idparameter is the identifier of the component within the device (mainis the device itself);capabilityis the name of the capability implemented by the device; andcommandis one of the defined operations within the capability.argsis an array of parameters to pass to the command when it accepts parameters (optional). See theSmartThings Capability Referencefor more information.result = await device.command("main", "switch", "on")
    assert result == True

    result = await device.command("main", "switchLevel", "setLevel", [75, 2])
    assert result == TrueDevices with theswitchcapability have the following coroutines:result = await device.switch_on()
    assert result == True

    result = await device.switch_off()
    assert result == TrueDevices with theswitchLevelcapability have the following function that sets the target brightness level and transitions using a specific duration (seconds).result = await device.set_level(75, 2)
    assert result == TrueDevices with thewindowShadeLevelcapability have the following function that sets the target shade level.result = await device.set_window_shade_level(50)
    assert result == True