core/tests/components/smlight/conftest.py

75 lines
2.2 KiB
Python
Raw Normal View History

New Integration: SMLIGHT SLZB-06 Adapters Integration (#118675) * Initial SMLIGHT integration Signed-off-by: Tim Lunn <tl@smlight.tech> * Generated content Signed-off-by: Tim Lunn <tl@smlight.tech> * Cleanup LOGGING * Use runtime data * Call super first * coordinator instance attributes * Move coordinatorEntity and attr to base class * cleanup sensors * update strings to use sentence case * Improve reauth flow on incorrect credentials * Use fixture for config_flow tests and test to completion * Split uptime hndling into a new uptime sensor entity * Drop server side events and internet callback will bring this back with binary sensor Platform * consolidate coordinator setup * entity always include connections * get_hostname tweak * Add tests for init, coordinator and sensor * Use custom type SmConfigEntry * update sensor snapshot * Drop reauth flow for later PR * Use _async_setup for initial setup * drop internet to be set later * sensor fixes * config flow re * typing fixes * Bump pysmlight dependency to 0.0.12 * dont trigger invalid auth message when first loading auth step * Merge uptime sensors back into main sensor class * clarify uptime handling * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * address review comments * pass host as parameter to the dataCoordinator * drop uptime sensors for a later PR * update sensor test snapshot * move coordinator unique_id to _async_setup * fix CI * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * drop invalid_auth test tag * use snapshot_platform, update fixtures * Finish all tests with abort or create entry * drop coordinator tests and remove hostname support * add test for update failure on connection error * use freezer for update_failed test * fix pysmlight imports --------- Signed-off-by: Tim Lunn <tl@smlight.tech> Co-authored-by: Tim Lunn <tim@feathertop.org> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-08-20 08:44:06 +00:00
"""Common fixtures for the SMLIGHT Zigbee tests."""
from collections.abc import Generator
from unittest.mock import AsyncMock, MagicMock, patch
from pysmlight.web import Info, Sensors
import pytest
from homeassistant.components.smlight.const import DOMAIN
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry, load_json_object_fixture
MOCK_HOST = "slzb-06.local"
MOCK_USERNAME = "test-user"
MOCK_PASSWORD = "test-pass"
@pytest.fixture
def mock_config_entry() -> MockConfigEntry:
"""Return the default mocked config entry."""
return MockConfigEntry(
domain=DOMAIN,
data={
CONF_HOST: MOCK_HOST,
CONF_USERNAME: MOCK_USERNAME,
CONF_PASSWORD: MOCK_PASSWORD,
},
unique_id="aa:bb:cc:dd:ee:ff",
)
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
"""Override async_setup_entry."""
with patch(
"homeassistant.components.smlight.async_setup_entry", return_value=True
) as mock_setup_entry:
yield mock_setup_entry
@pytest.fixture
def mock_smlight_client(request: pytest.FixtureRequest) -> Generator[MagicMock]:
"""Mock the SMLIGHT API client."""
with (
patch(
"homeassistant.components.smlight.coordinator.Api2", autospec=True
) as smlight_mock,
patch("homeassistant.components.smlight.config_flow.Api2", new=smlight_mock),
):
api = smlight_mock.return_value
api.host = MOCK_HOST
api.get_info.return_value = Info.from_dict(
load_json_object_fixture("info.json", DOMAIN)
)
api.get_sensors.return_value = Sensors.from_dict(
load_json_object_fixture("sensors.json", DOMAIN)
)
api.check_auth_needed.return_value = False
api.authenticate.return_value = True
yield api
async def setup_integration(hass: HomeAssistant, mock_config_entry: MockConfigEntry):
"""Set up the integration."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
return mock_config_entry