参考网站
1该mqtt
光平台可以让您控制您的MQTT启用灯。它支持设置亮度,色温,效果,闪烁,开/关,RGB颜色,过渡,XY颜色和白色值。
https://www.home-assistant.io/components/light.mqtt/
2 如何自动发现MQTT设备-hass外文教程(全面)
https://www.home-assistant.io/docs/mqtt/discovery/
3 如何自动发现MQTT设备-hass中文教程(简洁)
https://www.hachina.io/docs/7230.html
1 开启自动发现MQTT设备功能----------修改HASS配置文件
HomeAssistant支持自动发现,需要在配置文件中增加:
mqtt:
# 此处为mqtt原有的一些配置
# ……
# 配置自动发现
discovery: true
# 自动发现使用的主题位置前缀,缺省为homeassistant
discovery_prefix: homeassistant
2hass自动发现配置话题格式
设备将自己的配置信息发布在主题位置:
例如:一个编号ID为 “led1091289” 的开关(switch)需要把自己的配置信息发送到如下位置,hass才能自动识别它是一个开关类型设备
homeassistant/switch/led1091289/config
除了开关还有支持自动发现的设备包括:
- 开关型传感器(binary_sensor)
- 摄像头(camera)
- 窗帘(cover)
- 电扇(fan)
- 灯(light)
- 传感器(sensor)
- 开关(switch)
警告: 如果是一个灯,一定要选择好设备类型
例如一个 七彩可调亮度和颜色的灯
正确话题;
homeassistant/light/led1091289/config
在此话题下,hass判定这是一个灯,可以正确使用开关,颜色,亮度修改API
错误话题:
homeassistant/switch/led1091289/config
在此话题下,hass判定这是一个开关(例如插座),只能使用开关API,其余颜色和亮度API无法使用
3设备自身配置变量
例如,一个mqtt设备在配置文件中,一个只具备开关功能的灯配置信息。
switch:
- platform: mqtt
name: "garden"
command_topic: "hachina/switch/irrigation/set"
state_topic: "hachina/switch/irrigation/state"
hachina / 设备类型 /设备编号/ set
hachina / 设备类型 /设备编号/ state
为了给hass能够识别,需要把这些信息打包成json格式;
{"name": "garden", "command_topic": "hachina/switch/irrigation/set", "state_topic": "hachina/switch/irrigation/state"}
然后发送给配置话题
homeassistant/light/led1091289/config
4 示例使用
4.1仅支持开和关的一般灯
配置话题
homeassistant/light/led1/config
发送您内容
{"name": "garden", "command_topic": "hachina/switch/irrigation/set", "state_topic": "hachina/switch/irrigation/state"}
当HomeAssistant读取此信息,相当于配置文件中存在以下内容:
switch:
- platform: mqtt
name: "garden"
command_topic: "hachina/switch/irrigation/set"
state_topic: "hachina/switch/irrigation/state"
4.2支持亮度和RGB
------设备(例如ESP8266)上电后检查和MQTT连接状态,每次断连后重新往配置话题发送自己的配置信息。
配置话题
homeassistant/light/rgb_led1/config