mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Use mock_restore_cache in tests (#77298)
This commit is contained in:
parent
c55505b47b
commit
3d723bddf8
@ -1,6 +1,5 @@
|
|||||||
"""Test KNX binary sensor."""
|
"""Test KNX binary sensor."""
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from unittest.mock import patch
|
|
||||||
|
|
||||||
from homeassistant.components.knx.const import CONF_STATE_ADDRESS, CONF_SYNC_STATE
|
from homeassistant.components.knx.const import CONF_STATE_ADDRESS, CONF_SYNC_STATE
|
||||||
from homeassistant.components.knx.schema import BinarySensorSchema
|
from homeassistant.components.knx.schema import BinarySensorSchema
|
||||||
@ -12,7 +11,11 @@ from homeassistant.util import dt
|
|||||||
|
|
||||||
from .conftest import KNXTestKit
|
from .conftest import KNXTestKit
|
||||||
|
|
||||||
from tests.common import async_capture_events, async_fire_time_changed
|
from tests.common import (
|
||||||
|
async_capture_events,
|
||||||
|
async_fire_time_changed,
|
||||||
|
mock_restore_cache,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_binary_sensor_entity_category(hass: HomeAssistant, knx: KNXTestKit):
|
async def test_binary_sensor_entity_category(hass: HomeAssistant, knx: KNXTestKit):
|
||||||
@ -245,22 +248,19 @@ async def test_binary_sensor_restore_and_respond(hass, knx):
|
|||||||
"""Test restoring KNX binary sensor state and respond to read."""
|
"""Test restoring KNX binary sensor state and respond to read."""
|
||||||
_ADDRESS = "2/2/2"
|
_ADDRESS = "2/2/2"
|
||||||
fake_state = State("binary_sensor.test", STATE_ON)
|
fake_state = State("binary_sensor.test", STATE_ON)
|
||||||
|
mock_restore_cache(hass, (fake_state,))
|
||||||
|
|
||||||
with patch(
|
await knx.setup_integration(
|
||||||
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
|
{
|
||||||
return_value=fake_state,
|
BinarySensorSchema.PLATFORM: [
|
||||||
):
|
{
|
||||||
await knx.setup_integration(
|
CONF_NAME: "test",
|
||||||
{
|
CONF_STATE_ADDRESS: _ADDRESS,
|
||||||
BinarySensorSchema.PLATFORM: [
|
CONF_SYNC_STATE: False,
|
||||||
{
|
},
|
||||||
CONF_NAME: "test",
|
]
|
||||||
CONF_STATE_ADDRESS: _ADDRESS,
|
}
|
||||||
CONF_SYNC_STATE: False,
|
)
|
||||||
},
|
|
||||||
]
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
# restored state - doesn't send telegram
|
# restored state - doesn't send telegram
|
||||||
state = hass.states.get("binary_sensor.test")
|
state = hass.states.get("binary_sensor.test")
|
||||||
@ -277,23 +277,20 @@ async def test_binary_sensor_restore_invert(hass, knx):
|
|||||||
"""Test restoring KNX binary sensor state with invert."""
|
"""Test restoring KNX binary sensor state with invert."""
|
||||||
_ADDRESS = "2/2/2"
|
_ADDRESS = "2/2/2"
|
||||||
fake_state = State("binary_sensor.test", STATE_ON)
|
fake_state = State("binary_sensor.test", STATE_ON)
|
||||||
|
mock_restore_cache(hass, (fake_state,))
|
||||||
|
|
||||||
with patch(
|
await knx.setup_integration(
|
||||||
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
|
{
|
||||||
return_value=fake_state,
|
BinarySensorSchema.PLATFORM: [
|
||||||
):
|
{
|
||||||
await knx.setup_integration(
|
CONF_NAME: "test",
|
||||||
{
|
CONF_STATE_ADDRESS: _ADDRESS,
|
||||||
BinarySensorSchema.PLATFORM: [
|
BinarySensorSchema.CONF_INVERT: True,
|
||||||
{
|
CONF_SYNC_STATE: False,
|
||||||
CONF_NAME: "test",
|
},
|
||||||
CONF_STATE_ADDRESS: _ADDRESS,
|
]
|
||||||
BinarySensorSchema.CONF_INVERT: True,
|
}
|
||||||
CONF_SYNC_STATE: False,
|
)
|
||||||
},
|
|
||||||
]
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
# restored state - doesn't send telegram
|
# restored state - doesn't send telegram
|
||||||
state = hass.states.get("binary_sensor.test")
|
state = hass.states.get("binary_sensor.test")
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
"""Test KNX select."""
|
"""Test KNX select."""
|
||||||
from unittest.mock import patch
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.knx.const import (
|
from homeassistant.components.knx.const import (
|
||||||
@ -17,6 +15,8 @@ from homeassistant.core import HomeAssistant, State
|
|||||||
|
|
||||||
from .conftest import KNXTestKit
|
from .conftest import KNXTestKit
|
||||||
|
|
||||||
|
from tests.common import mock_restore_cache
|
||||||
|
|
||||||
|
|
||||||
async def test_select_dpt_2_simple(hass: HomeAssistant, knx: KNXTestKit):
|
async def test_select_dpt_2_simple(hass: HomeAssistant, knx: KNXTestKit):
|
||||||
"""Test simple KNX select."""
|
"""Test simple KNX select."""
|
||||||
@ -98,22 +98,19 @@ async def test_select_dpt_2_restore(hass: HomeAssistant, knx: KNXTestKit):
|
|||||||
test_address = "1/1/1"
|
test_address = "1/1/1"
|
||||||
test_passive_address = "3/3/3"
|
test_passive_address = "3/3/3"
|
||||||
fake_state = State("select.test", "Control - On")
|
fake_state = State("select.test", "Control - On")
|
||||||
|
mock_restore_cache(hass, (fake_state,))
|
||||||
|
|
||||||
with patch(
|
await knx.setup_integration(
|
||||||
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
|
{
|
||||||
return_value=fake_state,
|
SelectSchema.PLATFORM: {
|
||||||
):
|
CONF_NAME: "test",
|
||||||
await knx.setup_integration(
|
KNX_ADDRESS: [test_address, test_passive_address],
|
||||||
{
|
CONF_RESPOND_TO_READ: True,
|
||||||
SelectSchema.PLATFORM: {
|
CONF_PAYLOAD_LENGTH: 0,
|
||||||
CONF_NAME: "test",
|
SelectSchema.CONF_OPTIONS: _options,
|
||||||
KNX_ADDRESS: [test_address, test_passive_address],
|
|
||||||
CONF_RESPOND_TO_READ: True,
|
|
||||||
CONF_PAYLOAD_LENGTH: 0,
|
|
||||||
SelectSchema.CONF_OPTIONS: _options,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
)
|
}
|
||||||
|
)
|
||||||
# restored state - doesn't send telegram
|
# restored state - doesn't send telegram
|
||||||
state = hass.states.get("select.test")
|
state = hass.states.get("select.test")
|
||||||
assert state.state == "Control - On"
|
assert state.state == "Control - On"
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
"""Test KNX switch."""
|
"""Test KNX switch."""
|
||||||
from unittest.mock import patch
|
|
||||||
|
|
||||||
from homeassistant.components.knx.const import (
|
from homeassistant.components.knx.const import (
|
||||||
CONF_RESPOND_TO_READ,
|
CONF_RESPOND_TO_READ,
|
||||||
CONF_STATE_ADDRESS,
|
CONF_STATE_ADDRESS,
|
||||||
@ -12,6 +10,8 @@ from homeassistant.core import HomeAssistant, State
|
|||||||
|
|
||||||
from .conftest import KNXTestKit
|
from .conftest import KNXTestKit
|
||||||
|
|
||||||
|
from tests.common import mock_restore_cache
|
||||||
|
|
||||||
|
|
||||||
async def test_switch_simple(hass: HomeAssistant, knx: KNXTestKit):
|
async def test_switch_simple(hass: HomeAssistant, knx: KNXTestKit):
|
||||||
"""Test simple KNX switch."""
|
"""Test simple KNX switch."""
|
||||||
@ -115,20 +115,17 @@ async def test_switch_restore_and_respond(hass, knx):
|
|||||||
"""Test restoring KNX switch state and respond to read."""
|
"""Test restoring KNX switch state and respond to read."""
|
||||||
_ADDRESS = "1/1/1"
|
_ADDRESS = "1/1/1"
|
||||||
fake_state = State("switch.test", "on")
|
fake_state = State("switch.test", "on")
|
||||||
|
mock_restore_cache(hass, (fake_state,))
|
||||||
|
|
||||||
with patch(
|
await knx.setup_integration(
|
||||||
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
|
{
|
||||||
return_value=fake_state,
|
SwitchSchema.PLATFORM: {
|
||||||
):
|
CONF_NAME: "test",
|
||||||
await knx.setup_integration(
|
KNX_ADDRESS: _ADDRESS,
|
||||||
{
|
CONF_RESPOND_TO_READ: True,
|
||||||
SwitchSchema.PLATFORM: {
|
},
|
||||||
CONF_NAME: "test",
|
}
|
||||||
KNX_ADDRESS: _ADDRESS,
|
)
|
||||||
CONF_RESPOND_TO_READ: True,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
# restored state - doesn't send telegram
|
# restored state - doesn't send telegram
|
||||||
state = hass.states.get("switch.test")
|
state = hass.states.get("switch.test")
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
"""UniFi Network switch platform tests."""
|
"""UniFi Network switch platform tests."""
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from unittest.mock import patch
|
|
||||||
|
|
||||||
from aiounifi.controller import MESSAGE_CLIENT_REMOVED, MESSAGE_DEVICE, MESSAGE_EVENT
|
from aiounifi.controller import MESSAGE_CLIENT_REMOVED, MESSAGE_DEVICE, MESSAGE_EVENT
|
||||||
|
|
||||||
@ -32,6 +31,8 @@ from .test_controller import (
|
|||||||
setup_unifi_integration,
|
setup_unifi_integration,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from tests.common import mock_restore_cache
|
||||||
|
|
||||||
CLIENT_1 = {
|
CLIENT_1 = {
|
||||||
"hostname": "client_1",
|
"hostname": "client_1",
|
||||||
"ip": "10.0.0.1",
|
"ip": "10.0.0.1",
|
||||||
@ -1249,6 +1250,7 @@ async def test_restore_client_succeed(hass, aioclient_mock):
|
|||||||
"poe_mode": "auto",
|
"poe_mode": "auto",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
mock_restore_cache(hass, (fake_state,))
|
||||||
|
|
||||||
config_entry = config_entries.ConfigEntry(
|
config_entry = config_entries.ConfigEntry(
|
||||||
version=1,
|
version=1,
|
||||||
@ -1269,21 +1271,17 @@ async def test_restore_client_succeed(hass, aioclient_mock):
|
|||||||
config_entry=config_entry,
|
config_entry=config_entry,
|
||||||
)
|
)
|
||||||
|
|
||||||
with patch(
|
await setup_unifi_integration(
|
||||||
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
|
hass,
|
||||||
return_value=fake_state,
|
aioclient_mock,
|
||||||
):
|
options={
|
||||||
await setup_unifi_integration(
|
CONF_TRACK_CLIENTS: False,
|
||||||
hass,
|
CONF_TRACK_DEVICES: False,
|
||||||
aioclient_mock,
|
},
|
||||||
options={
|
clients_response=[],
|
||||||
CONF_TRACK_CLIENTS: False,
|
devices_response=[POE_DEVICE],
|
||||||
CONF_TRACK_DEVICES: False,
|
clients_all_response=[POE_CLIENT],
|
||||||
},
|
)
|
||||||
clients_response=[],
|
|
||||||
devices_response=[POE_DEVICE],
|
|
||||||
clients_all_response=[POE_CLIENT],
|
|
||||||
)
|
|
||||||
|
|
||||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1
|
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user