From 8c94a50d71c96ffb3bb60d1261b23fbcac3f2ae5 Mon Sep 17 00:00:00 2001 From: Marat Radchenko Date: Wed, 8 Jan 2025 18:58:28 +0300 Subject: [PATCH] Add basic test infrastructure --- .github/workflows/tox.yaml | 2 +- .gitignore | 1 + custom_components/localtuya/__init__.py | 28 ++++++++++++++++++------- pyproject.toml | 5 +++++ requirements_test.txt | 2 +- setup.cfg | 4 ++++ tests/__init__.py | 0 tests/conftest.py | 20 ++++++++++++++++++ tests/test_init.py | 9 ++++++++ tox.ini | 3 +-- 10 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/conftest.py create mode 100644 tests/test_init.py diff --git a/.github/workflows/tox.yaml b/.github/workflows/tox.yaml index 082994cc0..675214e8b 100644 --- a/.github/workflows/tox.yaml +++ b/.github/workflows/tox.yaml @@ -16,7 +16,7 @@ jobs: platform: - ubuntu-latest python-version: - - '3.10' + - '3.12' steps: - uses: actions/checkout@v2 - name: Set up Python ${{ matrix.python-version }} diff --git a/.gitignore b/.gitignore index ecd6feb54..2e5992502 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *~ __pycache__ +.idea .tox tuyadebug/ .pre-commit-config.yaml \ No newline at end of file diff --git a/custom_components/localtuya/__init__.py b/custom_components/localtuya/__init__.py index d37365110..6e21676e1 100644 --- a/custom_components/localtuya/__init__.py +++ b/custom_components/localtuya/__init__.py @@ -24,10 +24,12 @@ CONF_REGION, EVENT_HOMEASSISTANT_STOP, SERVICE_RELOAD, + CONF_DISCOVERY, ) from homeassistant.core import Event, HomeAssistant, ServiceCall, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.event import async_track_time_interval +from homeassistant.helpers.typing import ConfigType from .coordinator import TuyaDevice, HassLocalTuyaData, TuyaCloudApi from .config_flow import ENTRIES_VERSION @@ -47,6 +49,16 @@ _LOGGER = logging.getLogger(__name__) +CONFIG_SCHEMA = vol.Schema( + { + DOMAIN: vol.Schema( + { + vol.Optional(CONF_DISCOVERY, default=True): bool, + } + ), + } +) + CONF_DP = "dp" CONF_VALUE = "value" @@ -60,7 +72,7 @@ ) -async def async_setup(hass: HomeAssistant, config: dict): +async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the LocalTuya integration component.""" hass.data.setdefault(DOMAIN, {}) @@ -173,12 +185,14 @@ def _shutdown(event): ) discovery = TuyaDiscovery(_device_discovered) - try: - await discovery.start() - hass.data[DOMAIN][DATA_DISCOVERY] = discovery - hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _shutdown) - except Exception: # pylint: disable=broad-except - _LOGGER.exception("failed to set up discovery") + + if config[DOMAIN][CONF_DISCOVERY]: + try: + await discovery.start() + hass.data[DOMAIN][DATA_DISCOVERY] = discovery + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _shutdown) + except Exception: # pylint: disable=broad-except + _LOGGER.exception("failed to set up discovery") return True diff --git a/pyproject.toml b/pyproject.toml index cf08c0176..d8309a166 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,8 @@ +[project] +name = "localtuya" +version = "0.0.0" +requires-python = ">= 3.12" + [tool.black] target-version = ["py38", "py39"] include = 'custom_components/localtuya/.*\.py' diff --git a/requirements_test.txt b/requirements_test.txt index d67e17e88..944b279e6 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -5,4 +5,4 @@ mypy==0.901 pydocstyle==6.1.1 pylint==2.8.2 pylint-strict-informational==0.1 -homeassistant==2023.05.0 +pytest-homeassistant-custom-component==0.13.202 diff --git a/setup.cfg b/setup.cfg index c4dd99f36..f05b0fab3 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,3 +1,7 @@ +[tool:pytest] +asyncio_default_fixture_loop_scope = function +asyncio_mode = auto + [flake8] exclude = .git,.tox max-line-length = 120 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 000000000..1aa2c2490 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,20 @@ +from typing import Any +import pytest + +from homeassistant.const import CONF_DISCOVERY + +from custom_components.localtuya import DOMAIN + + +@pytest.fixture(autouse=True) +def auto_enable_custom_integrations(enable_custom_integrations): + return + + +@pytest.fixture(name="config") +def config_fixture() -> dict[str, Any]: + return { + DOMAIN: { + CONF_DISCOVERY: False, + }, + } diff --git a/tests/test_init.py b/tests/test_init.py new file mode 100644 index 000000000..1c6967c74 --- /dev/null +++ b/tests/test_init.py @@ -0,0 +1,9 @@ +from homeassistant.core import HomeAssistant +from homeassistant.helpers.typing import ConfigType +from homeassistant.setup import async_setup_component + +from custom_components.localtuya.const import DOMAIN + + +async def test_async_setup(hass: HomeAssistant, config: ConfigType): + assert await async_setup_component(hass, DOMAIN, config) diff --git a/tox.ini b/tox.ini index a0b7eacf7..001ddcc41 100644 --- a/tox.ini +++ b/tox.ini @@ -18,8 +18,7 @@ setenv = deps = -r{toxinidir}/requirements_test.txt commands = - true # TODO: Run tests later - #pytest -n auto --log-level=debug -v --timeout=30 --durations=10 {posargs} + pytest -n auto --basetemp="{envtmpdir}" {posargs} [testenv:lint] ignore_errors = True