本帖最后由 nontrace 于 2020-11-1 17:40 编辑
根据囧大神的帖子:传送门
接入了天猫精灵,接入步骤:
1:将灯,温湿度传感器等设备接入ha
2:在configuration.yaml的同一目录创建customize.yaml文件,在configuration.yaml文件中添加如下配置
homeassistant:
customize: !include customize.yaml
customize.yaml内容如下,
light.rgblighta4cf12efbd20:
friendly_name: 灯
tmall_genie: true
sensor.temperatureraspberrypi:
friendly_name: 温度
tmall_genie:
enabled: true
attach_sensor:
- entity_id: sensor.temperatureraspberrypi001
sensor.humidityraspberrypi:
friendly_name: 湿度
tmall_genie:
enabled: true
attach_sensor:
- entity_id: sensor.humidityraspberrypi001
3: 让ha可以外网访问
4:生成长token
5:访问此链接填入论坛账号绑定的邮箱和ha外网地址,点击登录,跳转到ha登录页面,登录成功后,根据提示填写邮箱地址,ha外网地址,和长令牌
6:打开自己的天猫精灵APP,在添加设备页面搜索HASSMART,然后点击其中任意一个设备,然后点击下边的同步设备,就能看到设备
7:默认只能集成一个设备,需要在回复教程贴申请提升接入数量,申请通过后可接入三个。
今天天猫精灵接入设备数量权限终于开放了。
但是我的温湿度传感器天猫精灵无法读取,每次问天猫精灵都传感器无法获取温湿度数据。
我是通过python脚本获取的温湿度数据然后通过mqtt接入的,Python脚本如下
import paho.mqtt.client as mqtt
import time
import Adafruit_DHT
# 设置传感器类型
sensor=Adafruit_DHT.DHT11
# 设置GPIO引脚
gpio=17
# mqtt客户端
client = mqtt.Client()
# 链接上服务器后回调
def on_connect(client, userdata, flags, rc):
print(f"Connected with result code {rc}")
# 设置链接成功后回调
client.on_connect = on_connect
# 链接mqtt服务器
client.connect("localhost", 1883, 60)
time.sleep(1)
# 注册温度传感器设备
client.publish('homeassistant/sensor/temperatureraspberrypi/config', payload='{"name":"temperatureraspberrypi","unique_id":"temperatureraspberrypi","state_topic":"dht11/state/dht11raspberrypi","value_template":"{{value_json.temperature}}","device_class":"temperature","unit_of_measurement":"°C"}', qos=0, retain=True)
# 注册湿度传感器设备
client.publish('homeassistant/sensor/humidityraspberrypi/config', payload='{"name":"humidityraspberrypi","unique_id":"humidityraspberrypi","state_topic":"dht11/state/dht11raspberrypi","value_template":"{{value_json.humidity}}","device_class":"humidity","unit_of_measurement":"%"}', qos=0, retain=True)
time.sleep(60)
while True:
# 读取温度与湿度
humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)
# 要发送的json字符串
payloadStr='{"temperature":"'+str(temperature)+'","humidity":"'+str(humidity)+'"}'
print(payloadStr)
client.loop(3)
# 发送到mqtt
client.publish('dht11/state/dht11raspberrypi', payload=payloadStr, qos=0, retain=True)
time.sleep(60)
已经可以正常接入ha,也可以正常显示,也接入了天猫精灵,但是就是天猫精灵无法读取。请指点
|