Improve MQTT update platform (#81131)
* Allow JSON as state_topic payload * Add title * Add release_url * Add release_summary * Add entity_picture * Fix typo * Add abbreviations
This commit is contained in:
parent
b39ff4a020
commit
11ea834cbe
3 changed files with 211 additions and 9 deletions
|
@ -6,7 +6,13 @@ import pytest
|
|||
|
||||
from homeassistant.components import mqtt, update
|
||||
from homeassistant.components.update import DOMAIN as UPDATE_DOMAIN, SERVICE_INSTALL
|
||||
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, Platform
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
STATE_UNKNOWN,
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from .test_common import (
|
||||
|
@ -68,6 +74,10 @@ async def test_run_update_setup(hass, mqtt_mock_entry_with_yaml_config):
|
|||
"state_topic": installed_version_topic,
|
||||
"latest_version_topic": latest_version_topic,
|
||||
"name": "Test Update",
|
||||
"release_summary": "Test release summary",
|
||||
"release_url": "https://example.com/release",
|
||||
"title": "Test Update Title",
|
||||
"entity_picture": "https://example.com/icon.png",
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -84,6 +94,10 @@ async def test_run_update_setup(hass, mqtt_mock_entry_with_yaml_config):
|
|||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get("installed_version") == "1.9.0"
|
||||
assert state.attributes.get("latest_version") == "1.9.0"
|
||||
assert state.attributes.get("release_summary") == "Test release summary"
|
||||
assert state.attributes.get("release_url") == "https://example.com/release"
|
||||
assert state.attributes.get("title") == "Test Update Title"
|
||||
assert state.attributes.get("entity_picture") == "https://example.com/icon.png"
|
||||
|
||||
async_fire_mqtt_message(hass, latest_version_topic, "2.0.0")
|
||||
|
||||
|
@ -126,6 +140,10 @@ async def test_value_template(hass, mqtt_mock_entry_with_yaml_config):
|
|||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get("installed_version") == "1.9.0"
|
||||
assert state.attributes.get("latest_version") == "1.9.0"
|
||||
assert (
|
||||
state.attributes.get("entity_picture")
|
||||
== "https://brands.home-assistant.io/_/mqtt/icon.png"
|
||||
)
|
||||
|
||||
async_fire_mqtt_message(hass, latest_version_topic, '{"latest":"2.0.0"}')
|
||||
|
||||
|
@ -137,6 +155,120 @@ async def test_value_template(hass, mqtt_mock_entry_with_yaml_config):
|
|||
assert state.attributes.get("latest_version") == "2.0.0"
|
||||
|
||||
|
||||
async def test_empty_json_state_message(hass, mqtt_mock_entry_with_yaml_config):
|
||||
"""Test an empty JSON payload."""
|
||||
state_topic = "test/state-topic"
|
||||
await async_setup_component(
|
||||
hass,
|
||||
mqtt.DOMAIN,
|
||||
{
|
||||
mqtt.DOMAIN: {
|
||||
update.DOMAIN: {
|
||||
"state_topic": state_topic,
|
||||
"name": "Test Update",
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
await mqtt_mock_entry_with_yaml_config()
|
||||
|
||||
async_fire_mqtt_message(hass, state_topic, "{}")
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("update.test_update")
|
||||
assert state.state == STATE_UNKNOWN
|
||||
|
||||
|
||||
async def test_json_state_message(hass, mqtt_mock_entry_with_yaml_config):
|
||||
"""Test whether it fetches data from a JSON payload."""
|
||||
state_topic = "test/state-topic"
|
||||
await async_setup_component(
|
||||
hass,
|
||||
mqtt.DOMAIN,
|
||||
{
|
||||
mqtt.DOMAIN: {
|
||||
update.DOMAIN: {
|
||||
"state_topic": state_topic,
|
||||
"name": "Test Update",
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
await mqtt_mock_entry_with_yaml_config()
|
||||
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
state_topic,
|
||||
'{"installed_version":"1.9.0","latest_version":"1.9.0",'
|
||||
'"title":"Test Update Title","release_url":"https://example.com/release",'
|
||||
'"release_summary":"Test release summary"}',
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("update.test_update")
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get("installed_version") == "1.9.0"
|
||||
assert state.attributes.get("latest_version") == "1.9.0"
|
||||
assert state.attributes.get("release_summary") == "Test release summary"
|
||||
assert state.attributes.get("release_url") == "https://example.com/release"
|
||||
assert state.attributes.get("title") == "Test Update Title"
|
||||
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
state_topic,
|
||||
'{"installed_version":"1.9.0","latest_version":"2.0.0","title":"Test Update Title"}',
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("update.test_update")
|
||||
assert state.state == STATE_ON
|
||||
assert state.attributes.get("installed_version") == "1.9.0"
|
||||
assert state.attributes.get("latest_version") == "2.0.0"
|
||||
|
||||
|
||||
async def test_json_state_message_with_template(hass, mqtt_mock_entry_with_yaml_config):
|
||||
"""Test whether it fetches data from a JSON payload with template."""
|
||||
state_topic = "test/state-topic"
|
||||
await async_setup_component(
|
||||
hass,
|
||||
mqtt.DOMAIN,
|
||||
{
|
||||
mqtt.DOMAIN: {
|
||||
update.DOMAIN: {
|
||||
"state_topic": state_topic,
|
||||
"value_template": '{{ {"installed_version": value_json.installed, "latest_version": value_json.latest} | to_json }}',
|
||||
"name": "Test Update",
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
await mqtt_mock_entry_with_yaml_config()
|
||||
|
||||
async_fire_mqtt_message(hass, state_topic, '{"installed":"1.9.0","latest":"1.9.0"}')
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("update.test_update")
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes.get("installed_version") == "1.9.0"
|
||||
assert state.attributes.get("latest_version") == "1.9.0"
|
||||
|
||||
async_fire_mqtt_message(hass, state_topic, '{"installed":"1.9.0","latest":"2.0.0"}')
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("update.test_update")
|
||||
assert state.state == STATE_ON
|
||||
assert state.attributes.get("installed_version") == "1.9.0"
|
||||
assert state.attributes.get("latest_version") == "2.0.0"
|
||||
|
||||
|
||||
async def test_run_install_service(hass, mqtt_mock_entry_with_yaml_config):
|
||||
"""Test that install service works."""
|
||||
installed_version_topic = "test/installed-version"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue