From 46307286b1ef091ee6079d75a2a28a16a3f3444e Mon Sep 17 00:00:00 2001 From: Jean-Marc Collin Date: Sun, 17 Dec 2023 08:57:08 +0000 Subject: [PATCH] With fixture for init_vtherm_api and init_central_config --- .../versatile_thermostat/vtherm_api.py | 4 ++ tests/conftest.py | 24 ++++++- tests/test_central_config.py | 67 ++++++++++++++----- tests/test_config_flow.py | 11 +-- 4 files changed, 78 insertions(+), 28 deletions(-) diff --git a/custom_components/versatile_thermostat/vtherm_api.py b/custom_components/versatile_thermostat/vtherm_api.py index ee7be72..89e2572 100644 --- a/custom_components/versatile_thermostat/vtherm_api.py +++ b/custom_components/versatile_thermostat/vtherm_api.py @@ -31,6 +31,10 @@ class VersatileThermostatAPI(dict): if VersatileThermostatAPI._hass is None: return None + domain = VersatileThermostatAPI._hass.data.get(DOMAIN) + if not domain: + hass.data.setdefault(DOMAIN, {}) + ret = VersatileThermostatAPI._hass.data.get(DOMAIN).get(VTHERM_API_NAME) if ret is None: ret = VersatileThermostatAPI() diff --git a/tests/conftest.py b/tests/conftest.py index 4e5aa59..570a67c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -26,15 +26,20 @@ from custom_components.versatile_thermostat.config_flow import ( VersatileThermostatBaseConfigFlow, ) +from custom_components.versatile_thermostat.vtherm_api import VersatileThermostatAPI from custom_components.versatile_thermostat.base_thermostat import BaseThermostat +from .commons import create_central_config + pytest_plugins = "pytest_homeassistant_custom_component" # pylint: disable=invalid-name # This fixture enables loading custom integrations in all tests. # Remove to enable selective use of this fixture @pytest.fixture(autouse=True) -def auto_enable_custom_integrations(enable_custom_integrations): # pylint: disable=unused-argument +def auto_enable_custom_integrations( + enable_custom_integrations, +): # pylint: disable=unused-argument """Enable all integration in tests""" yield @@ -108,3 +113,20 @@ def skip_send_event_fixture(): """Skip the send_event in BaseThermostat""" with patch.object(BaseThermostat, "send_event"): yield + + +@pytest.fixture(name="init_vtherm_api") +def init_vtherm_api_fixture(hass): + """Initialize the VTherm API""" + VersatileThermostatAPI.get_vtherm_api(hass) + yield + + +@pytest.fixture(name="init_central_config") +async def init_central_config_fixture( + hass, init_vtherm_api +): # pylint: disable=unused-argument + """Initialize the VTherm API""" + await create_central_config(hass) + + yield diff --git a/tests/test_central_config.py b/tests/test_central_config.py index 4380a45..fd7d27e 100644 --- a/tests/test_central_config.py +++ b/tests/test_central_config.py @@ -5,10 +5,11 @@ from unittest.mock import patch # , call # from datetime import datetime # , timedelta +from homeassistant import data_entry_flow from homeassistant.core import HomeAssistant # from homeassistant.components.climate import HVACAction, HVACMode -from homeassistant.config_entries import ConfigEntryState +from homeassistant.config_entries import ConfigEntryState, SOURCE_USER # from homeassistant.helpers.entity_component import EntityComponent # from homeassistant.components.climate import ClimateEntity, DOMAIN as CLIMATE_DOMAIN @@ -27,10 +28,9 @@ from custom_components.versatile_thermostat.thermostat_switch import ( from custom_components.versatile_thermostat.vtherm_api import VersatileThermostatAPI from .commons import * # pylint: disable=wildcard-import, unused-wildcard-import +from .const import * # pylint: disable=wildcard-import, unused-wildcard-import -# @pytest.mark.parametrize("expected_lingering_tasks", [True]) -# @pytest.mark.parametrize("expected_lingering_timers", [True]) async def test_add_a_central_config(hass: HomeAssistant, skip_hass_states_is_state): """Tests the clean_central_config_doubon of base_thermostat""" central_config_entry = MockConfigEntry( @@ -95,14 +95,10 @@ async def test_add_a_central_config(hass: HomeAssistant, skip_hass_states_is_sta assert central_configuration is not None -# @pytest.mark.parametrize("expected_lingering_tasks", [True]) -# @pytest.mark.parametrize("expected_lingering_timers", [True]) async def test_minimal_over_switch_wo_central_config( - hass: HomeAssistant, skip_hass_states_is_state + hass: HomeAssistant, skip_hass_states_is_state, init_vtherm_api ): """Tests that a VTherm without any central_configuration is working with its own attributes""" - create_central_config(hass) - # Add a Switch VTherm entry = MockConfigEntry( domain=DOMAIN, @@ -173,14 +169,10 @@ async def test_minimal_over_switch_wo_central_config( assert entity.is_inversed -# @pytest.mark.parametrize("expected_lingering_tasks", [True]) -# @pytest.mark.parametrize("expected_lingering_timers", [True]) async def test_full_over_switch_wo_central_config( - hass: HomeAssistant, skip_hass_states_is_state + hass: HomeAssistant, skip_hass_states_is_state, init_vtherm_api ): """Tests that a VTherm without any central_configuration is working with its own attributes""" - await create_central_config(hass) - # Add a Switch VTherm entry = MockConfigEntry( domain=DOMAIN, @@ -289,14 +281,10 @@ async def test_full_over_switch_wo_central_config( assert entity._presence_sensor_entity_id == "binary_sensor.mock_presence_sensor" -# @pytest.mark.parametrize("expected_lingering_tasks", [True]) -# @pytest.mark.parametrize("expected_lingering_timers", [True]) async def test_full_over_switch_with_central_config( - hass: HomeAssistant, skip_hass_states_is_state + hass: HomeAssistant, skip_hass_states_is_state, init_central_config ): """Tests that a VTherm with central_configuration is working with the central_config attributes""" - await create_central_config(hass) - # Add a Switch VTherm entry = MockConfigEntry( domain=DOMAIN, @@ -398,3 +386,46 @@ async def test_full_over_switch_with_central_config( assert entity._max_power_sensor_entity_id == "sensor.mock_max_power_sensor" assert entity._presence_sensor_entity_id == "binary_sensor.mock_presence_sensor" + + +async def test_over_switch_with_central_config_but_no_central_config( + hass: HomeAssistant, skip_hass_states_get, init_vtherm_api +): + """Tests that a VTherm with a central_configuration flag but no central config. Should lead to an error""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["step_id"] == SOURCE_USER + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_THERMOSTAT_TYPE: CONF_THERMOSTAT_SWITCH, + }, + ) + + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["step_id"] == "main" + assert result["errors"] == {} + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_NAME: "TheOverSwitchMockName", + CONF_TEMP_SENSOR: "sensor.mock_temp_sensor", + CONF_CYCLE_MIN: 5, + CONF_DEVICE_POWER: 1, + CONF_USE_WINDOW_FEATURE: True, + CONF_USE_MOTION_FEATURE: False, + CONF_USE_POWER_FEATURE: False, + CONF_USE_PRESENCE_FEATURE: False, + CONF_USE_MAIN_CENTRAL_CONFIG: True, + }, + ) + + assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + # in case of error we stays in main + assert result["step_id"] == "main" + assert result["errors"] == {"use_main_central_config": "no_central_config"} diff --git a/tests/test_config_flow.py b/tests/test_config_flow.py index b93216f..a7f5134 100644 --- a/tests/test_config_flow.py +++ b/tests/test_config_flow.py @@ -13,14 +13,8 @@ from .const import * # pylint: disable=wildcard-import, unused-wildcard-import @pytest.mark.parametrize("expected_lingering_tasks", [True]) @pytest.mark.parametrize("expected_lingering_timers", [True]) -async def test_show_form(hass: HomeAssistant) -> None: +async def test_show_form(hass: HomeAssistant, init_vtherm_api) -> None: """Test that the form is served with no input""" - # Init the API - # hass.data["custom_components"] = None - # loader.async_get_custom_components(hass) - # BaseThermostatAPI(hass) - await create_central_config(hass) - result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER} ) @@ -34,10 +28,9 @@ async def test_show_form(hass: HomeAssistant) -> None: # Disable this test which don't work anymore (kill the pytest !) @pytest.mark.skip async def test_user_config_flow_over_switch( - hass: HomeAssistant, skip_hass_states_get + hass: HomeAssistant, skip_hass_states_get, init_central_config ): # pylint: disable=unused-argument """Test the config flow with all thermostat_over_switch features and central config on""" - await create_central_config(hass) result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER} )