|
20 | 20 | from homeassistant.auth.const import GROUP_ID_ADMIN, GROUP_ID_READ_ONLY
|
21 | 21 | from homeassistant.auth.models import Credentials
|
22 | 22 | from homeassistant.auth.providers import homeassistant, legacy_api_password
|
23 |
| -from homeassistant.components import mqtt |
| 23 | +from homeassistant.components import mqtt, recorder |
24 | 24 | from homeassistant.components.websocket_api.auth import (
|
25 | 25 | TYPE_AUTH,
|
26 | 26 | TYPE_AUTH_OK,
|
|
43 | 43 | MockUser,
|
44 | 44 | async_fire_mqtt_message,
|
45 | 45 | async_test_home_assistant,
|
| 46 | + get_test_home_assistant, |
| 47 | + init_recorder_component, |
46 | 48 | mock_storage as mock_storage,
|
47 | 49 | )
|
48 | 50 | from .test_util.aiohttp import mock_aiohttp_client # noqa: E402, isort:skip
|
@@ -482,7 +484,7 @@ async def mqtt_mock(hass, mqtt_client_mock, mqtt_config):
|
482 | 484 | @pytest.fixture
|
483 | 485 | def mock_zeroconf():
|
484 | 486 | """Mock zeroconf."""
|
485 |
| - with patch("homeassistant.components.zeroconf.HaZeroconf") as mock_zc: |
| 487 | + with patch("homeassistant.components.zeroconf.models.HaZeroconf") as mock_zc: |
486 | 488 | yield mock_zc.return_value
|
487 | 489 |
|
488 | 490 |
|
@@ -599,3 +601,36 @@ def pattern_time_change_listener(ev) -> None:
|
599 | 601 | def enable_custom_integrations(hass):
|
600 | 602 | """Enable custom integrations defined in the test dir."""
|
601 | 603 | hass.data.pop(loader.DATA_CUSTOM_COMPONENTS)
|
| 604 | + |
| 605 | + |
| 606 | +@pytest.fixture |
| 607 | +def enable_statistics(): |
| 608 | + """Fixture to control enabling of recorder's statistics compilation. |
| 609 | +
|
| 610 | + To enable statistics, tests can be marked with: |
| 611 | + @pytest.mark.parametrize("enable_statistics", [True]) |
| 612 | + """ |
| 613 | + return False |
| 614 | + |
| 615 | + |
| 616 | +@pytest.fixture |
| 617 | +def hass_recorder(enable_statistics): |
| 618 | + """Home Assistant fixture with in-memory recorder.""" |
| 619 | + hass = get_test_home_assistant() |
| 620 | + stats = recorder.Recorder.async_hourly_statistics if enable_statistics else None |
| 621 | + with patch( |
| 622 | + "homeassistant.components.recorder.Recorder.async_hourly_statistics", |
| 623 | + side_effect=stats, |
| 624 | + autospec=True, |
| 625 | + ): |
| 626 | + |
| 627 | + def setup_recorder(config=None): |
| 628 | + """Set up with params.""" |
| 629 | + init_recorder_component(hass, config) |
| 630 | + hass.start() |
| 631 | + hass.block_till_done() |
| 632 | + hass.data[recorder.DATA_INSTANCE].block_till_done() |
| 633 | + return hass |
| 634 | + |
| 635 | + yield setup_recorder |
| 636 | + hass.stop() |
0 commit comments