Use freezegun in wled tests (#99048)

This commit is contained in:
Erik Montnemery 2023-08-25 15:59:30 +02:00 committed by GitHub
parent 65d555b138
commit 346674a1a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 18 deletions

View File

@ -2,6 +2,7 @@
from collections.abc import Generator from collections.abc import Generator
from unittest.mock import AsyncMock, MagicMock, patch from unittest.mock import AsyncMock, MagicMock, patch
from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
from wled import Device as WLEDDevice from wled import Device as WLEDDevice
@ -67,7 +68,10 @@ def mock_wled(device_fixture: str) -> Generator[MagicMock, None, None]:
@pytest.fixture @pytest.fixture
async def init_integration( async def init_integration(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_wled: MagicMock hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
mock_config_entry: MockConfigEntry,
mock_wled: MagicMock,
) -> MockConfigEntry: ) -> MockConfigEntry:
"""Set up the WLED integration for testing.""" """Set up the WLED integration for testing."""
mock_config_entry.add_to_hass(hass) mock_config_entry.add_to_hass(hass)
@ -75,4 +79,8 @@ async def init_integration(
await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
# Let some time pass so coordinators can be reliably triggered by bumping
# time by SCAN_INTERVAL
freezer.tick(1)
return mock_config_entry return mock_config_entry

View File

@ -13,7 +13,7 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er
pytestmark = [ pytestmark = [
pytest.mark.usefixtures("init_integration"), pytest.mark.usefixtures("init_integration"),
pytest.mark.freeze_time("2021-11-04 17:37:00+01:00"), pytest.mark.freeze_time("2021-11-04 17:36:59+01:00"),
] ]

View File

@ -2,6 +2,7 @@
import json import json
from unittest.mock import MagicMock from unittest.mock import MagicMock
from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
from wled import Device as WLEDDevice, WLEDConnectionError, WLEDError from wled import Device as WLEDDevice, WLEDConnectionError, WLEDError
@ -27,7 +28,6 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
import homeassistant.util.dt as dt_util
from tests.common import MockConfigEntry, async_fire_time_changed, load_fixture from tests.common import MockConfigEntry, async_fire_time_changed, load_fixture
@ -177,6 +177,7 @@ async def test_master_change_state(
@pytest.mark.parametrize("device_fixture", ["rgb_single_segment"]) @pytest.mark.parametrize("device_fixture", ["rgb_single_segment"])
async def test_dynamically_handle_segments( async def test_dynamically_handle_segments(
hass: HomeAssistant, hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
mock_wled: MagicMock, mock_wled: MagicMock,
) -> None: ) -> None:
"""Test if a new/deleted segment is dynamically added/removed.""" """Test if a new/deleted segment is dynamically added/removed."""
@ -190,7 +191,8 @@ async def test_dynamically_handle_segments(
json.loads(load_fixture("wled/rgb.json")) json.loads(load_fixture("wled/rgb.json"))
) )
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done() await hass.async_block_till_done()
assert (master := hass.states.get("light.wled_rgb_light_master")) assert (master := hass.states.get("light.wled_rgb_light_master"))
@ -202,7 +204,8 @@ async def test_dynamically_handle_segments(
# Test adding if segment shows up again, including the master entity # Test adding if segment shows up again, including the master entity
mock_wled.update.return_value = return_value mock_wled.update.return_value = return_value
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done() await hass.async_block_till_done()
assert (master := hass.states.get("light.wled_rgb_light_master")) assert (master := hass.states.get("light.wled_rgb_light_master"))
@ -216,6 +219,7 @@ async def test_dynamically_handle_segments(
@pytest.mark.parametrize("device_fixture", ["rgb_single_segment"]) @pytest.mark.parametrize("device_fixture", ["rgb_single_segment"])
async def test_single_segment_behavior( async def test_single_segment_behavior(
hass: HomeAssistant, hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
mock_wled: MagicMock, mock_wled: MagicMock,
) -> None: ) -> None:
"""Test the behavior of the integration with a single segment.""" """Test the behavior of the integration with a single segment."""
@ -228,7 +232,8 @@ async def test_single_segment_behavior(
# Test segment brightness takes master into account # Test segment brightness takes master into account
device.state.brightness = 100 device.state.brightness = 100
device.state.segments[0].brightness = 255 device.state.segments[0].brightness = 255
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done() await hass.async_block_till_done()
assert (state := hass.states.get("light.wled_rgb_light")) assert (state := hass.states.get("light.wled_rgb_light"))
@ -236,7 +241,8 @@ async def test_single_segment_behavior(
# Test segment is off when master is off # Test segment is off when master is off
device.state.on = False device.state.on = False
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get("light.wled_rgb_light") state = hass.states.get("light.wled_rgb_light")
assert state assert state

View File

@ -2,6 +2,7 @@
import json import json
from unittest.mock import MagicMock from unittest.mock import MagicMock
from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
from syrupy.assertion import SnapshotAssertion from syrupy.assertion import SnapshotAssertion
from wled import Device as WLEDDevice, WLEDConnectionError, WLEDError from wled import Device as WLEDDevice, WLEDConnectionError, WLEDError
@ -16,7 +17,6 @@ from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
import homeassistant.util.dt as dt_util
from tests.common import async_fire_time_changed, load_fixture from tests.common import async_fire_time_changed, load_fixture
@ -113,6 +113,7 @@ async def test_numbers(
) )
async def test_speed_dynamically_handle_segments( async def test_speed_dynamically_handle_segments(
hass: HomeAssistant, hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
mock_wled: MagicMock, mock_wled: MagicMock,
entity_id_segment0: str, entity_id_segment0: str,
entity_id_segment1: str, entity_id_segment1: str,
@ -130,7 +131,8 @@ async def test_speed_dynamically_handle_segments(
json.loads(load_fixture("wled/rgb.json")) json.loads(load_fixture("wled/rgb.json"))
) )
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done() await hass.async_block_till_done()
assert (segment0 := hass.states.get(entity_id_segment0)) assert (segment0 := hass.states.get(entity_id_segment0))
@ -140,7 +142,8 @@ async def test_speed_dynamically_handle_segments(
# Test remove segment again... # Test remove segment again...
mock_wled.update.return_value = return_value mock_wled.update.return_value = return_value
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done() await hass.async_block_till_done()
assert (segment0 := hass.states.get(entity_id_segment0)) assert (segment0 := hass.states.get(entity_id_segment0))

View File

@ -2,6 +2,7 @@
import json import json
from unittest.mock import MagicMock from unittest.mock import MagicMock
from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
from syrupy.assertion import SnapshotAssertion from syrupy.assertion import SnapshotAssertion
from wled import Device as WLEDDevice, WLEDConnectionError, WLEDError from wled import Device as WLEDDevice, WLEDConnectionError, WLEDError
@ -17,7 +18,6 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
import homeassistant.util.dt as dt_util
from tests.common import async_fire_time_changed, load_fixture from tests.common import async_fire_time_changed, load_fixture
@ -125,6 +125,7 @@ async def test_color_palette_state(
@pytest.mark.parametrize("device_fixture", ["rgb_single_segment"]) @pytest.mark.parametrize("device_fixture", ["rgb_single_segment"])
async def test_color_palette_dynamically_handle_segments( async def test_color_palette_dynamically_handle_segments(
hass: HomeAssistant, hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
mock_wled: MagicMock, mock_wled: MagicMock,
) -> None: ) -> None:
"""Test if a new/deleted segment is dynamically added/removed.""" """Test if a new/deleted segment is dynamically added/removed."""
@ -137,7 +138,8 @@ async def test_color_palette_dynamically_handle_segments(
json.loads(load_fixture("wled/rgb.json")) json.loads(load_fixture("wled/rgb.json"))
) )
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done() await hass.async_block_till_done()
assert (segment0 := hass.states.get("select.wled_rgb_light_color_palette")) assert (segment0 := hass.states.get("select.wled_rgb_light_color_palette"))
@ -149,7 +151,8 @@ async def test_color_palette_dynamically_handle_segments(
# Test adding if segment shows up again, including the master entity # Test adding if segment shows up again, including the master entity
mock_wled.update.return_value = return_value mock_wled.update.return_value = return_value
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done() await hass.async_block_till_done()
assert (segment0 := hass.states.get("select.wled_rgb_light_color_palette")) assert (segment0 := hass.states.get("select.wled_rgb_light_color_palette"))
@ -175,13 +178,15 @@ async def test_playlist_unavailable_without_playlists(hass: HomeAssistant) -> No
@pytest.mark.parametrize("device_fixture", ["rgbw"]) @pytest.mark.parametrize("device_fixture", ["rgbw"])
async def test_old_style_preset_active( async def test_old_style_preset_active(
hass: HomeAssistant, hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
mock_wled: MagicMock, mock_wled: MagicMock,
) -> None: ) -> None:
"""Test unknown preset returned (when old style/unknown) preset is active.""" """Test unknown preset returned (when old style/unknown) preset is active."""
# Set device preset state to a random number # Set device preset state to a random number
mock_wled.update.return_value.state.preset = 99 mock_wled.update.return_value.state.preset = 99
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done() await hass.async_block_till_done()
assert (state := hass.states.get("select.wled_rgbw_light_preset")) assert (state := hass.states.get("select.wled_rgbw_light_preset"))
@ -191,13 +196,15 @@ async def test_old_style_preset_active(
@pytest.mark.parametrize("device_fixture", ["rgbw"]) @pytest.mark.parametrize("device_fixture", ["rgbw"])
async def test_old_style_playlist_active( async def test_old_style_playlist_active(
hass: HomeAssistant, hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
mock_wled: MagicMock, mock_wled: MagicMock,
) -> None: ) -> None:
"""Test when old style playlist cycle is active.""" """Test when old style playlist cycle is active."""
# Set device playlist to 0, which meant "cycle" previously. # Set device playlist to 0, which meant "cycle" previously.
mock_wled.update.return_value.state.playlist = 0 mock_wled.update.return_value.state.playlist = 0
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done() await hass.async_block_till_done()
assert (state := hass.states.get("select.wled_rgbw_light_playlist")) assert (state := hass.states.get("select.wled_rgbw_light_playlist"))

View File

@ -2,6 +2,7 @@
import json import json
from unittest.mock import MagicMock from unittest.mock import MagicMock
from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
from syrupy.assertion import SnapshotAssertion from syrupy.assertion import SnapshotAssertion
from wled import Device as WLEDDevice, WLEDConnectionError, WLEDError from wled import Device as WLEDDevice, WLEDConnectionError, WLEDError
@ -19,7 +20,6 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
import homeassistant.util.dt as dt_util
from tests.common import async_fire_time_changed, load_fixture from tests.common import async_fire_time_changed, load_fixture
@ -132,6 +132,7 @@ async def test_switch_state(
@pytest.mark.parametrize("device_fixture", ["rgb_single_segment"]) @pytest.mark.parametrize("device_fixture", ["rgb_single_segment"])
async def test_switch_dynamically_handle_segments( async def test_switch_dynamically_handle_segments(
hass: HomeAssistant, hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
mock_wled: MagicMock, mock_wled: MagicMock,
) -> None: ) -> None:
"""Test if a new/deleted segment is dynamically added/removed.""" """Test if a new/deleted segment is dynamically added/removed."""
@ -146,7 +147,8 @@ async def test_switch_dynamically_handle_segments(
json.loads(load_fixture("wled/rgb.json")) json.loads(load_fixture("wled/rgb.json"))
) )
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done() await hass.async_block_till_done()
assert (segment0 := hass.states.get("switch.wled_rgb_light_reverse")) assert (segment0 := hass.states.get("switch.wled_rgb_light_reverse"))
@ -156,7 +158,8 @@ async def test_switch_dynamically_handle_segments(
# Test remove segment again... # Test remove segment again...
mock_wled.update.return_value = return_value mock_wled.update.return_value = return_value
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done() await hass.async_block_till_done()
assert (segment0 := hass.states.get("switch.wled_rgb_light_reverse")) assert (segment0 := hass.states.get("switch.wled_rgb_light_reverse"))