博联A1脚本
发表于 : 周三 5月 24, 2017 19:36
可以获取A1的的数据,只需要建立几个虚拟开关就可以了。
代码: 全选
#!/usr/bin/python
# -*- encoding: utf-8 -*-
import broadlink
import time
import sys
import requests
import urllib
import urllib2
device = broadlink.a1(host=("A1的IP地址",80), mac=bytearray.fromhex("A1的MAC地址"))
device.auth()
data = device.check_sensors_raw()
air_quality = data['air_quality']
temperature = data['temperature']
light = data['light']
noise = data['noise']
humidity = data ['humidity']
domoticzserver = "127.0.0.1:8084"
domoticzusername = ""
domoticzpassword = ""
AIR_IDX="25"
TEMP_IDX="18"
LIGHT_IDX="23"
NOISE_IDX="24"
HUMIDITY_IDX="19"
############
#update temperature
val_temp = "{}".format(temperature)
#print temperature
#print val_temp
url = "http://" + domoticzserver + "/json.htm?type=command¶m=udevice&idx="+ TEMP_IDX + "&nvalue=0&svalue=" + val_temp
#print url
response = urllib2.urlopen(url)
#html=response.read()
#print html
#update humidity
val_temp = "{}".format(humidity)
url = "http://" + domoticzserver + "/json.htm?type=command¶m=udevice&idx="+ HUMIDITY_IDX + "&nvalue=" + val_temp
response = urllib2.urlopen(url)
# update light
lux = '未知'
if light == 0:
lux = '黑'
elif light == 1:
lux = '昏暗'
elif light == 2:
lux = '正常'
elif light == 3:
lux = '亮'
lux = urllib.quote(lux)
url = "http://" + domoticzserver + "/json.htm?type=command¶m=udevice&idx="+ LIGHT_IDX + "&svalue=" + lux
response = urllib2.urlopen(url)
# update noise
db = '未知'
if noise == 0:
db = '寂静'
elif noise == 1:
db = '正常'
elif noise == 2:
db = '吵闹'
db = urllib.quote(db)
print noise
print db
url = "http://" + domoticzserver + "/json.htm?type=command¶m=udevice&idx="+ NOISE_IDX + "&nvalue=0&svalue=" + db
print url
response = urllib2.urlopen(url)
html=response.read()
print html
# update air_quality
air = '未知'
if air_quality == 0:
air = '优'
elif air_quality == 1:
air = '良'
elif air_quality == 2:
air = '正常'
elif air_quality == 3:
air = '差'
air = urllib.quote(air)
url = "http://" + domoticzserver + "/json.htm?type=command¶m=udevice&idx="+ AIR_IDX + "&svalue=" + air
response = urllib2.urlopen(url)