diff --git a/.coveragerc b/.coveragerc index 2c768060108..30ea684740d 100644 --- a/.coveragerc +++ b/.coveragerc @@ -172,7 +172,6 @@ omit = homeassistant/components/denonavr/media_player.py homeassistant/components/denonavr/receiver.py homeassistant/components/deutsche_bahn/sensor.py - homeassistant/components/devolo_home_control/__init__.py homeassistant/components/devolo_home_control/binary_sensor.py homeassistant/components/devolo_home_control/climate.py homeassistant/components/devolo_home_control/const.py diff --git a/tests/components/devolo_home_control/__init__.py b/tests/components/devolo_home_control/__init__.py index 5e1e323cad8..5ffc0781c84 100644 --- a/tests/components/devolo_home_control/__init__.py +++ b/tests/components/devolo_home_control/__init__.py @@ -1 +1,19 @@ """Tests for the devolo_home_control integration.""" + +from homeassistant.components.devolo_home_control.const import DOMAIN +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry + + +def configure_integration(hass: HomeAssistant) -> MockConfigEntry: + """Configure the integration.""" + config = { + "username": "test-username", + "password": "test-password", + "mydevolo_url": "https://test_mydevolo_url.test", + } + entry = MockConfigEntry(domain=DOMAIN, data=config, unique_id="123456") + entry.add_to_hass(hass) + + return entry diff --git a/tests/components/devolo_home_control/conftest.py b/tests/components/devolo_home_control/conftest.py new file mode 100644 index 00000000000..487831b0fa4 --- /dev/null +++ b/tests/components/devolo_home_control/conftest.py @@ -0,0 +1,33 @@ +"""Fixtures for tests.""" + +from unittest.mock import patch + +import pytest + + +def pytest_configure(config): + """Define custom markers.""" + config.addinivalue_line( + "markers", + "credentials_invalid: Treat credentials as invalid.", + ) + config.addinivalue_line( + "markers", + "maintenance: Set maintenance mode to on.", + ) + + +@pytest.fixture(autouse=True) +def patch_mydevolo(request): + """Fixture to patch mydevolo into a desired state.""" + with patch( + "homeassistant.components.devolo_home_control.Mydevolo.credentials_valid", + return_value=not bool(request.node.get_closest_marker("credentials_invalid")), + ), patch( + "homeassistant.components.devolo_home_control.Mydevolo.maintenance", + return_value=bool(request.node.get_closest_marker("maintenance")), + ), patch( + "homeassistant.components.devolo_home_control.Mydevolo.get_gateway_ids", + return_value=["1400000000000001", "1400000000000002"], + ): + yield diff --git a/tests/components/devolo_home_control/test_init.py b/tests/components/devolo_home_control/test_init.py new file mode 100644 index 00000000000..f45400716f8 --- /dev/null +++ b/tests/components/devolo_home_control/test_init.py @@ -0,0 +1,71 @@ +"""Tests for the devolo Home Control integration.""" +from unittest.mock import patch + +from devolo_home_control_api.exceptions.gateway import GatewayOfflineError +import pytest + +from homeassistant.config_entries import ( + ENTRY_STATE_LOADED, + ENTRY_STATE_NOT_LOADED, + ENTRY_STATE_SETUP_ERROR, + ENTRY_STATE_SETUP_RETRY, +) +from homeassistant.core import HomeAssistant + +from tests.components.devolo_home_control import configure_integration + + +async def test_setup_entry(hass: HomeAssistant): + """Test setup entry.""" + entry = configure_integration(hass) + with patch("homeassistant.components.devolo_home_control.HomeControl"): + await hass.config_entries.async_setup(entry.entry_id) + assert entry.state == ENTRY_STATE_LOADED + + +@pytest.mark.credentials_invalid +async def test_setup_entry_credentials_invalid(hass: HomeAssistant): + """Test setup entry fails if credentials are invalid.""" + entry = configure_integration(hass) + await hass.config_entries.async_setup(entry.entry_id) + assert entry.state == ENTRY_STATE_SETUP_ERROR + + +@pytest.mark.maintenance +async def test_setup_entry_maintenance(hass: HomeAssistant): + """Test setup entry fails if mydevolo is in maintenance mode.""" + entry = configure_integration(hass) + await hass.config_entries.async_setup(entry.entry_id) + assert entry.state == ENTRY_STATE_SETUP_RETRY + + +async def test_setup_connection_error(hass: HomeAssistant): + """Test setup entry fails on connection error.""" + entry = configure_integration(hass) + with patch( + "homeassistant.components.devolo_home_control.HomeControl", + side_effect=ConnectionError, + ): + await hass.config_entries.async_setup(entry.entry_id) + assert entry.state == ENTRY_STATE_SETUP_RETRY + + +async def test_setup_gateway_offline(hass: HomeAssistant): + """Test setup entry fails on gateway offline.""" + entry = configure_integration(hass) + with patch( + "homeassistant.components.devolo_home_control.HomeControl", + side_effect=GatewayOfflineError, + ): + await hass.config_entries.async_setup(entry.entry_id) + assert entry.state == ENTRY_STATE_SETUP_RETRY + + +async def test_unload_entry(hass: HomeAssistant): + """Test unload entry.""" + entry = configure_integration(hass) + with patch("homeassistant.components.devolo_home_control.HomeControl"): + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + await hass.config_entries.async_unload(entry.entry_id) + assert entry.state == ENTRY_STATE_NOT_LOADED