Update MQTT tests to use the config entry setup (#72373)

* New testframework and tests for fan platform

* Merge test_common_new to test_common

* Add alarm_control_panel

* Add binary_sensor

* Add button

* Add camera

* Add climate

* Add config_flow

* Add cover

* Add device_tracker_disovery

* Add device_trigger

* Add diagnostics

* Add discovery

* Add humidifier

* Add init

* Add lecacy_vacuum

* Add light_json

* Add light_template

* Add light

* Add lock

* Add number

* Add scene

* Add select

* Add sensor

* Add siren

* Add state_vacuum

* Add subscription

* Add switch

* Add tag

* Add trigger

* Add missed tests

* Add another missed test

* Add device_tracker

* Remove commented out code

* Correct tests according comments

* Improve mqtt_mock_entry and recover tests

* Split fixtures with and without yaml setup

* Update fixtures manual_mqtt

* Update fixtures mqtt_json

* Fix test tasmota

* Update fixture mqtt_room

* Revert fixture changes, improve test

* re-add test
This commit is contained in:
Jan Bouwhuis 2022-06-02 14:24:46 +02:00 committed by GitHub
parent 756988fe20
commit 52561ce076
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
36 changed files with 3612 additions and 1692 deletions

View file

@ -72,7 +72,9 @@ DEFAULT_CONFIG = {
}
async def test_setting_sensor_value_via_mqtt_message(hass, mqtt_mock):
async def test_setting_sensor_value_via_mqtt_message(
hass, mqtt_mock_entry_with_yaml_config
):
"""Test the setting of the value via MQTT."""
assert await async_setup_component(
hass,
@ -87,6 +89,7 @@ async def test_setting_sensor_value_via_mqtt_message(hass, mqtt_mock):
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
async_fire_mqtt_message(hass, "test-topic", "100")
state = hass.states.get("sensor.test")
@ -122,7 +125,13 @@ async def test_setting_sensor_value_via_mqtt_message(hass, mqtt_mock):
],
)
async def test_setting_sensor_native_value_handling_via_mqtt_message(
hass, mqtt_mock, caplog, device_class, native_value, state_value, log
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
device_class,
native_value,
state_value,
log,
):
"""Test the setting of the value via MQTT."""
assert await async_setup_component(
@ -138,6 +147,7 @@ async def test_setting_sensor_native_value_handling_via_mqtt_message(
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
async_fire_mqtt_message(hass, "test-topic", native_value)
state = hass.states.get("sensor.test")
@ -147,7 +157,9 @@ async def test_setting_sensor_native_value_handling_via_mqtt_message(
assert log == ("Invalid state message" in caplog.text)
async def test_setting_sensor_value_expires_availability_topic(hass, mqtt_mock, caplog):
async def test_setting_sensor_value_expires_availability_topic(
hass, mqtt_mock_entry_with_yaml_config, caplog
):
"""Test the expiration of the value."""
assert await async_setup_component(
hass,
@ -164,6 +176,7 @@ async def test_setting_sensor_value_expires_availability_topic(hass, mqtt_mock,
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
state = hass.states.get("sensor.test")
assert state.state == STATE_UNAVAILABLE
@ -174,10 +187,12 @@ async def test_setting_sensor_value_expires_availability_topic(hass, mqtt_mock,
state = hass.states.get("sensor.test")
assert state.state == STATE_UNAVAILABLE
await expires_helper(hass, mqtt_mock, caplog)
await expires_helper(hass, caplog)
async def test_setting_sensor_value_expires(hass, mqtt_mock, caplog):
async def test_setting_sensor_value_expires(
hass, mqtt_mock_entry_with_yaml_config, caplog
):
"""Test the expiration of the value."""
assert await async_setup_component(
hass,
@ -194,15 +209,16 @@ async def test_setting_sensor_value_expires(hass, mqtt_mock, caplog):
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
# State should be unavailable since expire_after is defined and > 0
state = hass.states.get("sensor.test")
assert state.state == STATE_UNAVAILABLE
await expires_helper(hass, mqtt_mock, caplog)
await expires_helper(hass, caplog)
async def expires_helper(hass, mqtt_mock, caplog):
async def expires_helper(hass, caplog):
"""Run the basic expiry code."""
realnow = dt_util.utcnow()
now = datetime(realnow.year + 1, 1, 1, 1, tzinfo=dt_util.UTC)
@ -253,7 +269,9 @@ async def expires_helper(hass, mqtt_mock, caplog):
assert state.state == STATE_UNAVAILABLE
async def test_setting_sensor_value_via_mqtt_json_message(hass, mqtt_mock):
async def test_setting_sensor_value_via_mqtt_json_message(
hass, mqtt_mock_entry_with_yaml_config
):
"""Test the setting of the value via MQTT with JSON payload."""
assert await async_setup_component(
hass,
@ -269,6 +287,7 @@ async def test_setting_sensor_value_via_mqtt_json_message(hass, mqtt_mock):
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
async_fire_mqtt_message(hass, "test-topic", '{ "val": "100" }')
state = hass.states.get("sensor.test")
@ -277,7 +296,7 @@ async def test_setting_sensor_value_via_mqtt_json_message(hass, mqtt_mock):
async def test_setting_sensor_value_via_mqtt_json_message_and_default_current_state(
hass, mqtt_mock
hass, mqtt_mock_entry_with_yaml_config
):
"""Test the setting of the value via MQTT with fall back to current state."""
assert await async_setup_component(
@ -294,6 +313,7 @@ async def test_setting_sensor_value_via_mqtt_json_message_and_default_current_st
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
async_fire_mqtt_message(
hass, "test-topic", '{ "val": "valcontent", "par": "parcontent" }'
@ -308,7 +328,9 @@ async def test_setting_sensor_value_via_mqtt_json_message_and_default_current_st
assert state.state == "valcontent-parcontent"
async def test_setting_sensor_last_reset_via_mqtt_message(hass, mqtt_mock, caplog):
async def test_setting_sensor_last_reset_via_mqtt_message(
hass, mqtt_mock_entry_with_yaml_config, caplog
):
"""Test the setting of the last_reset property via MQTT."""
assert await async_setup_component(
hass,
@ -325,6 +347,7 @@ async def test_setting_sensor_last_reset_via_mqtt_message(hass, mqtt_mock, caplo
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
async_fire_mqtt_message(hass, "last-reset-topic", "2020-01-02 08:11:00")
state = hass.states.get("sensor.test")
@ -338,7 +361,7 @@ async def test_setting_sensor_last_reset_via_mqtt_message(hass, mqtt_mock, caplo
@pytest.mark.parametrize("datestring", ["2020-21-02 08:11:00", "Hello there!"])
async def test_setting_sensor_bad_last_reset_via_mqtt_message(
hass, caplog, datestring, mqtt_mock
hass, caplog, datestring, mqtt_mock_entry_with_yaml_config
):
"""Test the setting of the last_reset property via MQTT."""
assert await async_setup_component(
@ -356,6 +379,7 @@ async def test_setting_sensor_bad_last_reset_via_mqtt_message(
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
async_fire_mqtt_message(hass, "last-reset-topic", datestring)
state = hass.states.get("sensor.test")
@ -364,7 +388,7 @@ async def test_setting_sensor_bad_last_reset_via_mqtt_message(
async def test_setting_sensor_empty_last_reset_via_mqtt_message(
hass, caplog, mqtt_mock
hass, caplog, mqtt_mock_entry_with_yaml_config
):
"""Test the setting of the last_reset property via MQTT."""
assert await async_setup_component(
@ -382,6 +406,7 @@ async def test_setting_sensor_empty_last_reset_via_mqtt_message(
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
async_fire_mqtt_message(hass, "last-reset-topic", "")
state = hass.states.get("sensor.test")
@ -389,7 +414,9 @@ async def test_setting_sensor_empty_last_reset_via_mqtt_message(
assert "Ignoring empty last_reset message" in caplog.text
async def test_setting_sensor_last_reset_via_mqtt_json_message(hass, mqtt_mock):
async def test_setting_sensor_last_reset_via_mqtt_json_message(
hass, mqtt_mock_entry_with_yaml_config
):
"""Test the setting of the value via MQTT with JSON payload."""
assert await async_setup_component(
hass,
@ -407,6 +434,7 @@ async def test_setting_sensor_last_reset_via_mqtt_json_message(hass, mqtt_mock):
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
async_fire_mqtt_message(
hass, "last-reset-topic", '{ "last_reset": "2020-01-02 08:11:00" }'
@ -417,7 +445,7 @@ async def test_setting_sensor_last_reset_via_mqtt_json_message(hass, mqtt_mock):
@pytest.mark.parametrize("extra", [{}, {"last_reset_topic": "test-topic"}])
async def test_setting_sensor_last_reset_via_mqtt_json_message_2(
hass, mqtt_mock, caplog, extra
hass, mqtt_mock_entry_with_yaml_config, caplog, extra
):
"""Test the setting of the value via MQTT with JSON payload."""
assert await async_setup_component(
@ -439,6 +467,7 @@ async def test_setting_sensor_last_reset_via_mqtt_json_message_2(
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
async_fire_mqtt_message(
hass,
@ -455,7 +484,7 @@ async def test_setting_sensor_last_reset_via_mqtt_json_message_2(
)
async def test_force_update_disabled(hass, mqtt_mock):
async def test_force_update_disabled(hass, mqtt_mock_entry_with_yaml_config):
"""Test force update option."""
assert await async_setup_component(
hass,
@ -470,6 +499,7 @@ async def test_force_update_disabled(hass, mqtt_mock):
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
events = []
@ -488,7 +518,7 @@ async def test_force_update_disabled(hass, mqtt_mock):
assert len(events) == 1
async def test_force_update_enabled(hass, mqtt_mock):
async def test_force_update_enabled(hass, mqtt_mock_entry_with_yaml_config):
"""Test force update option."""
assert await async_setup_component(
hass,
@ -504,6 +534,7 @@ async def test_force_update_enabled(hass, mqtt_mock):
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
events = []
@ -522,70 +553,80 @@ async def test_force_update_enabled(hass, mqtt_mock):
assert len(events) == 2
async def test_availability_when_connection_lost(hass, mqtt_mock):
async def test_availability_when_connection_lost(
hass, mqtt_mock_entry_with_yaml_config
):
"""Test availability after MQTT disconnection."""
await help_test_availability_when_connection_lost(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_availability_without_topic(hass, mqtt_mock):
async def test_availability_without_topic(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability without defined availability topic."""
await help_test_availability_without_topic(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_default_availability_payload(hass, mqtt_mock):
async def test_default_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability by default payload with defined topic."""
await help_test_default_availability_payload(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_default_availability_list_payload(hass, mqtt_mock):
async def test_default_availability_list_payload(
hass, mqtt_mock_entry_with_yaml_config
):
"""Test availability by default payload with defined topic."""
await help_test_default_availability_list_payload(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_default_availability_list_payload_all(hass, mqtt_mock):
async def test_default_availability_list_payload_all(
hass, mqtt_mock_entry_with_yaml_config
):
"""Test availability by default payload with defined topic."""
await help_test_default_availability_list_payload_all(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_default_availability_list_payload_any(hass, mqtt_mock):
async def test_default_availability_list_payload_any(
hass, mqtt_mock_entry_with_yaml_config
):
"""Test availability by default payload with defined topic."""
await help_test_default_availability_list_payload_any(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_default_availability_list_single(hass, mqtt_mock, caplog):
async def test_default_availability_list_single(
hass, mqtt_mock_entry_no_yaml_config, caplog
):
"""Test availability list and availability_topic are mutually exclusive."""
await help_test_default_availability_list_single(
hass, mqtt_mock, caplog, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, caplog, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_custom_availability_payload(hass, mqtt_mock):
async def test_custom_availability_payload(hass, mqtt_mock_entry_with_yaml_config):
"""Test availability by custom payload with defined topic."""
await help_test_custom_availability_payload(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_discovery_update_availability(hass, mqtt_mock):
async def test_discovery_update_availability(hass, mqtt_mock_entry_no_yaml_config):
"""Test availability discovery update."""
await help_test_discovery_update_availability(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_invalid_device_class(hass, mqtt_mock):
async def test_invalid_device_class(hass, mqtt_mock_entry_no_yaml_config):
"""Test device_class option with invalid value."""
assert await async_setup_component(
hass,
@ -600,12 +641,13 @@ async def test_invalid_device_class(hass, mqtt_mock):
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_no_yaml_config()
state = hass.states.get("sensor.test")
assert state is None
async def test_valid_device_class(hass, mqtt_mock):
async def test_valid_device_class(hass, mqtt_mock_entry_with_yaml_config):
"""Test device_class option with valid values."""
assert await async_setup_component(
hass,
@ -623,6 +665,7 @@ async def test_valid_device_class(hass, mqtt_mock):
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
state = hass.states.get("sensor.test_1")
assert state.attributes["device_class"] == "temperature"
@ -630,7 +673,7 @@ async def test_valid_device_class(hass, mqtt_mock):
assert "device_class" not in state.attributes
async def test_invalid_state_class(hass, mqtt_mock):
async def test_invalid_state_class(hass, mqtt_mock_entry_no_yaml_config):
"""Test state_class option with invalid value."""
assert await async_setup_component(
hass,
@ -645,12 +688,13 @@ async def test_invalid_state_class(hass, mqtt_mock):
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_no_yaml_config()
state = hass.states.get("sensor.test")
assert state is None
async def test_valid_state_class(hass, mqtt_mock):
async def test_valid_state_class(hass, mqtt_mock_entry_with_yaml_config):
"""Test state_class option with valid values."""
assert await async_setup_component(
hass,
@ -668,6 +712,7 @@ async def test_valid_state_class(hass, mqtt_mock):
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
state = hass.states.get("sensor.test_1")
assert state.attributes["state_class"] == "measurement"
@ -675,49 +720,61 @@ async def test_valid_state_class(hass, mqtt_mock):
assert "state_class" not in state.attributes
async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
async def test_setting_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry_with_yaml_config
):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_via_mqtt_json_message(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_setting_blocked_attribute_via_mqtt_json_message(hass, mqtt_mock):
async def test_setting_blocked_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry_no_yaml_config
):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_blocked_attribute_via_mqtt_json_message(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG, MQTT_SENSOR_ATTRIBUTES_BLOCKED
hass,
mqtt_mock_entry_no_yaml_config,
sensor.DOMAIN,
DEFAULT_CONFIG,
MQTT_SENSOR_ATTRIBUTES_BLOCKED,
)
async def test_setting_attribute_with_template(hass, mqtt_mock):
async def test_setting_attribute_with_template(hass, mqtt_mock_entry_with_yaml_config):
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_with_template(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
async def test_update_with_json_attrs_not_dict(
hass, mqtt_mock_entry_with_yaml_config, 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
hass, mqtt_mock_entry_with_yaml_config, caplog, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
async def test_update_with_json_attrs_bad_JSON(
hass, mqtt_mock_entry_with_yaml_config, 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
hass, mqtt_mock_entry_with_yaml_config, caplog, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_discovery_update_attr(hass, mqtt_mock, caplog):
async def test_discovery_update_attr(hass, mqtt_mock_entry_no_yaml_config, caplog):
"""Test update of discovered MQTTAttributes."""
await help_test_discovery_update_attr(
hass, mqtt_mock, caplog, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, caplog, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_unique_id(hass, mqtt_mock):
async def test_unique_id(hass, mqtt_mock_entry_with_yaml_config):
"""Test unique id option only creates one sensor per unique_id."""
config = {
sensor.DOMAIN: [
@ -735,16 +792,22 @@ async def test_unique_id(hass, mqtt_mock):
},
]
}
await help_test_unique_id(hass, mqtt_mock, sensor.DOMAIN, config)
await help_test_unique_id(
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, config
)
async def test_discovery_removal_sensor(hass, mqtt_mock, caplog):
async def test_discovery_removal_sensor(hass, mqtt_mock_entry_no_yaml_config, caplog):
"""Test removal of discovered sensor."""
data = '{ "name": "test", "state_topic": "test_topic" }'
await help_test_discovery_removal(hass, mqtt_mock, caplog, sensor.DOMAIN, data)
await help_test_discovery_removal(
hass, mqtt_mock_entry_no_yaml_config, caplog, sensor.DOMAIN, data
)
async def test_discovery_update_sensor_topic_template(hass, mqtt_mock, caplog):
async def test_discovery_update_sensor_topic_template(
hass, mqtt_mock_entry_no_yaml_config, caplog
):
"""Test update of discovered sensor."""
config = {"name": "test", "state_topic": "test_topic"}
config1 = copy.deepcopy(config)
@ -767,7 +830,7 @@ async def test_discovery_update_sensor_topic_template(hass, mqtt_mock, caplog):
await help_test_discovery_update(
hass,
mqtt_mock,
mqtt_mock_entry_no_yaml_config,
caplog,
sensor.DOMAIN,
config1,
@ -777,7 +840,9 @@ async def test_discovery_update_sensor_topic_template(hass, mqtt_mock, caplog):
)
async def test_discovery_update_sensor_template(hass, mqtt_mock, caplog):
async def test_discovery_update_sensor_template(
hass, mqtt_mock_entry_no_yaml_config, caplog
):
"""Test update of discovered sensor."""
config = {"name": "test", "state_topic": "test_topic"}
config1 = copy.deepcopy(config)
@ -798,7 +863,7 @@ async def test_discovery_update_sensor_template(hass, mqtt_mock, caplog):
await help_test_discovery_update(
hass,
mqtt_mock,
mqtt_mock_entry_no_yaml_config,
caplog,
sensor.DOMAIN,
config1,
@ -808,71 +873,79 @@ async def test_discovery_update_sensor_template(hass, mqtt_mock, caplog):
)
async def test_discovery_update_unchanged_sensor(hass, mqtt_mock, caplog):
async def test_discovery_update_unchanged_sensor(
hass, mqtt_mock_entry_no_yaml_config, caplog
):
"""Test update of discovered sensor."""
data1 = '{ "name": "Beer", "state_topic": "test_topic" }'
with patch(
"homeassistant.components.mqtt.sensor.MqttSensor.discovery_update"
) as discovery_update:
await help_test_discovery_update_unchanged(
hass, mqtt_mock, caplog, sensor.DOMAIN, data1, discovery_update
hass,
mqtt_mock_entry_no_yaml_config,
caplog,
sensor.DOMAIN,
data1,
discovery_update,
)
@pytest.mark.no_fail_on_log_exception
async def test_discovery_broken(hass, mqtt_mock, caplog):
async def test_discovery_broken(hass, mqtt_mock_entry_no_yaml_config, caplog):
"""Test handling of bad discovery message."""
data1 = '{ "name": "Beer", "state_topic": "test_topic#" }'
data2 = '{ "name": "Milk", "state_topic": "test_topic" }'
await help_test_discovery_broken(
hass, mqtt_mock, caplog, sensor.DOMAIN, data1, data2
hass, mqtt_mock_entry_no_yaml_config, caplog, sensor.DOMAIN, data1, data2
)
async def test_entity_device_info_with_connection(hass, mqtt_mock):
async def test_entity_device_info_with_connection(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT sensor device registry integration."""
await help_test_entity_device_info_with_connection(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_device_info_with_identifier(hass, mqtt_mock):
async def test_entity_device_info_with_identifier(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT sensor device registry integration."""
await help_test_entity_device_info_with_identifier(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_device_info_update(hass, mqtt_mock):
async def test_entity_device_info_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry update."""
await help_test_entity_device_info_update(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_device_info_remove(hass, mqtt_mock):
async def test_entity_device_info_remove(hass, mqtt_mock_entry_no_yaml_config):
"""Test device registry remove."""
await help_test_entity_device_info_remove(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_id_update_subscriptions(hass, mqtt_mock):
async def test_entity_id_update_subscriptions(hass, mqtt_mock_entry_with_yaml_config):
"""Test MQTT subscriptions are managed when entity_id is updated."""
await help_test_entity_id_update_subscriptions(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_with_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_id_update_discovery_update(hass, mqtt_mock):
async def test_entity_id_update_discovery_update(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT discovery update when entity_id is updated."""
await help_test_entity_id_update_discovery_update(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_device_info_with_hub(hass, mqtt_mock):
async def test_entity_device_info_with_hub(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT sensor device registry integration."""
await mqtt_mock_entry_no_yaml_config()
registry = dr.async_get(hass)
hub = registry.async_get_or_create(
config_entry_id="123",
@ -899,53 +972,57 @@ async def test_entity_device_info_with_hub(hass, mqtt_mock):
assert device.via_device_id == hub.id
async def test_entity_debug_info(hass, mqtt_mock):
async def test_entity_debug_info(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT sensor debug info."""
await help_test_entity_debug_info(hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG)
await help_test_entity_debug_info(
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_debug_info_max_messages(hass, mqtt_mock):
async def test_entity_debug_info_max_messages(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT sensor debug info."""
await help_test_entity_debug_info_max_messages(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_debug_info_message(hass, mqtt_mock):
async def test_entity_debug_info_message(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT debug info."""
await help_test_entity_debug_info_message(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG, None
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG, None
)
async def test_entity_debug_info_remove(hass, mqtt_mock):
async def test_entity_debug_info_remove(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT sensor debug info."""
await help_test_entity_debug_info_remove(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_debug_info_update_entity_id(hass, mqtt_mock):
async def test_entity_debug_info_update_entity_id(hass, mqtt_mock_entry_no_yaml_config):
"""Test MQTT sensor debug info."""
await help_test_entity_debug_info_update_entity_id(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_disabled_by_default(hass, mqtt_mock):
async def test_entity_disabled_by_default(hass, mqtt_mock_entry_no_yaml_config):
"""Test entity disabled by default."""
await help_test_entity_disabled_by_default(
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
@pytest.mark.no_fail_on_log_exception
async def test_entity_category(hass, mqtt_mock):
async def test_entity_category(hass, mqtt_mock_entry_no_yaml_config):
"""Test entity category."""
await help_test_entity_category(hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG)
await help_test_entity_category(
hass, mqtt_mock_entry_no_yaml_config, sensor.DOMAIN, DEFAULT_CONFIG
)
async def test_value_template_with_entity_id(hass, mqtt_mock):
async def test_value_template_with_entity_id(hass, mqtt_mock_entry_with_yaml_config):
"""Test the access to attributes in value_template via the entity_id."""
assert await async_setup_component(
hass,
@ -966,6 +1043,7 @@ async def test_value_template_with_entity_id(hass, mqtt_mock):
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
async_fire_mqtt_message(hass, "test-topic", "100")
state = hass.states.get("sensor.test")
@ -973,11 +1051,13 @@ async def test_value_template_with_entity_id(hass, mqtt_mock):
assert state.state == "101"
async def test_reloadable(hass, mqtt_mock, caplog, tmp_path):
async def test_reloadable(hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path):
"""Test reloading the MQTT platform."""
domain = sensor.DOMAIN
config = DEFAULT_CONFIG[domain]
await help_test_reloadable(hass, mqtt_mock, caplog, tmp_path, domain, config)
await help_test_reloadable(
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, domain, config
)
async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path):
@ -988,7 +1068,7 @@ async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path):
async def test_cleanup_triggers_and_restoring_state(
hass, mqtt_mock, caplog, tmp_path, freezer
hass, mqtt_mock_entry_with_yaml_config, caplog, tmp_path, freezer
):
"""Test cleanup old triggers at reloading and restoring the state."""
domain = sensor.DOMAIN
@ -1014,6 +1094,7 @@ async def test_cleanup_triggers_and_restoring_state(
{domain: [config1, config2]},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
async_fire_mqtt_message(hass, "test-topic1", "100")
state = hass.states.get("sensor.test1")
assert state.state == "38" # 100 °F -> 38 °C
@ -1053,7 +1134,7 @@ async def test_cleanup_triggers_and_restoring_state(
async def test_skip_restoring_state_with_over_due_expire_trigger(
hass, mqtt_mock, caplog, freezer
hass, mqtt_mock_entry_with_yaml_config, caplog, freezer
):
"""Test restoring a state with over due expire timer."""
@ -1081,6 +1162,7 @@ async def test_skip_restoring_state_with_over_due_expire_trigger(
):
assert await async_setup_component(hass, domain, {domain: config3})
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
assert "Skip state recovery after reload for sensor.test3" in caplog.text
@ -1092,12 +1174,18 @@ async def test_skip_restoring_state_with_over_due_expire_trigger(
],
)
async def test_encoding_subscribable_topics(
hass, mqtt_mock, caplog, topic, value, attribute, attribute_value
hass,
mqtt_mock_entry_with_yaml_config,
caplog,
topic,
value,
attribute,
attribute_value,
):
"""Test handling of incoming encoded payload."""
await help_test_encoding_subscribable_topics(
hass,
mqtt_mock,
mqtt_mock_entry_with_yaml_config,
caplog,
sensor.DOMAIN,
DEFAULT_CONFIG[sensor.DOMAIN],