Skip to content

Commit

Permalink
Bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewFlamm committed May 30, 2024
1 parent eb03fb9 commit 4e8daa4
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 89 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pytest-homeassistant-custom-component

![HA core version](https://img.shields.io/static/v1?label=HA+core+version&message=2024.5.5&labelColor=blue)
![HA core version](https://img.shields.io/static/v1?label=HA+core+version&message=2024.6.0b1&labelColor=blue)

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/MatthewFlamm/pytest-homeassistant-custom-component)

Expand Down
2 changes: 1 addition & 1 deletion ha_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024.5.5
2024.6.0b1
20 changes: 10 additions & 10 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# This file is originally from homeassistant/core and modified by pytest-homeassistant-custom-component.
astroid==3.1.0
pre-commit==3.7.0
pylint==3.1.0
types-aiofiles==23.2.0.20240311
astroid==3.2.2
pre-commit==3.7.1
pylint==3.2.2
types-aiofiles==23.2.0.20240403
types-atomicwrites==1.4.5.1
types-croniter==2.0.0.20240321
types-beautifulsoup4==4.12.0.20240229
types-caldav==1.3.0.20240106
types-croniter==2.0.0.20240423
types-beautifulsoup4==4.12.0.20240511
types-caldav==1.3.0.20240331
types-chardet==0.1.5
types-decorator==5.1.8.20240310
types-paho-mqtt==1.6.0.20240321
types-pillow==10.2.0.20240324
types-pillow==10.2.0.20240511
types-protobuf==4.24.0.20240106
types-psutil==5.9.5.20240316
types-psutil==5.9.5.20240511
types-python-dateutil==2.9.0.20240316
types-python-slugify==8.0.2.20240310
types-pytz==2024.1.0.20240203
types-pytz==2024.1.0.20240417
types-PyYAML==6.0.12.20240311
types-requests==2.31.0.3
types-xmltodict==0.13.0.3
22 changes: 11 additions & 11 deletions requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
-c homeassistant/package_constraints.txt
-r requirements_test_pre_commit.txt
coverage==7.5.0
freezegun==1.4.0
freezegun==1.5.0
mock-open==1.4.0
mypy-dev==1.10.0a3
pydantic==1.10.12
mypy-dev==1.11.0a3
pydantic==1.10.15
pylint-per-file-ignores==1.3.2
pipdeptree==2.17.0
pipdeptree==2.19.0
pytest-asyncio==0.23.6
pytest-aiohttp==1.0.5
pytest-cov==5.0.0
Expand All @@ -25,15 +25,15 @@ pytest-sugar==1.0.0
pytest-timeout==2.3.1
pytest-unordered==0.6.0
pytest-picked==0.5.0
pytest-xdist==3.5.0
pytest==8.1.1
pytest-xdist==3.6.1
pytest==8.2.0
requests-mock==1.12.1
respx==0.21.0
respx==0.21.1
syrupy==4.6.1
tqdm==4.66.2
uv==0.1.35
homeassistant==2024.5.5
SQLAlchemy==2.0.29
tqdm==4.66.4
uv==0.1.43
homeassistant==2024.6.0b1
SQLAlchemy==2.0.30

paho-mqtt==1.6.1

Expand Down
66 changes: 37 additions & 29 deletions src/pytest_homeassistant_custom_component/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import time
import traceback
from types import FrameType, ModuleType
from typing import Any, NoReturn, TypeVar
from typing import Any, NoReturn
from unittest.mock import AsyncMock, Mock, patch

from aiohttp.test_utils import unused_port as get_test_instance_port # noqa: F401
Expand All @@ -43,7 +43,7 @@
_async_get_device_automation_capabilities as async_get_device_automation_capabilities,
)
from homeassistant.config import async_process_component_config
from homeassistant.config_entries import ConfigEntry, ConfigFlow
from homeassistant.config_entries import ConfigEntry, ConfigFlow, _DataT
from homeassistant.const import (
DEVICE_DEFAULT_NAME,
EVENT_HOMEASSISTANT_CLOSE,
Expand Down Expand Up @@ -179,6 +179,7 @@ def run_loop() -> None:
"""Run event loop."""

loop._thread_ident = threading.get_ident()
hass._loop_thread_id = loop._thread_ident
loop.run_forever()
loop_stop_event.set()

Expand All @@ -204,10 +205,7 @@ def stop_hass() -> None:
loop.close()


_T = TypeVar("_T", bound=Mapping[str, Any] | Sequence[Any])


class StoreWithoutWriteLoad(storage.Store[_T]):
class StoreWithoutWriteLoad[_T: (Mapping[str, Any] | Sequence[Any])](storage.Store[_T]):
"""Fake store that does not write or load. Used for testing."""

async def async_save(self, *args: Any, **kwargs: Any) -> None:
Expand Down Expand Up @@ -240,7 +238,7 @@ async def async_test_home_assistant(
orig_async_add_job = hass.async_add_job
orig_async_add_executor_job = hass.async_add_executor_job
orig_async_create_task_internal = hass.async_create_task_internal
orig_tz = dt_util.DEFAULT_TIME_ZONE
orig_tz = dt_util.get_default_time_zone()

def async_add_job(target, *args, eager_start: bool = False):
"""Add job."""
Expand Down Expand Up @@ -287,7 +285,7 @@ def async_create_task_internal(coroutine, name=None, eager_start=True):
hass.config.latitude = 32.87336
hass.config.longitude = -117.22743
hass.config.elevation = 0
hass.config.set_time_zone("US/Pacific")
await hass.config.async_set_time_zone("US/Pacific")
hass.config.units = METRIC_SYSTEM
hass.config.media_dirs = {"local": get_test_config_dir("media")}
hass.config.skip_pip = True
Expand Down Expand Up @@ -358,17 +356,18 @@ def async_create_task_internal(coroutine, name=None, eager_start=True):

hass.set_state(CoreState.running)

async def clear_instance(event):
@callback
def clear_instance(event):
"""Clear global instance."""
await asyncio.sleep(0) # Give aiohttp one loop iteration to close
INSTANCES.remove(hass)
# Give aiohttp one loop iteration to close
hass.loop.call_soon(INSTANCES.remove, hass)

hass.bus.async_listen_once(EVENT_HOMEASSISTANT_CLOSE, clear_instance)

yield hass

# Restore timezone, it is set when creating the hass object
dt_util.DEFAULT_TIME_ZONE = orig_tz
dt_util.set_default_time_zone(orig_tz)


def async_mock_service(
Expand Down Expand Up @@ -454,6 +453,7 @@ def async_fire_mqtt_message(
msg.payload = payload
msg.qos = qos
msg.retain = retain
msg.timestamp = time.monotonic()

mqtt_data: MqttData = hass.data["mqtt"]
assert mqtt_data.client
Expand Down Expand Up @@ -636,6 +636,7 @@ def mock_registry(
registry.entities[key] = entry

hass.data[er.DATA_REGISTRY] = registry
er.async_get.cache_clear()
return registry


Expand All @@ -659,6 +660,7 @@ def mock_area_registry(
registry.areas[key] = entry

hass.data[ar.DATA_REGISTRY] = registry
ar.async_get.cache_clear()
return registry


Expand Down Expand Up @@ -687,6 +689,7 @@ def mock_device_registry(
registry.deleted_devices = dr.DeviceRegistryItems()

hass.data[dr.DATA_REGISTRY] = registry
dr.async_get.cache_clear()
return registry


Expand Down Expand Up @@ -974,40 +977,42 @@ def last_call(self, method=None):
return None


class MockConfigEntry(config_entries.ConfigEntry):
class MockConfigEntry(config_entries.ConfigEntry[_DataT]):
"""Helper for creating config entries that adds some defaults."""

runtime_data: _DataT

def __init__(
self,
*,
domain="test",
data=None,
version=1,
minor_version=1,
disabled_by=None,
domain="test",
entry_id=None,
source=config_entries.SOURCE_USER,
title="Mock Title",
state=None,
options={},
minor_version=1,
options=None,
pref_disable_new_entities=None,
pref_disable_polling=None,
unique_id=None,
disabled_by=None,
reason=None,
source=config_entries.SOURCE_USER,
state=None,
title="Mock Title",
unique_id=None,
version=1,
) -> None:
"""Initialize a mock config entry."""
kwargs = {
"entry_id": entry_id or uuid_util.random_uuid_hex(),
"domain": domain,
"data": data or {},
"disabled_by": disabled_by,
"domain": domain,
"entry_id": entry_id or uuid_util.random_uuid_hex(),
"minor_version": minor_version,
"options": options or {},
"pref_disable_new_entities": pref_disable_new_entities,
"pref_disable_polling": pref_disable_polling,
"options": options,
"version": version,
"minor_version": minor_version,
"title": title,
"unique_id": unique_id,
"disabled_by": disabled_by,
"version": version,
}
if source is not None:
kwargs["source"] = source
Expand Down Expand Up @@ -1176,6 +1181,7 @@ def mock_restore_cache(hass: HomeAssistant, states: Sequence[State]) -> None:
_LOGGER.debug("Restore cache: %s", data.last_states)
assert len(data.last_states) == len(states), f"Duplicate entity_id? {states}"

restore_state.async_get.cache_clear()
hass.data[key] = data


Expand Down Expand Up @@ -1203,6 +1209,7 @@ def mock_restore_cache_with_extra_data(
_LOGGER.debug("Restore cache: %s", data.last_states)
assert len(data.last_states) == len(states), f"Duplicate entity_id? {states}"

restore_state.async_get.cache_clear()
hass.data[key] = data


Expand Down Expand Up @@ -1757,5 +1764,6 @@ async def snapshot_platform(
for entity_entry in entity_entries:
assert entity_entry == snapshot(name=f"{entity_entry.entity_id}-entry")
assert entity_entry.disabled_by is None, "Please enable all entities."
assert (state := hass.states.get(entity_entry.entity_id))
state = hass.states.get(entity_entry.entity_id)
assert state, f"State not found for {entity_entry.entity_id}"
assert state == snapshot(name=f"{entity_entry.entity_id}-state")
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ def assert_dict_of_states_equal_without_context_and_last_changed(
)


async def async_record_states(hass: HomeAssistant):
"""Record some test states."""
return await hass.async_add_executor_job(record_states, hass)


def record_states(hass):
"""Record some test states.
Expand Down
4 changes: 2 additions & 2 deletions src/pytest_homeassistant_custom_component/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""
from typing import TYPE_CHECKING, Final
MAJOR_VERSION: Final = 2024
MINOR_VERSION: Final = 5
PATCH_VERSION: Final = "5"
MINOR_VERSION: Final = 6
PATCH_VERSION: Final = "0b1"
__short_version__: Final = f"{MAJOR_VERSION}.{MINOR_VERSION}"
__version__: Final = f"{__short_version__}.{PATCH_VERSION}"
4 changes: 4 additions & 0 deletions src/pytest_homeassistant_custom_component/patch_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import time

from homeassistant import runner, util
from homeassistant.helpers import event as event_helper
from homeassistant.util import dt as dt_util


Expand All @@ -23,6 +24,9 @@ def _monotonic() -> float:
return time.monotonic()


# Replace partial functions which are not found by freezegun
dt_util.utcnow = _utcnow # type: ignore[assignment]
event_helper.time_tracker_utcnow = _utcnow # type: ignore[assignment]
util.utcnow = _utcnow # type: ignore[assignment]

runner.monotonic = _monotonic # type: ignore[assignment]
Loading

0 comments on commit 4e8daa4

Please sign in to comment.