Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic test infrastructure #466

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tox.yaml
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ jobs:
platform:
- ubuntu-latest
python-version:
- '3.10'
- '3.12'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to bump Python version for two reasons:

  1. localtuya actually doesn't work with Python < 3.11 because of from typing import Self
  2. Drop Python 3.11 support home-assistant/core#114220

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*~
__pycache__
.idea
.tox
tuyadebug/
.pre-commit-config.yaml
28 changes: 21 additions & 7 deletions custom_components/localtuya/__init__.py
Original file line number Diff line number Diff line change
@@ -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

5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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'
2 changes: 1 addition & 1 deletion requirements_test.txt
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[tool:pytest]
asyncio_default_fixture_loop_scope = function
asyncio_mode = auto

[flake8]
exclude = .git,.tox
max-line-length = 120
Empty file added tests/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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,
},
}
9 changes: 9 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 1 addition & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -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
Loading