Skip to main content

Awesome python sdk for Aqara development

Project description

项目简介

本SDK为Aqara开放平台的API的Python封装版本,除了对API进行封装外,还提供了一层控制层,用于简化对设备的查询和操作。

使用说明

准备工作

  1. 默认你已经准备好了Aqara设备,注册了Aqara Home账号,并将它们接入了Aqara Home App
  2. 进入Aqara开放平台, 使用你的Aqara Home账号登录,并进入控制台
  3. 在项目管理页面中,创建一个新项目,获取到AppID, AppKey, KeyID

创建Client

这里假定你已经将AppID, AppKey, KeyID保存在环境变量中,可以通过os.getenv获取到这些值

  1. 没有accessToken的情况下,可以创建Client并获取accessToken
from aqara_python_sdk import AqaraClient
import os

app_id = os.getenv("AQARA_APP_ID")
key_id = os.getenv("AQARA_KEY_ID")
app_key = os.getenv("AQARA_APP_KEY")

client = AqaraClient(app_id, key_id, app_key, account="Your Aqara Username")
# 会向你的手机发送一条验证码, 后续获取到Token的有效期可以自行指定,支持1~24h, 1~30d, 1~10y, 默认为30d
client.send_auth_code(token_validity='30d')
# 输入手机收到的验证码
code = input("Please input the code: ")
token_res = client.get_token(code)
token = token_res['accessToken']
refresh_token = token_res['refreshToken']
print(f"Token: {token}")

# 你可以直接进行后续的逻辑,也可以将token保存下来,下次直接使用
# 当次运行中AqaraClient里会保留这次获取到的Token,但是下次运行时需要重新获取
# Your business logic here
  1. 已经有accessToken的情况下,可以直接创建Client
from aqara_python_sdk import AqaraClient
import os

app_id = os.getenv("AQARA_APP_ID")
key_id = os.getenv("AQARA_KEY_ID")
app_key = os.getenv("AQARA_APP_KEY")
token = os.getenv("AQARA_ACCESS_TOKEN")
refresh_token = os.getenv("AQARA_REFRESH_TOKEN")

client = AqaraClient(app_id, key_id, app_key, 
                     account="Your Aqara Username", 
                     token=token, 
                     refresh_token=refresh_token)
# Your business logic here

查询

client只作为一个最底层的API封装类,所以一般不推荐直接调用client中的方法进行操作,而是引入AqaraController进行操作

from aqara_python_sdk import AqaraClient, AqaraController
from aqara_python_sdk.enums import DeviceType
import os

app_id = os.getenv("AQARA_APP_ID")
key_id = os.getenv("AQARA_KEY_ID")
app_key = os.getenv("AQARA_APP_KEY")
token = os.getenv("AQARA_ACCESS_TOKEN")
refresh_token = os.getenv("AQARA_REFRESH_TOKEN")

client = AqaraClient(app_id, key_id, app_key, 
                     account="Your Aqara Username", 
                     token=token, 
                     refresh_token=refresh_token)

controller = AqaraController(client)
# 加载所有数据,如果数据在其他地方有更新(如在Aqara Home APP中更新了),需要重新调用一次此方法获取最新的数据
controller.load_data()

# 查询指定设备
devices = controller.query_device().position_name("客厅").device_type(DeviceType.LIGHT).device_name("我的射灯").query()

# 查询灯设备
light_controls = controller.query_device().position_name("客厅").device_type(DeviceType.LIGHT).device_name("我的射灯").light()

# 查询场景
all_scene_names = controller.scene().list_scene_names()
my_scene = controller.scene().get_scene_by_name("我的场景")

操作设备 / 场景

from aqara_python_sdk import AqaraClient, AqaraController
from aqara_python_sdk.enums import DeviceType
import os

app_id = os.getenv("AQARA_APP_ID")
key_id = os.getenv("AQARA_KEY_ID")
app_key = os.getenv("AQARA_APP_KEY")
token = os.getenv("AQARA_ACCESS_TOKEN")
refresh_token = os.getenv("AQARA_REFRESH_TOKEN")

client = AqaraClient(app_id, key_id, app_key, 
                     account="Your Aqara Username", 
                     token=token, 
                     refresh_token=refresh_token)

controller = AqaraController(client)
# 加载所有数据,如果数据在其他地方有更新(如在Aqara Home APP中更新了),需要重新调用一次此方法获取最新的数据
controller.load_data()

# 获取灯设备
my_light = controller.query_device().position_name("客厅").device_type(DeviceType.LIGHT).device_name("我的射灯").light()[0]
# 执行灯操作
if not my_light.is_on():
    my_light.turn_on()
my_light.set_brightness(50)
my_light.set_color_temperature(4000)

# 执行场景
controller.scene().execute_scene("我的场景")

TODO List:

  • 发布至Pypi
  • 查询功能完善
  • 支持更多开关功能定制
  • 支持窗帘等更多设备
  • 支持场景的创建和编辑

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

aqara_python_sdk-0.0.3.1.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aqara_python_sdk-0.0.3.1-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

Details for the file aqara_python_sdk-0.0.3.1.tar.gz.

File metadata

  • Download URL: aqara_python_sdk-0.0.3.1.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.22.4 CPython/3.13.2 Darwin/24.1.0

File hashes

Hashes for aqara_python_sdk-0.0.3.1.tar.gz
Algorithm Hash digest
SHA256 090f5ad8fffca96afdec2c7a0a597ef7a4df19ac3448b5c22835d0abc065a546
MD5 64dd94061719f419c62dbb25c8d01756
BLAKE2b-256 55a6c190370afddefab4156a2702c3268d1935f17d1c2b9c0a306e7c61b22bb5

See more details on using hashes here.

File details

Details for the file aqara_python_sdk-0.0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for aqara_python_sdk-0.0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3800409a73ce7294d09c6ef5deae40b9c5ba389b01ba03a53378452d6f5839c4
MD5 9f17732bcb626d90dcfea0a7f4e40c53
BLAKE2b-256 fa9de9c23bd7b4b605f17140e24469a6e75ba871b0cfec28a4f505514c36b6a0

See more details on using hashes here.

Supported by

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