用于与使用asyncio和aiohttp构建的smartthings云api交互的python库。
pysmartthings的Python项目详细描述
pysmartthings
用于与使用asyncio和aiohttp构建的SmartThings云API交互的Python库。
功能
该软件包仍处于测试阶段,但提供以下功能:
- 地点:列表,获取
- 房间:列出、获取、创建、更新、删除
- 设备:列表、获取、命令、状态
- 应用程序:list、get、create、update、delete、settings get&update、oauth:get、update和generate
- 安装应用程序:列出、获取、删除
- 订阅:列出、获取、创建、删除、全部删除
- 场景:列表,执行
- OAuth:生成刷新/访问令牌对
安装
pip install pysmartthings
或
pip install --use-wheel pysmartthings
用法
初始化
smartthings类封装了api操作,构造函数接受aiohttpWebSession
和您的personal access token。
import aiohttp import pysmartthings token = 'PERSONAL_ACCESS_TOKEN' async with aiohttp.ClientSession() as session: api = pysmartthings.SmartThings(session, token) # ...
位置
可以通过调用协同路由locations()
来检索SmartThings中的位置列表。
locations = await api.locations() print(len(locations)) location = locations[0] print(location.name) print(location.location_id)
输出:
2 'Test Home' '5c03e518-118a-44cb-85ad-7877d0b302e4'
设备
可以通过调用协同路由devices(location_ids=None, capabilities=None, device_ids=None)
来检索设备列表。可选参数允许筛选返回的列表。
devices = await api.devices() print(len(devices)) device = devices[0] print(device.device_id) print(device.name) print(device.label) print(device.capabilities)
输出:
19 '0d38d5ca-705f-44f7-89bd-36a8cf73678d' 'GE In-Wall Smart Dimmer' 'Back Patio Light' ['switch', 'switchLevel', 'refresh', 'indicator', 'button', 'sensor', 'actuator', 'healthCheck', 'light']
当调用协程status.refresh()
时,将填充设备的当前状态。devicestatus类表示功能的当前值,并提供几个规范化的属性访问器。
await device.status.refresh() print(device.status.values) print(device.status.switch) print(device.status.level)
输出:
{'button': 'pressed', 'numberOfButtons': None, 'supportedButtonValues': None, 'indicatorStatus': 'when off', 'switch': 'on', 'checkInterval': 1920, 'healthStatus': None, 'DeviceWatch-DeviceStatus': None, 'level': 100} True 100
设备命令
可以通过调用coroutinecommand(capability, command, args=None)
函数在设备上执行命令。capability
参数对应于检测到的功能之一,command
是define命令之一。args
是要传递给命令的参数数组(可选)。有关详细信息,请参见SmartThings Capability Reference。
result = await device.command("switch", "on") assert result == True result = await device.command("switchLevel", "setLevel", [75, 2]) assert result == True
具有switch
功能的设备具有以下协同程序:
result = await device.switch_on() assert result == True result = await device.switch_off() assert result == True
具有switchLevel
功能的设备具有以下功能,可以使用特定的持续时间(秒)设置目标亮度级别和转换。
result = await device.set_level(75, 2) assert result == True