Skip to content

Commit 81c8b5d

Browse files
authored
Fix missing fixture (#67)
* add more lines from const * use fixture * use autouse in conftest * run generate
1 parent 03a39d1 commit 81c8b5d

File tree

8 files changed

+67
-23
lines changed

8 files changed

+67
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# pytest-homeassistant-custom-component
22

3-
![HA core version](https://img.shields.io/static/v1?label=HA+core+version&message=2021.5.0b4&labelColor=blue)
3+
![HA core version](https://img.shields.io/static/v1?label=HA+core+version&message=2021.6.0b0&labelColor=blue)
44

55
Package to automatically extract testing plugins from Home Assistant for custom component testing.
66
The goal is to provide the same functionality as the tests in home-assistant/core.

pytest_homeassistant_custom_component/common.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import collections
1010
from collections import OrderedDict
1111
from contextlib import contextmanager
12-
from datetime import timedelta
12+
from datetime import datetime, timedelta
1313
import functools as ft
1414
from io import StringIO
1515
import json
@@ -49,7 +49,7 @@
4949
STATE_OFF,
5050
STATE_ON,
5151
)
52-
from homeassistant.core import BLOCK_LOG_TIMEOUT, State
52+
from homeassistant.core import BLOCK_LOG_TIMEOUT, HomeAssistant, State
5353
from homeassistant.helpers import (
5454
area_registry,
5555
device_registry,
@@ -275,7 +275,7 @@ async def _await_count_and_log_pending(
275275
hass.config.latitude = 32.87336
276276
hass.config.longitude = -117.22743
277277
hass.config.elevation = 0
278-
hass.config.time_zone = date_util.get_time_zone("US/Pacific")
278+
hass.config.time_zone = "US/Pacific"
279279
hass.config.units = METRIC_SYSTEM
280280
hass.config.media_dirs = {"local": get_test_config_dir("media")}
281281
hass.config.skip_pip = True
@@ -366,7 +366,9 @@ def async_fire_mqtt_message(hass, topic, payload, qos=0, retain=False):
366366

367367

368368
@ha.callback
369-
def async_fire_time_changed(hass, datetime_, fire_all=False):
369+
def async_fire_time_changed(
370+
hass: HomeAssistant, datetime_: datetime, fire_all: bool = False
371+
) -> None:
370372
"""Fire a time changes event."""
371373
hass.bus.async_fire(EVENT_TIME_CHANGED, {"now": date_util.as_utc(datetime_)})
372374

@@ -737,7 +739,6 @@ def __init__(
737739
state=None,
738740
options={},
739741
system_options={},
740-
connection_class=config_entries.CONN_CLASS_UNKNOWN,
741742
unique_id=None,
742743
disabled_by=None,
743744
reason=None,
@@ -751,7 +752,6 @@ def __init__(
751752
"options": options,
752753
"version": version,
753754
"title": title,
754-
"connection_class": connection_class,
755755
"unique_id": unique_id,
756756
"disabled_by": disabled_by,
757757
}

pytest_homeassistant_custom_component/const.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
44
This file is originally from homeassistant/core and modified by pytest-homeassistant-custom-component.
55
"""
6-
MAJOR_VERSION = 2021
7-
MINOR_VERSION = 5
8-
PATCH_VERSION = "0b4"
9-
__short_version__ = f"{MAJOR_VERSION}.{MINOR_VERSION}"
10-
__version__ = f"{__short_version__}.{PATCH_VERSION}"
6+
from __future__ import annotations
7+
8+
from typing import Final
9+
10+
MAJOR_VERSION: Final = 2021
11+
MINOR_VERSION: Final = 6
12+
PATCH_VERSION: Final = "0b0"
13+
__short_version__: Final = f"{MAJOR_VERSION}.{MINOR_VERSION}"
14+
__version__: Final = f"{__short_version__}.{PATCH_VERSION}"

pytest_homeassistant_custom_component/plugins.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from homeassistant.auth.const import GROUP_ID_ADMIN, GROUP_ID_READ_ONLY
2121
from homeassistant.auth.models import Credentials
2222
from homeassistant.auth.providers import homeassistant, legacy_api_password
23-
from homeassistant.components import mqtt
23+
from homeassistant.components import mqtt, recorder
2424
from homeassistant.components.websocket_api.auth import (
2525
TYPE_AUTH,
2626
TYPE_AUTH_OK,
@@ -43,6 +43,8 @@
4343
MockUser,
4444
async_fire_mqtt_message,
4545
async_test_home_assistant,
46+
get_test_home_assistant,
47+
init_recorder_component,
4648
mock_storage as mock_storage,
4749
)
4850
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):
482484
@pytest.fixture
483485
def mock_zeroconf():
484486
"""Mock zeroconf."""
485-
with patch("homeassistant.components.zeroconf.HaZeroconf") as mock_zc:
487+
with patch("homeassistant.components.zeroconf.models.HaZeroconf") as mock_zc:
486488
yield mock_zc.return_value
487489

488490

@@ -599,3 +601,36 @@ def pattern_time_change_listener(ev) -> None:
599601
def enable_custom_integrations(hass):
600602
"""Enable custom integrations defined in the test dir."""
601603
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()

requirements_dev.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# This file is originally from homeassistant/core and modified by pytest-homeassistant-custom-component.
22
codecov==2.1.10
33
mypy==0.812
4-
pre-commit==2.12.1
5-
pylint==2.8.0
6-
astroid==2.5.5
4+
pre-commit==2.13.0
5+
pylint==2.8.2

requirements_test.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ pytest-cov==2.10.1
1515
pytest-test-groups==1.0.3
1616
pytest-sugar==0.9.4
1717
pytest-timeout==1.4.2
18-
pytest-xdist==2.1.0
19-
pytest==6.2.3
20-
requests_mock==1.8.0
18+
pytest-xdist==2.2.1
19+
pytest==6.2.4
20+
requests_mock==1.9.2
2121
responses==0.12.0
2222
respx==0.17.0
2323
stdlib-list==0.7.0
2424
tqdm==4.49.0
25-
homeassistant==2021.5.0b4
25+
homeassistant==2021.6.0b0

tests/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""Fixtures for trsting."""
2+
import pytest
3+
4+
5+
@pytest.fixture(autouse=True)
6+
def auto_enable_custom_integrations(enable_custom_integrations):
7+
yield
8+

tests/test_config_flow.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
from custom_components.simple_integration.const import DOMAIN
66

77

8-
9-
108
async def test_form(hass):
119
"""Test we get the form."""
1210
await setup.async_setup_component(hass, "persistent_notification", {})

0 commit comments

Comments
 (0)