Skip to main content

A python library for interacting with the SmartThings cloud API build with asyncio and aiohttp.

Project description

pysmartthings

CI Status codecov image image image

A python library for interacting with the SmartThings cloud API build with asyncio and aiohttp.

Features

The package is still in beta, but the following features are available:

  1. Locations: List, Get
  2. Rooms: List, Get, Create, Update, Deletegit
  3. Devices: List, Get, Command, Status
  4. Apps: List, Get, Create, Update, Delete, Settings Get & Update, OAuth: Get, Update, & Generate
  5. InstalledApps: List, Get, Delete
  6. Subscriptions: List, Get, Create, Delete, Delete All
  7. Scenes: List, Execute
  8. OAuth: Generate refresh/access token pair

Installation

pip install pysmartthings

or

pip install --use-wheel pysmartthings

Usage

Initialization

The SmartThings class encapsulates the API operations and the constructor accepts the aiohttp WebSession and your personal access token.

import aiohttp
import pysmartthings

token = 'PERSONAL_ACCESS_TOKEN'

async with aiohttp.ClientSession() as session:
    api = pysmartthings.SmartThings(session, token)
    # ...

Locations

A list of locations in SmartThings can be retrieved by invoking the coroutine locations().

    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'

Devices

A list of devices can be retrieved by invoking the coroutine devices(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 coroutine status.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
100

Device Commands

You can execute a command on a device by calling the coroutine command(component_id, capability, command, args=None) function. The component_id parameter is the identifier of the component within the device (main is the device itself); capability is the name of the capability implemented by the device; and command is one of the defined operations within the capability. args is an array of parameters to pass to the command when it accepts parameters (optional). See the SmartThings Capability Reference for more information.

    result = await device.command("main", "switch", "on")
    assert result == True

    result = await device.command("main", "switchLevel", "setLevel", [75, 2])
    assert result == True

Devices with the switch capability have the following coroutines:

    result = await device.switch_on()
    assert result == True

    result = await device.switch_off()
    assert result == True

Devices with the switchLevel capability 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 == True

Devices with the windowShadeLevel capability have the following function that sets the target shade level.

    result = await device.set_window_shade_level(50)
    assert result == True

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pysmartthings-0.7.8.tar.gz (42.6 kB view details)

Uploaded Source

Built Distribution

pysmartthings-0.7.8-py3-none-any.whl (34.8 kB view details)

Uploaded Python 3

File details

Details for the file pysmartthings-0.7.8.tar.gz.

File metadata

  • Download URL: pysmartthings-0.7.8.tar.gz
  • Upload date:
  • Size: 42.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pysmartthings-0.7.8.tar.gz
Algorithm Hash digest
SHA256 c92140a9146804d902a99c6f29190f7334082dd7dd9a24c45479adf8d7cd74d7
MD5 b23117c553e5617474630fe1587a4be6
BLAKE2b-256 83bf975a1db67b3b24b49c6205202889efe798cfa4eeda2d5ceb78fa176139fd

See more details on using hashes here.

File details

Details for the file pysmartthings-0.7.8-py3-none-any.whl.

File metadata

File hashes

Hashes for pysmartthings-0.7.8-py3-none-any.whl
Algorithm Hash digest
SHA256 ab1820b4375942a66ee7012afdbb58350862b246b0cb9fca110f9eb97f6f524e
MD5 caafda4c1fa79ee6f093fec1c495689c
BLAKE2b-256 b4cab84eafccc5866e15a5ed2a93c3080291538438fedc1841cb5835d6428492

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page