Use mock_restore_cache in mqtt tests (#77297)
This commit is contained in:
parent
cef6ffb552
commit
c55505b47b
9 changed files with 152 additions and 170 deletions
|
@ -53,6 +53,7 @@ from tests.common import (
|
||||||
assert_setup_component,
|
assert_setup_component,
|
||||||
async_fire_mqtt_message,
|
async_fire_mqtt_message,
|
||||||
async_fire_time_changed,
|
async_fire_time_changed,
|
||||||
|
mock_restore_cache,
|
||||||
)
|
)
|
||||||
|
|
||||||
DEFAULT_CONFIG = {
|
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"),
|
last_changed=datetime.fromisoformat("2022-02-02 12:01:35+01:00"),
|
||||||
)
|
)
|
||||||
with patch(
|
mock_restore_cache(hass, (fake_state,))
|
||||||
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
|
|
||||||
return_value=fake_state,
|
with assert_setup_component(1, domain):
|
||||||
), assert_setup_component(1, domain):
|
|
||||||
assert await async_setup_component(hass, domain, {domain: config3})
|
assert await async_setup_component(hass, domain, {domain: config3})
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
await mqtt_mock_entry_with_yaml_config()
|
await mqtt_mock_entry_with_yaml_config()
|
||||||
|
|
|
@ -43,6 +43,7 @@ from tests.common import (
|
||||||
async_fire_time_changed,
|
async_fire_time_changed,
|
||||||
mock_device_registry,
|
mock_device_registry,
|
||||||
mock_registry,
|
mock_registry,
|
||||||
|
mock_restore_cache,
|
||||||
)
|
)
|
||||||
from tests.testing_config.custom_components.test.sensor import DEVICE_CLASSES
|
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."""
|
"""Test the rendering of entity variables."""
|
||||||
topic = "test/select"
|
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(
|
assert await async_setup_component(
|
||||||
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
|
hass,
|
||||||
return_value=fake_state,
|
"select",
|
||||||
):
|
{
|
||||||
assert await async_setup_component(
|
"select": {
|
||||||
hass,
|
"platform": "mqtt",
|
||||||
"select",
|
"command_topic": topic,
|
||||||
{
|
"name": "Test Select",
|
||||||
"select": {
|
"options": ["milk", "beer"],
|
||||||
"platform": "mqtt",
|
"command_template": '{"option": "{{ value }}", "entity_id": "{{ entity_id }}", "name": "{{ name }}", "this_object_state": "{{ this.state }}"}',
|
||||||
"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()
|
||||||
},
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
|
|
||||||
|
|
||||||
state = hass.states.get("select.test_select")
|
state = hass.states.get("select.test_select")
|
||||||
assert state.state == "milk"
|
assert state.state == "milk"
|
||||||
|
|
|
@ -226,7 +226,11 @@ from .test_common import (
|
||||||
help_test_update_with_json_attrs_not_dict,
|
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
|
from tests.components.light import common
|
||||||
|
|
||||||
DEFAULT_CONFIG = {
|
DEFAULT_CONFIG = {
|
||||||
|
@ -792,10 +796,9 @@ async def test_sending_mqtt_commands_and_optimistic(
|
||||||
"color_mode": "hs",
|
"color_mode": "hs",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
with patch(
|
mock_restore_cache(hass, (fake_state,))
|
||||||
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
|
|
||||||
return_value=fake_state,
|
with assert_setup_component(1, light.DOMAIN):
|
||||||
), assert_setup_component(1, light.DOMAIN):
|
|
||||||
assert await async_setup_component(hass, light.DOMAIN, config)
|
assert await async_setup_component(hass, light.DOMAIN, config)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
|
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
|
||||||
|
|
|
@ -124,7 +124,7 @@ from .test_common import (
|
||||||
help_test_update_with_json_attrs_not_dict,
|
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
|
from tests.components.light import common
|
||||||
|
|
||||||
DEFAULT_CONFIG = {
|
DEFAULT_CONFIG = {
|
||||||
|
@ -614,32 +614,29 @@ async def test_sending_mqtt_commands_and_optimistic(
|
||||||
"color_temp": 100,
|
"color_temp": 100,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
mock_restore_cache(hass, (fake_state,))
|
||||||
|
|
||||||
with patch(
|
assert await async_setup_component(
|
||||||
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
|
hass,
|
||||||
return_value=fake_state,
|
light.DOMAIN,
|
||||||
):
|
{
|
||||||
assert await async_setup_component(
|
light.DOMAIN: {
|
||||||
hass,
|
"platform": "mqtt",
|
||||||
light.DOMAIN,
|
"schema": "json",
|
||||||
{
|
"name": "test",
|
||||||
light.DOMAIN: {
|
"command_topic": "test_light_rgb/set",
|
||||||
"platform": "mqtt",
|
"brightness": True,
|
||||||
"schema": "json",
|
"color_temp": True,
|
||||||
"name": "test",
|
"effect": True,
|
||||||
"command_topic": "test_light_rgb/set",
|
"hs": True,
|
||||||
"brightness": True,
|
"rgb": True,
|
||||||
"color_temp": True,
|
"xy": True,
|
||||||
"effect": True,
|
"qos": 2,
|
||||||
"hs": True,
|
}
|
||||||
"rgb": True,
|
},
|
||||||
"xy": True,
|
)
|
||||||
"qos": 2,
|
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("light.test")
|
state = hass.states.get("light.test")
|
||||||
assert state.state == STATE_ON
|
assert state.state == STATE_ON
|
||||||
|
@ -754,30 +751,27 @@ async def test_sending_mqtt_commands_and_optimistic2(
|
||||||
"hs_color": [100, 100],
|
"hs_color": [100, 100],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
mock_restore_cache(hass, (fake_state,))
|
||||||
|
|
||||||
with patch(
|
assert await async_setup_component(
|
||||||
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
|
hass,
|
||||||
return_value=fake_state,
|
light.DOMAIN,
|
||||||
):
|
{
|
||||||
assert await async_setup_component(
|
light.DOMAIN: {
|
||||||
hass,
|
"brightness": True,
|
||||||
light.DOMAIN,
|
"color_mode": True,
|
||||||
{
|
"command_topic": "test_light_rgb/set",
|
||||||
light.DOMAIN: {
|
"effect": True,
|
||||||
"brightness": True,
|
"name": "test",
|
||||||
"color_mode": True,
|
"platform": "mqtt",
|
||||||
"command_topic": "test_light_rgb/set",
|
"qos": 2,
|
||||||
"effect": True,
|
"schema": "json",
|
||||||
"name": "test",
|
"supported_color_modes": supported_color_modes,
|
||||||
"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()
|
||||||
},
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
|
|
||||||
|
|
||||||
state = hass.states.get("light.test")
|
state = hass.states.get("light.test")
|
||||||
assert state.state == STATE_ON
|
assert state.state == STATE_ON
|
||||||
|
|
|
@ -74,7 +74,11 @@ from .test_common import (
|
||||||
help_test_update_with_json_attrs_not_dict,
|
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
|
from tests.components.light import common
|
||||||
|
|
||||||
DEFAULT_CONFIG = {
|
DEFAULT_CONFIG = {
|
||||||
|
@ -334,11 +338,9 @@ async def test_sending_mqtt_commands_and_optimistic(
|
||||||
"color_temp": 100,
|
"color_temp": 100,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
mock_restore_cache(hass, (fake_state,))
|
||||||
|
|
||||||
with patch(
|
with assert_setup_component(1, light.DOMAIN):
|
||||||
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
|
|
||||||
return_value=fake_state,
|
|
||||||
), assert_setup_component(1, light.DOMAIN):
|
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
light.DOMAIN,
|
light.DOMAIN,
|
||||||
|
|
|
@ -25,6 +25,8 @@ from .test_common import (
|
||||||
help_test_unload_config_entry_with_platform,
|
help_test_unload_config_entry_with_platform,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from tests.common import mock_restore_cache
|
||||||
|
|
||||||
DEFAULT_CONFIG = {
|
DEFAULT_CONFIG = {
|
||||||
scene.DOMAIN: {
|
scene.DOMAIN: {
|
||||||
"platform": "mqtt",
|
"platform": "mqtt",
|
||||||
|
@ -45,25 +47,22 @@ def scene_platform_only():
|
||||||
async def test_sending_mqtt_commands(hass, mqtt_mock_entry_with_yaml_config):
|
async def test_sending_mqtt_commands(hass, mqtt_mock_entry_with_yaml_config):
|
||||||
"""Test the sending MQTT commands."""
|
"""Test the sending MQTT commands."""
|
||||||
fake_state = ha.State("scene.test", STATE_UNKNOWN)
|
fake_state = ha.State("scene.test", STATE_UNKNOWN)
|
||||||
|
mock_restore_cache(hass, (fake_state,))
|
||||||
|
|
||||||
with patch(
|
assert await async_setup_component(
|
||||||
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
|
hass,
|
||||||
return_value=fake_state,
|
scene.DOMAIN,
|
||||||
):
|
{
|
||||||
assert await async_setup_component(
|
scene.DOMAIN: {
|
||||||
hass,
|
"platform": "mqtt",
|
||||||
scene.DOMAIN,
|
"name": "test",
|
||||||
{
|
"command_topic": "command-topic",
|
||||||
scene.DOMAIN: {
|
"payload_on": "beer on",
|
||||||
"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")
|
state = hass.states.get("scene.test")
|
||||||
assert state.state == STATE_UNKNOWN
|
assert state.state == STATE_UNKNOWN
|
||||||
|
|
|
@ -53,7 +53,7 @@ from .test_common import (
|
||||||
help_test_update_with_json_attrs_not_dict,
|
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 = {
|
DEFAULT_CONFIG = {
|
||||||
select.DOMAIN: {
|
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."""
|
"""Test that set_value service works in optimistic mode."""
|
||||||
topic = "test/select"
|
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(
|
assert await async_setup_component(
|
||||||
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
|
hass,
|
||||||
return_value=fake_state,
|
select.DOMAIN,
|
||||||
):
|
{
|
||||||
assert await async_setup_component(
|
"select": {
|
||||||
hass,
|
"platform": "mqtt",
|
||||||
select.DOMAIN,
|
"command_topic": topic,
|
||||||
{
|
"name": "Test Select",
|
||||||
"select": {
|
"options": ["milk", "beer"],
|
||||||
"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()
|
||||||
},
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
|
|
||||||
|
|
||||||
state = hass.states.get("select.test_select")
|
state = hass.states.get("select.test_select")
|
||||||
assert state.state == "milk"
|
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."""
|
"""Test that set_value service works in optimistic mode and with a command_template."""
|
||||||
topic = "test/select"
|
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(
|
assert await async_setup_component(
|
||||||
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
|
hass,
|
||||||
return_value=fake_state,
|
select.DOMAIN,
|
||||||
):
|
{
|
||||||
assert await async_setup_component(
|
"select": {
|
||||||
hass,
|
"platform": "mqtt",
|
||||||
select.DOMAIN,
|
"command_topic": topic,
|
||||||
{
|
"name": "Test Select",
|
||||||
"select": {
|
"options": ["milk", "beer"],
|
||||||
"platform": "mqtt",
|
"command_template": '{"option": "{{ value }}"}',
|
||||||
"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()
|
||||||
},
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
|
|
||||||
|
|
||||||
state = hass.states.get("select.test_select")
|
state = hass.states.get("select.test_select")
|
||||||
assert state.state == "milk"
|
assert state.state == "milk"
|
||||||
|
|
|
@ -67,6 +67,7 @@ from tests.common import (
|
||||||
assert_setup_component,
|
assert_setup_component,
|
||||||
async_fire_mqtt_message,
|
async_fire_mqtt_message,
|
||||||
async_fire_time_changed,
|
async_fire_time_changed,
|
||||||
|
mock_restore_cache_with_extra_data,
|
||||||
)
|
)
|
||||||
|
|
||||||
DEFAULT_CONFIG = {
|
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"),
|
last_changed=datetime.fromisoformat("2022-02-02 12:01:35+01:00"),
|
||||||
)
|
)
|
||||||
fake_extra_data = MagicMock()
|
fake_extra_data = MagicMock()
|
||||||
with patch(
|
mock_restore_cache_with_extra_data(hass, ((fake_state, fake_extra_data),))
|
||||||
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
|
|
||||||
return_value=fake_state,
|
with assert_setup_component(1, domain):
|
||||||
), patch(
|
|
||||||
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_extra_data",
|
|
||||||
return_value=fake_extra_data,
|
|
||||||
), assert_setup_component(
|
|
||||||
1, domain
|
|
||||||
):
|
|
||||||
assert await async_setup_component(hass, domain, {domain: config3})
|
assert await async_setup_component(hass, domain, {domain: config3})
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
await mqtt_mock_entry_with_yaml_config()
|
await mqtt_mock_entry_with_yaml_config()
|
||||||
|
|
|
@ -47,7 +47,7 @@ from .test_common import (
|
||||||
help_test_update_with_json_attrs_not_dict,
|
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
|
from tests.components.switch import common
|
||||||
|
|
||||||
DEFAULT_CONFIG = {
|
DEFAULT_CONFIG = {
|
||||||
|
@ -108,27 +108,24 @@ async def test_sending_mqtt_commands_and_optimistic(
|
||||||
):
|
):
|
||||||
"""Test the sending MQTT commands in optimistic mode."""
|
"""Test the sending MQTT commands in optimistic mode."""
|
||||||
fake_state = ha.State("switch.test", "on")
|
fake_state = ha.State("switch.test", "on")
|
||||||
|
mock_restore_cache(hass, (fake_state,))
|
||||||
|
|
||||||
with patch(
|
assert await async_setup_component(
|
||||||
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
|
hass,
|
||||||
return_value=fake_state,
|
switch.DOMAIN,
|
||||||
):
|
{
|
||||||
assert await async_setup_component(
|
switch.DOMAIN: {
|
||||||
hass,
|
"platform": "mqtt",
|
||||||
switch.DOMAIN,
|
"name": "test",
|
||||||
{
|
"command_topic": "command-topic",
|
||||||
switch.DOMAIN: {
|
"payload_on": "beer on",
|
||||||
"platform": "mqtt",
|
"payload_off": "beer off",
|
||||||
"name": "test",
|
"qos": "2",
|
||||||
"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()
|
||||||
},
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
|
|
||||||
|
|
||||||
state = hass.states.get("switch.test")
|
state = hass.states.get("switch.test")
|
||||||
assert state.state == STATE_ON
|
assert state.state == STATE_ON
|
||||||
|
|
Loading…
Add table
Reference in a new issue