zigate网关的python库(zigbee)http://zigate.fr
zigate的Python项目详细描述
zigate
用于ZiGate的python库。 这个库管理python和zigate密钥之间的通信,支持usb和wifi密钥。
Zigate是一个通用网关,与许多Zigbee设备(如小米、Philipps Hue、宜家等)兼容。
当前覆盖范围:
- 命令69/91
- 回答48/52
开始
安装
要安装,请执行以下操作:
pip3 install zigate
或者如果您计划使用mqtt
pip3 install zigate[mqtt]
使用量
# if you want loggingimportlogginglogging.basicConfig()logging.root.setLevel(logging.DEBUG)importzigatez=zigate.connect(port=None)# Leave None to auto-discover the portprint(z.get_version())OrderedDict([('major',1),('installer','30c'),('rssi',0),('version','3.0c')])print(z.get_version_text())3.0c# refresh devices listz.get_devices_list()# start inclusion mode>>>z.permit_join()>>>z.is_permitting_join()True# list devices>>>z.devices[Device677c,Deviceb8ce,Device92a7,Device59ef]>>>z.devices[0].addr'677c'# get all discovered endpoints>>>z.devices[0].endpoints{1:{'clusters':{0:Cluster0General:Basic,1026:Cluster1026Measurement:Temperature,1027:Cluster1027Measurement:AtmosphericPressure,1029:Cluster1029Measurement:Humidity},}}# get well known attributes>>>forattributeinz.devices[0].properties:print(attribute){'data':'lumi.weather','name':'type','attribute':5,'value':'lumi.weather'}{'data':'0121c70b0421a8010521090006240100000000642932096521851c662bd87c01000a210000','name':'battery','value':3.015,'unit':'V','attribute':65281}{'data':-1983,'name':'temperature','value':-19.83,'unit':'°C','attribute':0}{'data':9779,'name':'pressure2','value':977.9,'unit':'mb','attribute':16}{'data':977,'name':'pressure','value':977,'unit':'mb','attribute':0}{'data':4484,'name':'humidity','value':44.84,'unit':'%','attribute':0}# get specific property>>>z.devices[0].get_property('temperature'){'data':-1983,'name':'temperature','value':-19.83,'unit':'°C','attribute':0}# call action on devicesz.action_onoff('b8ce',1,zigate.ON)# or from devicesz.devices[1].action_onoff(zigate.ON)# OTA process# Load image and send headers to ZiGatez.ota_load_image('path/to/ota/image_file.ota')# Tell client that image is availablez.ota_image_notify('addr')# It will take client usually couple seconds to query headers# from server. Upgrade process start automatically if correct# headers are loaded to ZiGate. If you have logging level debug# enabled you will get automatically progress updates.# Manually check ota status - logging level INFOz.get_ota_status()# Whole upgrade process time depends on device and ota image size# Upgrading ikea bulb took ~15 minutes# Upgrading ikea remote took ~45 minutes
OTA更新
一些设备(如宜家Tradfri)可以更新。 对于宜家,您可以使用以下命令下载可用的OTA文件:
python3 zigate.ikea_ota_download
# OTA process# Load image and send headers to ZiGatez.ota_load_image('path/to/ota/image_file.ota')# Tell client that image is availablez.ota_image_notify('addr')# It will take client usually couple seconds to query headers# from server. Upgrade process start automatically if correct# headers are loaded to ZiGate. If you have logging level debug# enabled you will get automatically progress updates.# Manually check ota status - logging level INFOz.get_ota_status()# Whole upgrade process time depends on device and ota image size# Upgrading ikea bulb took ~15 minutes# Upgrading ikea remote took ~45 minutes
回拨
我们使用pydispatcher进行回调
fromzigateimportdispatcherdefmy_callback(sender,signal,**kwargs):print(sender)# zigate instanceprint(signal)# one of EVENTprint(kwargs)# contains device and/or attribute changes, etcdispatcher.connect(my_callback,zigate.ZIGATE_ATTRIBUTE_UPDATED)z=zigate.connect()# orz=zigate.connect(port='/dev/ttyUSB0')# to catch any eventsdispatcher.connect(my_callback,dispatcher.Any)
事件可以是:
zigate.ZIGATE_DEVICE_ADDEDzigate.ZIGATE_DEVICE_UPDATEDzigate.ZIGATE_DEVICE_REMOVEDzigate.ZIGATE_DEVICE_ADDRESS_CHANGEDzigate.ZIGATE_ATTRIBUTE_ADDEDzigate.ZIGATE_ATTRIBUTE_UPDATEDzigate.ZIGATE_DEVICE_NEED_DISCOVERY
Kwargs取决于事件类型:
- 对于
zigate.ZIGATE_DEVICE_ADDED
,kwargs包含设备。 - 对于
zigate.ZIGATE_DEVICE_UPDATED
Kwargs包含设备。 - 对于
zigate.ZIGATE_DEVICE_REMOVED
,kwargs包含addr(设备短地址)。 - 对于
zigate.ZIGATE_DEVICE_ADDRESS_CHANGED
,kwargs包含旧地址和新地址(在重新配对已知设备时使用)。 - 对于
zigate.ZIGATE_ATTRIBUTE_ADDED
,kwargs包含设备和发现的属性。 - 对于
zigate.ZIGATE_ATTRIBUTE_UPDATED
,kwargs包含设备和更新的属性。 - 对于
zigate.ZIGATE_DEVICE_NEED_DISCOVERY
Kwargs包含设备。
WiFi zigate
WiFi zigate也受支持:
importzigatez=zigate.connect(host='192.168.0.10')# or if you want to set the portz=zigate.connect(host='192.168.0.10:1234')
pizigate
pi zigate(覆盆子pi的zigate模块)也受支持:
importzigatez=zigate.connect(gpio=True)# or if you want to set the portz=zigate.connect(port='/dev/serial0',gpio=True)
要在rpi3上使用pizigate,您需要禁用蓝牙模块。 禁用蓝牙:
- 在
/boot/config.txt
中添加 - 从
/boot/cmdline.txt
中删除 - 禁用hciuart
sudo systemctl disable hciuart
- 将用户添加到gpio组,例如使用pi user
sudo usermod -aG gpio pi
- 以及
reboot
dtoverlay=pi3-disable-bt
console=serial0,115200
或者,您可以为蓝牙或zigate设置mini-uart,但请注意存在性能问题。
MQTT代理
这需要paho mqtt。它可以作为pip3 install zigate[mqtt]
的依赖项安装
python3 -m zigate.mqtt_broker --device auto --mqtt_host localhost:1883
添加--mqtt_username
和--mqtt_password
作为参数,并允许它们用于建立到mqtt代理的连接。
代理发布以下主题:zigate/device_changed/[addr]
有效载荷示例:
'zigate/device_changed/522a'{"addr":"522a","endpoints":[{"device":0,"clusters":[{"cluster":1026,"attributes":[{"value":22.27,"data":2227,"unit":"\u00b0C","name":"temperature","attribute":0}]},{"cluster":1027,"attributes":[{"value":977,"data":977,"unit":"mb","name":"pressure","attribute":0},{"value":977.7,"data":9777,"unit":"mb","name":"pressure2","attribute":16},{"data":-1,"attribute":20}]},{"cluster":1029,"attributes":[{"value":35.03,"data":3503,"unit":"%","name":"humidity","attribute":0}]}],"profile":0,"out_clusters":[],"in_clusters":[],"endpoint":1}],"info":{"power_source":0,"ieee":"158d0002271c25","addr":"522a","id":2,"rssi":255,"last_seen":"2018-02-21 09:41:27"}}
已删除zigate/设备。 有效载荷示例:
{"addr":"522a"}
zigate/attribute_changed/[addr]/[endpoint]/[cluster]/[attribute]负载的属性已更改。 有效载荷示例:
'zigate/attribute_changed/522a/01/0403/0010'{"cluster":1027,"value":978.5,"data":9785,"attribute":16,"unit":"mb","endpoint":1,"addr":"522a","name":"pressure2"}
您可以使用主题zigate向zigate发送命令/命令有效负载应为:
{"function":"function_name","args":["optional","args","list"]}# example to start permit joinpayload='{"function": "permit_join"}'client.publish('zigate/command',payload)
代理将使用主题“zigate/command/result”发布结果。 有效载荷示例:
{"function":"permit_join","result":0}
所有zigate函数都可以调用:
# turn on endpoint 1payload='{"function": "action_onoff", "args": ["522a", 1, 1]}'client.publish('zigate/command',payload)# turn off endpoint 1payload='{"function": "action_onoff", "args": ["522a", 1, 0]}'client.publish('zigate/command',payload)
闪光灯
用于刷新zigate的python工具(jennic jn5168)
感谢Sander Hoentjen(Tjikkun),我们现在有了闪光灯! Original repo
闪光灯使用
usage: python3 -m zigate.flasher [-h] -p {/dev/ttyUSB0}[-w WRITE][-s SAVE][-e][--pdm-only] optional arguments: -h, --help show this help message and exit -p {/dev/ttyUSB0}, --serialport {/dev/ttyUSB0} Serial port, e.g. /dev/ttyUSB0 -w WRITE, --write WRITE Firmware bin to flash onto the chip -s SAVE, --save SAVE File to save the currently loaded firmware to -e, --erase Erase EEPROM --pdm-only Erase PDM only, use it with --erase