core/tests/components/hydrawise/conftest.py

108 lines
3.0 KiB
Python
Raw Normal View History

Add config flow to Hydrawise (#95589) * Add config flow to Hydrawise * Raise an issue when a YAML config is detected * Add a test for YAML import * Add missing __init__.py * Update CODEOWNERS * Update requirements_test_all.txt * Add config flow data to strings.json * Hande scan_interval not being in YAML on import * Fix requirements * Update deprecation dates * Update requirements_test_all.txt * Changes from review * Update homeassistant/components/hydrawise/__init__.py Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Add already_configured to strings.json * Add back setup_platform functions * Apply suggestions from code review Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Add back setup_platform * Update requirements_test_all.txt * Run black on hydrawise/*.py * Add missing import of HOMEASSISTANT_DOMAIN * Use more specific errors in config flow * Add additional tests * Update config flow to use pydrawise.legacy * Re-work YAML deprecation issues * Revert some changes to binary_sensor, as requested in review * Changes requested during review * Apply suggestions from code review Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Remove unused STE_USER_DATA_SCHEMA Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Update comment in setup_platform * Re-work the config flow again * Apply suggestions from code review Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Update tests * Add back the _default_watering_timer attribute * Bump deprecation dates * Update requirements_test_all.txt * Update CODEOWNERS --------- Co-authored-by: G Johansson <goran.johansson@shiftit.se> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2023-09-23 22:03:07 +00:00
"""Common fixtures for the Hydrawise tests."""
from collections.abc import Generator
from typing import Any
from unittest.mock import AsyncMock, Mock, patch
Add config flow to Hydrawise (#95589) * Add config flow to Hydrawise * Raise an issue when a YAML config is detected * Add a test for YAML import * Add missing __init__.py * Update CODEOWNERS * Update requirements_test_all.txt * Add config flow data to strings.json * Hande scan_interval not being in YAML on import * Fix requirements * Update deprecation dates * Update requirements_test_all.txt * Changes from review * Update homeassistant/components/hydrawise/__init__.py Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Add already_configured to strings.json * Add back setup_platform functions * Apply suggestions from code review Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Add back setup_platform * Update requirements_test_all.txt * Run black on hydrawise/*.py * Add missing import of HOMEASSISTANT_DOMAIN * Use more specific errors in config flow * Add additional tests * Update config flow to use pydrawise.legacy * Re-work YAML deprecation issues * Revert some changes to binary_sensor, as requested in review * Changes requested during review * Apply suggestions from code review Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Remove unused STE_USER_DATA_SCHEMA Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Update comment in setup_platform * Re-work the config flow again * Apply suggestions from code review Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Update tests * Add back the _default_watering_timer attribute * Bump deprecation dates * Update requirements_test_all.txt * Update CODEOWNERS --------- Co-authored-by: G Johansson <goran.johansson@shiftit.se> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2023-09-23 22:03:07 +00:00
import pytest
from homeassistant.components.hydrawise.const import DOMAIN
from homeassistant.const import CONF_API_KEY
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
Add config flow to Hydrawise (#95589) * Add config flow to Hydrawise * Raise an issue when a YAML config is detected * Add a test for YAML import * Add missing __init__.py * Update CODEOWNERS * Update requirements_test_all.txt * Add config flow data to strings.json * Hande scan_interval not being in YAML on import * Fix requirements * Update deprecation dates * Update requirements_test_all.txt * Changes from review * Update homeassistant/components/hydrawise/__init__.py Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Add already_configured to strings.json * Add back setup_platform functions * Apply suggestions from code review Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Add back setup_platform * Update requirements_test_all.txt * Run black on hydrawise/*.py * Add missing import of HOMEASSISTANT_DOMAIN * Use more specific errors in config flow * Add additional tests * Update config flow to use pydrawise.legacy * Re-work YAML deprecation issues * Revert some changes to binary_sensor, as requested in review * Changes requested during review * Apply suggestions from code review Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Remove unused STE_USER_DATA_SCHEMA Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Update comment in setup_platform * Re-work the config flow again * Apply suggestions from code review Co-authored-by: G Johansson <goran.johansson@shiftit.se> * Update tests * Add back the _default_watering_timer attribute * Bump deprecation dates * Update requirements_test_all.txt * Update CODEOWNERS --------- Co-authored-by: G Johansson <goran.johansson@shiftit.se> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2023-09-23 22:03:07 +00:00
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
"""Override async_setup_entry."""
with patch(
"homeassistant.components.hydrawise.async_setup_entry", return_value=True
) as mock_setup_entry:
yield mock_setup_entry
@pytest.fixture
def mock_pydrawise(
mock_controller: dict[str, Any],
mock_zones: list[dict[str, Any]],
) -> Generator[Mock, None, None]:
"""Mock LegacyHydrawise."""
with patch("pydrawise.legacy.LegacyHydrawise", autospec=True) as mock_pydrawise:
mock_pydrawise.return_value.controller_info = {"controllers": [mock_controller]}
mock_pydrawise.return_value.current_controller = mock_controller
mock_pydrawise.return_value.controller_status = {"relays": mock_zones}
mock_pydrawise.return_value.relays = mock_zones
mock_pydrawise.return_value.relays_by_zone_number = {
r["relay"]: r for r in mock_zones
}
yield mock_pydrawise.return_value
@pytest.fixture
def mock_controller() -> dict[str, Any]:
"""Mock Hydrawise controller."""
return {
"name": "Home Controller",
"last_contact": 1693292420,
"serial_number": "0310b36090",
"controller_id": 52496,
"status": "Unknown",
}
@pytest.fixture
def mock_zones() -> list[dict[str, Any]]:
"""Mock Hydrawise zones."""
return [
{
"name": "Zone One",
"period": 259200,
"relay": 1,
"relay_id": 5965394,
"run": 1800,
"stop": 1,
"time": 330597,
"timestr": "Sat",
"type": 1,
},
{
"name": "Zone Two",
"period": 259200,
"relay": 2,
"relay_id": 5965395,
"run": 1788,
"stop": 1,
"time": 1,
"timestr": "Now",
"type": 106,
},
]
@pytest.fixture
def mock_config_entry() -> MockConfigEntry:
"""Mock ConfigEntry."""
return MockConfigEntry(
title="Hydrawise",
domain=DOMAIN,
data={
CONF_API_KEY: "abc123",
},
unique_id="hydrawise-customerid",
)
@pytest.fixture
async def mock_added_config_entry(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_pydrawise: Mock,
) -> MockConfigEntry:
"""Mock ConfigEntry that's been added to HA."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert DOMAIN in hass.config_entries.async_domains()
return mock_config_entry