Deduplicate MQTT tests (#32874)
This commit is contained in:
1 parent
a278cf3db2
commit
86d48c608e
16 files changed
+908
-1747
No files matched your search
@@ -6,21 +6,26 @@ from unittest.mock import patch
|
||||
from homeassistant.components import mqtt
|
||||
from homeassistant.components.mqtt.discovery import async_start
|
||||
import homeassistant.components.sensor as sensor
|
||||
from homeassistant.const import EVENT_STATE_CHANGED, STATE_UNAVAILABLE
|
||||
from homeassistant.const import EVENT_STATE_CHANGED
|
||||
import homeassistant.core as ha
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from .common import (
|
||||
help_test_availability_without_topic,
|
||||
help_test_custom_availability_payload,
|
||||
help_test_default_availability_payload,
|
||||
help_test_discovery_broken,
|
||||
help_test_discovery_removal,
|
||||
help_test_discovery_update,
|
||||
help_test_discovery_update_attr,
|
||||
help_test_entity_device_info_remove,
|
||||
help_test_entity_device_info_update,
|
||||
help_test_entity_device_info_with_connection,
|
||||
help_test_entity_device_info_with_identifier,
|
||||
help_test_entity_id_update,
|
||||
help_test_setting_attribute_via_mqtt_json_message,
|
||||
help_test_setting_attribute_with_template,
|
||||
help_test_unique_id,
|
||||
help_test_update_with_json_attrs_bad_JSON,
|
||||
help_test_update_with_json_attrs_not_dict,
|
||||
@@ -32,28 +37,8 @@ from tests.common import (
|
||||
async_fire_time_changed,
|
||||
)
|
||||
|
||||
DEFAULT_CONFIG_ATTR = {
|
||||
sensor.DOMAIN: {
|
||||
"platform": "mqtt",
|
||||
"name": "test",
|
||||
"state_topic": "test-topic",
|
||||
"json_attributes_topic": "attr-topic",
|
||||
}
|
||||
}
|
||||
|
||||
DEFAULT_CONFIG_DEVICE_INFO = {
|
||||
"platform": "mqtt",
|
||||
"name": "Test 1",
|
||||
"state_topic": "test-topic",
|
||||
"device": {
|
||||
"identifiers": ["helloworld"],
|
||||
"connections": [["mac", "02:5b:26:a8:dc:12"]],
|
||||
"manufacturer": "Whatever",
|
||||
"name": "Beer",
|
||||
"model": "Glass",
|
||||
"sw_version": "0.1-beta",
|
||||
},
|
||||
"unique_id": "veryunique",
|
||||
DEFAULT_CONFIG = {
|
||||
sensor.DOMAIN: {"platform": "mqtt", "name": "test", "state_topic": "test-topic"}
|
||||
}
|
||||
|
||||
|
||||
@@ -234,65 +219,26 @@ async def test_force_update_enabled(hass, mqtt_mock):
|
||||
assert len(events) == 2
|
||||
|
||||
|
||||
async def test_default_availability_payload(hass, mqtt_mock):
|
||||
"""Test availability by default payload with defined topic."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
sensor.DOMAIN,
|
||||
{
|
||||
sensor.DOMAIN: {
|
||||
"platform": "mqtt",
|
||||
"name": "test",
|
||||
"state_topic": "test-topic",
|
||||
"availability_topic": "availability-topic",
|
||||
}
|
||||
},
|
||||
async def test_availability_without_topic(hass, mqtt_mock):
|
||||
"""Test availability without defined availability topic."""
|
||||
await help_test_availability_without_topic(
|
||||
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
state = hass.states.get("sensor.test")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
async_fire_mqtt_message(hass, "availability-topic", "online")
|
||||
|
||||
state = hass.states.get("sensor.test")
|
||||
assert state.state != STATE_UNAVAILABLE
|
||||
|
||||
async_fire_mqtt_message(hass, "availability-topic", "offline")
|
||||
|
||||
state = hass.states.get("sensor.test")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
async def test_default_availability_payload(hass, mqtt_mock):
|
||||
"""Test availability by default payload with defined topic."""
|
||||
await help_test_default_availability_payload(
|
||||
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_custom_availability_payload(hass, mqtt_mock):
|
||||
"""Test availability by custom payload with defined topic."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
sensor.DOMAIN,
|
||||
{
|
||||
sensor.DOMAIN: {
|
||||
"platform": "mqtt",
|
||||
"name": "test",
|
||||
"state_topic": "test-topic",
|
||||
"availability_topic": "availability-topic",
|
||||
"payload_available": "good",
|
||||
"payload_not_available": "nogood",
|
||||
}
|
||||
},
|
||||
await help_test_custom_availability_payload(
|
||||
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
state = hass.states.get("sensor.test")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
async_fire_mqtt_message(hass, "availability-topic", "good")
|
||||
|
||||
state = hass.states.get("sensor.test")
|
||||
assert state.state != STATE_UNAVAILABLE
|
||||
|
||||
async_fire_mqtt_message(hass, "availability-topic", "nogood")
|
||||
|
||||
state = hass.states.get("sensor.test")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
|
||||
async def test_setting_sensor_attribute_via_legacy_mqtt_json_message(hass, mqtt_mock):
|
||||
"""Test the setting of attribute via MQTT with JSON payload."""
|
||||
@@ -432,72 +378,36 @@ async def test_valid_device_class(hass, mqtt_mock):
|
||||
|
||||
async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
|
||||
"""Test the setting of attribute via MQTT with JSON payload."""
|
||||
config = {
|
||||
sensor.DOMAIN: {
|
||||
"platform": "mqtt",
|
||||
"name": "test",
|
||||
"state_topic": "test-topic",
|
||||
"json_attributes_topic": "attr-topic",
|
||||
}
|
||||
}
|
||||
await help_test_setting_attribute_via_mqtt_json_message(
|
||||
hass, mqtt_mock, sensor.DOMAIN, config
|
||||
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_setting_attribute_with_template(hass, mqtt_mock):
|
||||
"""Test the setting of attribute via MQTT with JSON payload."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
sensor.DOMAIN,
|
||||
{
|
||||
sensor.DOMAIN: {
|
||||
"platform": "mqtt",
|
||||
"name": "test",
|
||||
"state_topic": "test-topic",
|
||||
"json_attributes_topic": "attr-topic",
|
||||
"json_attributes_template": "{{ value_json['Timer1'] | tojson }}",
|
||||
}
|
||||
},
|
||||
await help_test_setting_attribute_with_template(
|
||||
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
async_fire_mqtt_message(
|
||||
hass, "attr-topic", json.dumps({"Timer1": {"Arm": 0, "Time": "22:18"}})
|
||||
)
|
||||
state = hass.states.get("sensor.test")
|
||||
|
||||
assert state.attributes.get("Arm") == 0
|
||||
assert state.attributes.get("Time") == "22:18"
|
||||
|
||||
|
||||
async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
|
||||
"""Test attributes get extracted from a JSON result."""
|
||||
await help_test_update_with_json_attrs_not_dict(
|
||||
hass, mqtt_mock, caplog, sensor.DOMAIN, DEFAULT_CONFIG_ATTR
|
||||
hass, mqtt_mock, caplog, sensor.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
|
||||
"""Test attributes get extracted from a JSON result."""
|
||||
await help_test_update_with_json_attrs_bad_JSON(
|
||||
hass, mqtt_mock, caplog, sensor.DOMAIN, DEFAULT_CONFIG_ATTR
|
||||
hass, mqtt_mock, caplog, sensor.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_discovery_update_attr(hass, mqtt_mock, caplog):
|
||||
"""Test update of discovered MQTTAttributes."""
|
||||
data1 = (
|
||||
'{ "name": "test",'
|
||||
' "state_topic": "test_topic",'
|
||||
' "json_attributes_topic": "attr-topic1" }'
|
||||
)
|
||||
data2 = (
|
||||
'{ "name": "test",'
|
||||
' "state_topic": "test_topic",'
|
||||
' "json_attributes_topic": "attr-topic2" }'
|
||||
)
|
||||
await help_test_discovery_update_attr(
|
||||
hass, mqtt_mock, caplog, sensor.DOMAIN, data1, data2
|
||||
hass, mqtt_mock, caplog, sensor.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
@@ -546,46 +456,37 @@ async def test_discovery_broken(hass, mqtt_mock, caplog):
|
||||
)
|
||||
|
||||
|
||||
async def test_entity_device_info_with_connection(hass, mqtt_mock):
|
||||
"""Test MQTT sensor device registry integration."""
|
||||
await help_test_entity_device_info_with_connection(
|
||||
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_entity_device_info_with_identifier(hass, mqtt_mock):
|
||||
"""Test MQTT sensor device registry integration."""
|
||||
await help_test_entity_device_info_with_identifier(
|
||||
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO
|
||||
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_entity_device_info_update(hass, mqtt_mock):
|
||||
"""Test device registry update."""
|
||||
await help_test_entity_device_info_update(
|
||||
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG_DEVICE_INFO
|
||||
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_entity_device_info_remove(hass, mqtt_mock):
|
||||
"""Test device registry remove."""
|
||||
config = {
|
||||
"platform": "mqtt",
|
||||
"name": "Test 1",
|
||||
"state_topic": "test-topic",
|
||||
"device": {"identifiers": ["helloworld"]},
|
||||
"unique_id": "veryunique",
|
||||
}
|
||||
await help_test_entity_device_info_remove(hass, mqtt_mock, sensor.DOMAIN, config)
|
||||
await help_test_entity_device_info_remove(
|
||||
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_entity_id_update(hass, mqtt_mock):
|
||||
"""Test MQTT subscriptions are managed when entity_id is updated."""
|
||||
config = {
|
||||
sensor.DOMAIN: [
|
||||
{
|
||||
"platform": "mqtt",
|
||||
"name": "beer",
|
||||
"state_topic": "test-topic",
|
||||
"availability_topic": "avty-topic",
|
||||
"unique_id": "TOTALLY_UNIQUE",
|
||||
}
|
||||
]
|
||||
}
|
||||
await help_test_entity_id_update(hass, mqtt_mock, sensor.DOMAIN, config)
|
||||
await help_test_entity_id_update(hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG)
|
||||
|
||||
|
||||
async def test_entity_device_info_with_hub(hass, mqtt_mock):
|
||||
|
||||
Reference in new issue
Block a user