Use freezegun in motioneye tests (#99038)

This commit is contained in:
Erik Montnemery 2023-08-25 16:00:11 +02:00 committed by GitHub
parent 943db9e0d5
commit e0af9de877
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 20 deletions

View file

@ -3,6 +3,7 @@ import copy
from datetime import timedelta
from unittest.mock import AsyncMock, patch
from freezegun.api import FrozenDateTimeFactory
from motioneye_client.const import KEY_ACTIONS
from homeassistant.components.motioneye import get_motioneye_device_identifier
@ -14,7 +15,6 @@ from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.config_entries import RELOAD_AFTER_UPDATE_DELAY
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
import homeassistant.util.dt as dt_util
from . import (
TEST_CAMERA,
@ -28,7 +28,9 @@ from . import (
from tests.common import async_fire_time_changed
async def test_sensor_actions(hass: HomeAssistant) -> None:
async def test_sensor_actions(
hass: HomeAssistant, freezer: FrozenDateTimeFactory
) -> None:
"""Test the actions sensor."""
register_test_entity(
hass,
@ -51,7 +53,8 @@ async def test_sensor_actions(hass: HomeAssistant) -> None:
# When the next refresh is called return the updated values.
client.async_get_cameras = AsyncMock(return_value={"cameras": [updated_camera]})
async_fire_time_changed(hass, dt_util.utcnow() + DEFAULT_SCAN_INTERVAL)
freezer.tick(DEFAULT_SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
entity_state = hass.states.get(TEST_SENSOR_ACTION_ENTITY_ID)
@ -60,7 +63,8 @@ async def test_sensor_actions(hass: HomeAssistant) -> None:
assert entity_state.attributes.get(KEY_ACTIONS) == ["one"]
del updated_camera[KEY_ACTIONS]
async_fire_time_changed(hass, dt_util.utcnow() + DEFAULT_SCAN_INTERVAL)
freezer.tick(DEFAULT_SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
entity_state = hass.states.get(TEST_SENSOR_ACTION_ENTITY_ID)
@ -99,7 +103,9 @@ async def test_sensor_device_info(hass: HomeAssistant) -> None:
assert TEST_SENSOR_ACTION_ENTITY_ID in entities_from_device
async def test_sensor_actions_can_be_enabled(hass: HomeAssistant) -> None:
async def test_sensor_actions_can_be_enabled(
hass: HomeAssistant, freezer: FrozenDateTimeFactory
) -> None:
"""Verify the action sensor can be enabled."""
client = create_mock_motioneye_client()
await setup_mock_motioneye_config_entry(hass, client=client)
@ -122,10 +128,8 @@ async def test_sensor_actions_can_be_enabled(hass: HomeAssistant) -> None:
assert not updated_entry.disabled
await hass.async_block_till_done()
async_fire_time_changed(
hass,
dt_util.utcnow() + timedelta(seconds=RELOAD_AFTER_UPDATE_DELAY + 1),
)
freezer.tick(timedelta(seconds=RELOAD_AFTER_UPDATE_DELAY + 1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
entity_state = hass.states.get(TEST_SENSOR_ACTION_ENTITY_ID)

View file

@ -3,6 +3,7 @@ import copy
from datetime import timedelta
from unittest.mock import AsyncMock, call, patch
from freezegun.api import FrozenDateTimeFactory
from motioneye_client.const import (
KEY_MOTION_DETECTION,
KEY_MOVIES,
@ -19,7 +20,6 @@ from homeassistant.config_entries import RELOAD_AFTER_UPDATE_DELAY
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
import homeassistant.util.dt as dt_util
from . import (
TEST_CAMERA,
@ -34,7 +34,9 @@ from . import (
from tests.common import async_fire_time_changed
async def test_switch_turn_on_off(hass: HomeAssistant) -> None:
async def test_switch_turn_on_off(
hass: HomeAssistant, freezer: FrozenDateTimeFactory
) -> None:
"""Test turning the switch on and off."""
client = create_mock_motioneye_client()
await setup_mock_motioneye_config_entry(hass, client=client)
@ -60,7 +62,8 @@ async def test_switch_turn_on_off(hass: HomeAssistant) -> None:
blocking=True,
)
async_fire_time_changed(hass, dt_util.utcnow() + DEFAULT_SCAN_INTERVAL)
freezer.tick(DEFAULT_SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
# Verify correct parameters are passed to the library.
@ -85,7 +88,8 @@ async def test_switch_turn_on_off(hass: HomeAssistant) -> None:
# Verify correct parameters are passed to the library.
assert client.async_set_camera.call_args == call(TEST_CAMERA_ID, TEST_CAMERA)
async_fire_time_changed(hass, dt_util.utcnow() + DEFAULT_SCAN_INTERVAL)
freezer.tick(DEFAULT_SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
# Verify the switch turns on.
@ -94,7 +98,9 @@ async def test_switch_turn_on_off(hass: HomeAssistant) -> None:
assert entity_state.state == "on"
async def test_switch_state_update_from_coordinator(hass: HomeAssistant) -> None:
async def test_switch_state_update_from_coordinator(
hass: HomeAssistant, freezer: FrozenDateTimeFactory
) -> None:
"""Test that coordinator data impacts state."""
client = create_mock_motioneye_client()
await setup_mock_motioneye_config_entry(hass, client=client)
@ -108,7 +114,8 @@ async def test_switch_state_update_from_coordinator(hass: HomeAssistant) -> None
updated_cameras["cameras"][0][KEY_MOTION_DETECTION] = False
client.async_get_cameras = AsyncMock(return_value=updated_cameras)
async_fire_time_changed(hass, dt_util.utcnow() + DEFAULT_SCAN_INTERVAL)
freezer.tick(DEFAULT_SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
# Verify the switch turns off.
@ -144,7 +151,9 @@ async def test_switch_has_correct_entities(hass: HomeAssistant) -> None:
assert not entity_state
async def test_disabled_switches_can_be_enabled(hass: HomeAssistant) -> None:
async def test_disabled_switches_can_be_enabled(
hass: HomeAssistant, freezer: FrozenDateTimeFactory
) -> None:
"""Verify disabled switches can be enabled."""
client = create_mock_motioneye_client()
await setup_mock_motioneye_config_entry(hass, client=client)
@ -174,10 +183,8 @@ async def test_disabled_switches_can_be_enabled(hass: HomeAssistant) -> None:
assert not updated_entry.disabled
await hass.async_block_till_done()
async_fire_time_changed(
hass,
dt_util.utcnow() + timedelta(seconds=RELOAD_AFTER_UPDATE_DELAY + 1),
)
freezer.tick(timedelta(seconds=RELOAD_AFTER_UPDATE_DELAY + 1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
entity_state = hass.states.get(entity_id)