From c55505b47b108142f3625326645c065496f7418b Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 25 Aug 2022 09:27:38 +0200 Subject: [PATCH] Use mock_restore_cache in mqtt tests (#77297) --- tests/components/mqtt/test_binary_sensor.py | 8 +- tests/components/mqtt/test_init.py | 38 ++++---- tests/components/mqtt/test_light.py | 13 +-- tests/components/mqtt/test_light_json.py | 92 +++++++++----------- tests/components/mqtt/test_light_template.py | 12 +-- tests/components/mqtt/test_scene.py | 33 ++++--- tests/components/mqtt/test_select.py | 74 ++++++++-------- tests/components/mqtt/test_sensor.py | 13 +-- tests/components/mqtt/test_switch.py | 39 ++++----- 9 files changed, 152 insertions(+), 170 deletions(-) diff --git a/tests/components/mqtt/test_binary_sensor.py b/tests/components/mqtt/test_binary_sensor.py index 658af79f20f..7d4edace988 100644 --- a/tests/components/mqtt/test_binary_sensor.py +++ b/tests/components/mqtt/test_binary_sensor.py @@ -53,6 +53,7 @@ from tests.common import ( assert_setup_component, async_fire_mqtt_message, async_fire_time_changed, + mock_restore_cache, ) DEFAULT_CONFIG = { @@ -1062,10 +1063,9 @@ async def test_skip_restoring_state_with_over_due_expire_trigger( {}, last_changed=datetime.fromisoformat("2022-02-02 12:01:35+01:00"), ) - with patch( - "homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state", - return_value=fake_state, - ), assert_setup_component(1, domain): + mock_restore_cache(hass, (fake_state,)) + + with assert_setup_component(1, domain): assert await async_setup_component(hass, domain, {domain: config3}) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index fd77dcec3a7..020fae1732b 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -43,6 +43,7 @@ from tests.common import ( async_fire_time_changed, mock_device_registry, mock_registry, + mock_restore_cache, ) from tests.testing_config.custom_components.test.sensor import DEVICE_CLASSES @@ -284,27 +285,24 @@ async def test_command_template_variables(hass, mqtt_mock_entry_with_yaml_config """Test the rendering of entity variables.""" topic = "test/select" - fake_state = ha.State("select.test", "milk") + fake_state = ha.State("select.test_select", "milk") + mock_restore_cache(hass, (fake_state,)) - with patch( - "homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state", - return_value=fake_state, - ): - assert await async_setup_component( - hass, - "select", - { - "select": { - "platform": "mqtt", - "command_topic": topic, - "name": "Test Select", - "options": ["milk", "beer"], - "command_template": '{"option": "{{ value }}", "entity_id": "{{ entity_id }}", "name": "{{ name }}", "this_object_state": "{{ this.state }}"}', - } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + assert await async_setup_component( + hass, + "select", + { + "select": { + "platform": "mqtt", + "command_topic": topic, + "name": "Test Select", + "options": ["milk", "beer"], + "command_template": '{"option": "{{ value }}", "entity_id": "{{ entity_id }}", "name": "{{ name }}", "this_object_state": "{{ this.state }}"}', + } + }, + ) + await hass.async_block_till_done() + mqtt_mock = await mqtt_mock_entry_with_yaml_config() state = hass.states.get("select.test_select") assert state.state == "milk" diff --git a/tests/components/mqtt/test_light.py b/tests/components/mqtt/test_light.py index ff529b3dda4..e916276cb4a 100644 --- a/tests/components/mqtt/test_light.py +++ b/tests/components/mqtt/test_light.py @@ -226,7 +226,11 @@ from .test_common import ( help_test_update_with_json_attrs_not_dict, ) -from tests.common import assert_setup_component, async_fire_mqtt_message +from tests.common import ( + assert_setup_component, + async_fire_mqtt_message, + mock_restore_cache, +) from tests.components.light import common DEFAULT_CONFIG = { @@ -792,10 +796,9 @@ async def test_sending_mqtt_commands_and_optimistic( "color_mode": "hs", }, ) - with patch( - "homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state", - return_value=fake_state, - ), assert_setup_component(1, light.DOMAIN): + mock_restore_cache(hass, (fake_state,)) + + with assert_setup_component(1, light.DOMAIN): assert await async_setup_component(hass, light.DOMAIN, config) await hass.async_block_till_done() mqtt_mock = await mqtt_mock_entry_with_yaml_config() diff --git a/tests/components/mqtt/test_light_json.py b/tests/components/mqtt/test_light_json.py index 89e966112c1..8fac9092e3f 100644 --- a/tests/components/mqtt/test_light_json.py +++ b/tests/components/mqtt/test_light_json.py @@ -124,7 +124,7 @@ from .test_common import ( help_test_update_with_json_attrs_not_dict, ) -from tests.common import async_fire_mqtt_message +from tests.common import async_fire_mqtt_message, mock_restore_cache from tests.components.light import common DEFAULT_CONFIG = { @@ -614,32 +614,29 @@ async def test_sending_mqtt_commands_and_optimistic( "color_temp": 100, }, ) + mock_restore_cache(hass, (fake_state,)) - with patch( - "homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state", - return_value=fake_state, - ): - assert await async_setup_component( - hass, - light.DOMAIN, - { - light.DOMAIN: { - "platform": "mqtt", - "schema": "json", - "name": "test", - "command_topic": "test_light_rgb/set", - "brightness": True, - "color_temp": True, - "effect": True, - "hs": True, - "rgb": True, - "xy": True, - "qos": 2, - } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + assert await async_setup_component( + hass, + light.DOMAIN, + { + light.DOMAIN: { + "platform": "mqtt", + "schema": "json", + "name": "test", + "command_topic": "test_light_rgb/set", + "brightness": True, + "color_temp": True, + "effect": True, + "hs": True, + "rgb": True, + "xy": True, + "qos": 2, + } + }, + ) + await hass.async_block_till_done() + mqtt_mock = await mqtt_mock_entry_with_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_ON @@ -754,30 +751,27 @@ async def test_sending_mqtt_commands_and_optimistic2( "hs_color": [100, 100], }, ) + mock_restore_cache(hass, (fake_state,)) - with patch( - "homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state", - return_value=fake_state, - ): - assert await async_setup_component( - hass, - light.DOMAIN, - { - light.DOMAIN: { - "brightness": True, - "color_mode": True, - "command_topic": "test_light_rgb/set", - "effect": True, - "name": "test", - "platform": "mqtt", - "qos": 2, - "schema": "json", - "supported_color_modes": supported_color_modes, - } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + assert await async_setup_component( + hass, + light.DOMAIN, + { + light.DOMAIN: { + "brightness": True, + "color_mode": True, + "command_topic": "test_light_rgb/set", + "effect": True, + "name": "test", + "platform": "mqtt", + "qos": 2, + "schema": "json", + "supported_color_modes": supported_color_modes, + } + }, + ) + await hass.async_block_till_done() + mqtt_mock = await mqtt_mock_entry_with_yaml_config() state = hass.states.get("light.test") assert state.state == STATE_ON diff --git a/tests/components/mqtt/test_light_template.py b/tests/components/mqtt/test_light_template.py index e4f755eab4e..3b330b1434d 100644 --- a/tests/components/mqtt/test_light_template.py +++ b/tests/components/mqtt/test_light_template.py @@ -74,7 +74,11 @@ from .test_common import ( help_test_update_with_json_attrs_not_dict, ) -from tests.common import assert_setup_component, async_fire_mqtt_message +from tests.common import ( + assert_setup_component, + async_fire_mqtt_message, + mock_restore_cache, +) from tests.components.light import common DEFAULT_CONFIG = { @@ -334,11 +338,9 @@ async def test_sending_mqtt_commands_and_optimistic( "color_temp": 100, }, ) + mock_restore_cache(hass, (fake_state,)) - with patch( - "homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state", - return_value=fake_state, - ), assert_setup_component(1, light.DOMAIN): + with assert_setup_component(1, light.DOMAIN): assert await async_setup_component( hass, light.DOMAIN, diff --git a/tests/components/mqtt/test_scene.py b/tests/components/mqtt/test_scene.py index 713410059fe..d676429bf3e 100644 --- a/tests/components/mqtt/test_scene.py +++ b/tests/components/mqtt/test_scene.py @@ -25,6 +25,8 @@ from .test_common import ( help_test_unload_config_entry_with_platform, ) +from tests.common import mock_restore_cache + DEFAULT_CONFIG = { scene.DOMAIN: { "platform": "mqtt", @@ -45,25 +47,22 @@ def scene_platform_only(): async def test_sending_mqtt_commands(hass, mqtt_mock_entry_with_yaml_config): """Test the sending MQTT commands.""" fake_state = ha.State("scene.test", STATE_UNKNOWN) + mock_restore_cache(hass, (fake_state,)) - with patch( - "homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state", - return_value=fake_state, - ): - assert await async_setup_component( - hass, - scene.DOMAIN, - { - scene.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "command-topic", - "payload_on": "beer on", - }, + assert await async_setup_component( + hass, + scene.DOMAIN, + { + scene.DOMAIN: { + "platform": "mqtt", + "name": "test", + "command_topic": "command-topic", + "payload_on": "beer on", }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + }, + ) + await hass.async_block_till_done() + mqtt_mock = await mqtt_mock_entry_with_yaml_config() state = hass.states.get("scene.test") assert state.state == STATE_UNKNOWN diff --git a/tests/components/mqtt/test_select.py b/tests/components/mqtt/test_select.py index 4c3a0523951..085c4d5df00 100644 --- a/tests/components/mqtt/test_select.py +++ b/tests/components/mqtt/test_select.py @@ -53,7 +53,7 @@ from .test_common import ( help_test_update_with_json_attrs_not_dict, ) -from tests.common import async_fire_mqtt_message +from tests.common import async_fire_mqtt_message, mock_restore_cache DEFAULT_CONFIG = { select.DOMAIN: { @@ -152,26 +152,23 @@ async def test_run_select_service_optimistic(hass, mqtt_mock_entry_with_yaml_con """Test that set_value service works in optimistic mode.""" topic = "test/select" - fake_state = ha.State("select.test", "milk") + fake_state = ha.State("select.test_select", "milk") + mock_restore_cache(hass, (fake_state,)) - with patch( - "homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state", - return_value=fake_state, - ): - assert await async_setup_component( - hass, - select.DOMAIN, - { - "select": { - "platform": "mqtt", - "command_topic": topic, - "name": "Test Select", - "options": ["milk", "beer"], - } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + assert await async_setup_component( + hass, + select.DOMAIN, + { + "select": { + "platform": "mqtt", + "command_topic": topic, + "name": "Test Select", + "options": ["milk", "beer"], + } + }, + ) + await hass.async_block_till_done() + mqtt_mock = await mqtt_mock_entry_with_yaml_config() state = hass.states.get("select.test_select") assert state.state == "milk" @@ -196,27 +193,24 @@ async def test_run_select_service_optimistic_with_command_template( """Test that set_value service works in optimistic mode and with a command_template.""" topic = "test/select" - fake_state = ha.State("select.test", "milk") + fake_state = ha.State("select.test_select", "milk") + mock_restore_cache(hass, (fake_state,)) - with patch( - "homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state", - return_value=fake_state, - ): - assert await async_setup_component( - hass, - select.DOMAIN, - { - "select": { - "platform": "mqtt", - "command_topic": topic, - "name": "Test Select", - "options": ["milk", "beer"], - "command_template": '{"option": "{{ value }}"}', - } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + assert await async_setup_component( + hass, + select.DOMAIN, + { + "select": { + "platform": "mqtt", + "command_topic": topic, + "name": "Test Select", + "options": ["milk", "beer"], + "command_template": '{"option": "{{ value }}"}', + } + }, + ) + await hass.async_block_till_done() + mqtt_mock = await mqtt_mock_entry_with_yaml_config() state = hass.states.get("select.test_select") assert state.state == "milk" diff --git a/tests/components/mqtt/test_sensor.py b/tests/components/mqtt/test_sensor.py index ab094c20b6f..b446b1a8b76 100644 --- a/tests/components/mqtt/test_sensor.py +++ b/tests/components/mqtt/test_sensor.py @@ -67,6 +67,7 @@ from tests.common import ( assert_setup_component, async_fire_mqtt_message, async_fire_time_changed, + mock_restore_cache_with_extra_data, ) DEFAULT_CONFIG = { @@ -1160,15 +1161,9 @@ async def test_skip_restoring_state_with_over_due_expire_trigger( last_changed=datetime.fromisoformat("2022-02-02 12:01:35+01:00"), ) fake_extra_data = MagicMock() - with patch( - "homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state", - return_value=fake_state, - ), patch( - "homeassistant.helpers.restore_state.RestoreEntity.async_get_last_extra_data", - return_value=fake_extra_data, - ), assert_setup_component( - 1, domain - ): + mock_restore_cache_with_extra_data(hass, ((fake_state, fake_extra_data),)) + + with assert_setup_component(1, domain): assert await async_setup_component(hass, domain, {domain: config3}) await hass.async_block_till_done() await mqtt_mock_entry_with_yaml_config() diff --git a/tests/components/mqtt/test_switch.py b/tests/components/mqtt/test_switch.py index af6c0f99f50..ac69b17e18e 100644 --- a/tests/components/mqtt/test_switch.py +++ b/tests/components/mqtt/test_switch.py @@ -47,7 +47,7 @@ from .test_common import ( help_test_update_with_json_attrs_not_dict, ) -from tests.common import async_fire_mqtt_message +from tests.common import async_fire_mqtt_message, mock_restore_cache from tests.components.switch import common DEFAULT_CONFIG = { @@ -108,27 +108,24 @@ async def test_sending_mqtt_commands_and_optimistic( ): """Test the sending MQTT commands in optimistic mode.""" fake_state = ha.State("switch.test", "on") + mock_restore_cache(hass, (fake_state,)) - with patch( - "homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state", - return_value=fake_state, - ): - assert await async_setup_component( - hass, - switch.DOMAIN, - { - switch.DOMAIN: { - "platform": "mqtt", - "name": "test", - "command_topic": "command-topic", - "payload_on": "beer on", - "payload_off": "beer off", - "qos": "2", - } - }, - ) - await hass.async_block_till_done() - mqtt_mock = await mqtt_mock_entry_with_yaml_config() + assert await async_setup_component( + hass, + switch.DOMAIN, + { + switch.DOMAIN: { + "platform": "mqtt", + "name": "test", + "command_topic": "command-topic", + "payload_on": "beer on", + "payload_off": "beer off", + "qos": "2", + } + }, + ) + await hass.async_block_till_done() + mqtt_mock = await mqtt_mock_entry_with_yaml_config() state = hass.states.get("switch.test") assert state.state == STATE_ON