Use mock_restore_cache in mqtt tests (#77297)

pull/77305/head
Erik Montnemery 2022-08-25 09:27:38 +02:00 committed by GitHub
parent cef6ffb552
commit c55505b47b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 152 additions and 170 deletions

View File

@ -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()

View File

@ -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"

View File

@ -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()

View File

@ -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

View File

@ -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,

View File

@ -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

View File

@ -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"

View File

@ -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()

View File

@ -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