MQTT 更新
mqtt 更新平台允许您将可能通过 MQTT 暴露安装的固件/软件及其最新版本的设备集成到 Home Assistant 中,作为一个更新实体。每当接收到配置中的 topic 下的消息时,实体将在 Home Assistant 中更新。
配置
要在您的安装中启用 MQTT 更新,请将以下内容添加到您的 configuration.yamlconfiguration.yaml 文件是 Home Assistant 的主要配置文件。它列出了要加载的集成及其特定配置。在某些情况下,需要直接在 configuration.yaml 文件中手动编辑配置。大多数集成可以在 UI 中配置。 [Learn more] 文件中:
# 示例 configuration.yaml 条目
mqtt:
- update:
state_topic: topic-installed
latest_version_topic: topic-latest
Configuration Variables
订阅以接收可用性 (在线/离线) 更新的 MQTT 主题列表。不得与 availability_topic 一起使用。
当配置 availability 时,这控制设置实体为 available 所需的条件。有效条目为 all、any 和 latest。如果设置为 all,必须在所有配置的可用性主题上接收到 payload_available,然后实体才会标记为在线。如果设置为 any,则必须在至少一个配置的可用性主题上接收到 payload_available,然后实体才会标记为在线。如果设置为 latest,则最后接收到的在任何配置的可用性主题上的 payload_available 或 payload_not_available 控制可用性。
Defines a template to extract device’s availability from the availability_topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
设备与外界连接的列表,作为元组 [connection_type, connection_identifier] 的列表。例如,网络接口的 MAC 地址: "connections": [["mac", "02:5b:26:a8:dc:12"]]。
用于在前端设置图标的更新的 类型/类。device_class 可以是 null。
实体的 类别。
Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic.
订阅以接收 JSON 字典有效负载的 MQTT 主题,然后将其设置为实体属性。在此主题上接收到消息时隐含 force_update 当前选择状态。
Defines a template to extract the latest version value. Use state_topic with a value_template if all update state values can be extracted from a single JSON payload.
订阅以接收最新版本更新的 MQTT 主题。如果所有更新状态值可以从单个 JSON 有效负载中提取,请使用 state_topic 和 value_template。
必须为 update。仅在 MQTT 自动发现设备消息 中允许和必需。
订阅以接收状态更新的 MQTT 主题。状态更新可以是 JSON 或一个简单字符串,其值为 installed_version。当检测到 JSON 有效负载时,JSON 有效负载的状态值应提供 installed_version 并可以选择性地提供: latest_version、title、release_summary、release_url 和 entity_picture URL。为了允许进度监控,in_progress (布尔值以指示更新正在进行中),或 update_percentage (浮点值以指示进度百分比) 可能是 JSON 消息的一部分。
确保您的主题完全匹配。some-topic/ 和 some-topic 是不同的主题。
示例
这是 Shelly Gen1 设备的更新实体配置示例。
# 示例 configuration.yaml 条目
mqtt:
- update:
name: "Shelly Plug S 固件更新"
title: "Shelly Plug S 固件"
release_url: "https://shelly-api-docs.shelly.cloud/gen1/#changelog"
entity_picture: "https://brands.home-assistant.io/_/shelly/icon.png"
state_topic: "shellies/shellyplug-s-112233/info"
value_template: "{{ value_json['update'].old_version }}"
latest_version_topic: "shellies/shellyplug-s-112233/info"
latest_version_template: "{% if value_json['update'].new_version %}{{ value_json['update'].new_version }}{% else %}{{ value_json['update'].old_version }}{% endif %}"
device_class: "firmware"
command_topic: "shellies/shellyplug-s-112233/command"
payload_install: "update_fw"
JSON 也可以作为 state_topic 有效负载使用。请注意,此功能还允许处理和显示实时进度信息。
{
"installed_version": "1.21.0",
"latest_version": "1.22.0",
"title": "设备固件",
"release_url": "https://example.com/release",
"release_summary": "我们出色的固件的新版本",
"entity_picture": "https://example.com/icon.png"
}
简单的进度状态更新示例:
{
"installed_version": "1.21.0",
"latest_version": "1.22.0",
"title": "设备固件",
"release_url": "https://example.com/release",
"release_summary": "我们出色的固件的新版本",
"entity_picture": "https://example.com/icon.png",
"in_progress": true
}
更新百分比状态更新示例:
{
"installed_version": "1.21.0",
"latest_version": "1.22.0",
"title": "设备固件",
"release_url": "https://example.com/release",
"release_summary": "我们出色的固件的新版本",
"entity_picture": "https://example.com/icon.png",
"update_percentage": 78
}
发布 null 以重置更新百分比状态更新:
{
"installed_version": "1.22.0",
"latest_version": "1.22.0",
"title": "设备固件",
"release_url": "https://example.com/release",
"release_summary": "我们出色的固件的新版本",
"entity_picture": "https://example.com/icon.png",
"update_percentage": null
}
JSON 中的值是可选的,但必须在以下模式中有效:
对于上述 JSON 有效负载示例,update 实体配置应如下所示:
# 示例 configuration.yaml 条目
mqtt:
- update:
name: "神奇设备更新"
title: "设备固件"
state_topic: "amazing-device/state-topic"
device_class: "firmware"
command_topic: "amazing-device/command"
payload_install: "install"
如果设备/服务以 JSON 格式发送数据,但模式不同,则可以使用 value_template 对 JSON 进行重新格式化。
{
"installed_ver": "2022.11",
"new_ver": "2022.12"
}
对于上述 JSON 有效负载,update 实体配置应如下所示:
# 示例 configuration.yaml 条目
mqtt:
update:
name: "神奇设备更新"
title: "设备固件"
state_topic: "amazing-device/state-topic"
value_template: "{{ {'installed_version': value_json.installed_ver, 'latest_version': value_json.new_ver } | to_json }}"
device_class: "firmware"
command_topic: "amazing-device/command"
payload_install: "install"