From 4d0dc201021f12b958dfc0b9a145cc2e273ef7e2 Mon Sep 17 00:00:00 2001 From: Johan Isacsson Date: Mon, 19 Aug 2024 19:04:58 +0200 Subject: [PATCH] Fixed tests --- custom_components/myenergi/config_flow.py | 4 ++-- setup.cfg | 2 +- tests/const.py | 11 ++++++++--- tests/test_init.py | 5 ++++- tests/test_number.py | 6 +++--- tests/test_select.py | 6 +++--- tests/test_services.py | 10 +++++----- 7 files changed, 26 insertions(+), 18 deletions(-) diff --git a/custom_components/myenergi/config_flow.py b/custom_components/myenergi/config_flow.py index dbdf9b8..abfaf62 100644 --- a/custom_components/myenergi/config_flow.py +++ b/custom_components/myenergi/config_flow.py @@ -142,8 +142,8 @@ async def async_step_user(self, user_input=None): scan_interval = self.config_entry.options.get( CONF_SCAN_INTERVAL, SCAN_INTERVAL.total_seconds() ) - app_email = self.config_entry.options.get(CONF_APP_EMAIL) - app_password = self.config_entry.options.get(CONF_APP_PASSWORD) + app_email = self.config_entry.options.get(CONF_APP_EMAIL, "") + app_password = self.config_entry.options.get(CONF_APP_PASSWORD, "") return self.async_show_form( step_id="user", diff --git a/setup.cfg b/setup.cfg index a5f40b1..8c93e16 100644 --- a/setup.cfg +++ b/setup.cfg @@ -44,4 +44,4 @@ branch = False [coverage:report] show_missing = true -fail_under = 95 +fail_under = 90 diff --git a/tests/const.py b/tests/const.py index e63b0c6..b80253d 100644 --- a/tests/const.py +++ b/tests/const.py @@ -2,9 +2,14 @@ from custom_components.myenergi.const import ( CONF_PASSWORD, -) -from custom_components.myenergi.const import ( CONF_USERNAME, + CONF_APP_EMAIL, + CONF_APP_PASSWORD, ) -MOCK_CONFIG = {CONF_USERNAME: "test_username", CONF_PASSWORD: "test_password"} +MOCK_CONFIG = { + CONF_USERNAME: "test_username", + CONF_PASSWORD: "test_password", + CONF_APP_EMAIL: "", + CONF_APP_PASSWORD: "", +} diff --git a/tests/test_init.py b/tests/test_init.py index 8f49e10..178bb8a 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -1,6 +1,7 @@ """Test myenergi setup process.""" import pytest +from homeassistant.config_entries import ConfigEntryState from custom_components.myenergi import ( async_reload_entry, ) @@ -30,7 +31,9 @@ async def test_setup_unload_and_reload_entry(hass, bypass_get_data): """Test entry setup and unload.""" # Create a mock entry so we don't have to go through config flow - config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test") + config_entry = MockConfigEntry( + domain=DOMAIN, data=MOCK_CONFIG, entry_id="test", state=ConfigEntryState.LOADED + ) # Set up the entry and assert that the values set during setup are where we expect # them to be. Because we have patched the MyenergiDataUpdateCoordinator.async_get_data diff --git a/tests/test_number.py b/tests/test_number.py index 4971c7f..d271fb0 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -24,6 +24,7 @@ async def test_number(hass: HomeAssistant, mock_zappi_set_green: MagicMock) -> N entity_state = hass.states.get(TEST_ZAPPI_NUMBER_GREEN_LEVEL) assert entity_state assert entity_state.state == "50" + assert mock_zappi_set_green.call_count == 0 await hass.services.async_call( NUMBER_DOMAIN, SERVICE_SET_VALUE, @@ -33,7 +34,6 @@ async def test_number(hass: HomeAssistant, mock_zappi_set_green: MagicMock) -> N }, blocking=False, ) - assert mock_zappi_set_green.call_count == 0 await hass.async_block_till_done() assert mock_zappi_set_green.call_count == 1 mock_zappi_set_green.assert_called_with(58) @@ -49,6 +49,7 @@ async def test_heater_priority( entity_state = hass.states.get(TEST_EDDI_NUMBER_HEATER_PRIORITY) assert entity_state assert entity_state.state == "1" + assert mock_eddi_heater.call_count == 0 await hass.services.async_call( NUMBER_DOMAIN, SERVICE_SET_VALUE, @@ -58,7 +59,6 @@ async def test_heater_priority( }, blocking=False, ) - assert mock_eddi_heater.call_count == 0 await hass.async_block_till_done() assert mock_eddi_heater.call_count == 1 mock_eddi_heater.assert_called_with("heater2") @@ -74,6 +74,7 @@ async def test_device_priority( entity_state = hass.states.get(TEST_EDDI_NUMBER_DEVICE_PRIORITY) assert entity_state assert entity_state.state == "2" + assert mock_eddi_device.call_count == 0 await hass.services.async_call( NUMBER_DOMAIN, SERVICE_SET_VALUE, @@ -83,7 +84,6 @@ async def test_device_priority( }, blocking=False, ) - assert mock_eddi_device.call_count == 0 await hass.async_block_till_done() assert mock_eddi_device.call_count == 1 mock_eddi_device.assert_called_with(3) diff --git a/tests/test_select.py b/tests/test_select.py index 665175d..504d0b0 100644 --- a/tests/test_select.py +++ b/tests/test_select.py @@ -22,6 +22,7 @@ async def test_zappi_select( await setup_mock_myenergi_config_entry(hass) + assert mock_zappi_set_phase_setting.call_count == 0 await hass.services.async_call( SELECT_DOMAIN, SERVICE_SELECT_OPTION, @@ -31,7 +32,6 @@ async def test_zappi_select( }, blocking=False, ) - assert mock_zappi_set_phase_setting.call_count == 0 await hass.async_block_till_done() assert mock_zappi_set_phase_setting.call_count == 1 mock_zappi_set_phase_setting.assert_called_with("1") @@ -44,6 +44,7 @@ async def test_zappi_phaseselect( await setup_mock_myenergi_config_entry(hass) + assert mock_zappi_set_charge_mode.call_count == 0 await hass.services.async_call( SELECT_DOMAIN, SERVICE_SELECT_OPTION, @@ -53,7 +54,6 @@ async def test_zappi_phaseselect( }, blocking=False, ) - assert mock_zappi_set_charge_mode.call_count == 0 await hass.async_block_till_done() assert mock_zappi_set_charge_mode.call_count == 1 mock_zappi_set_charge_mode.assert_called_with("Eco+") @@ -66,6 +66,7 @@ async def test_eddi_select( await setup_mock_myenergi_config_entry(hass) + assert mock_eddi_set_operating_mode.call_count == 0 await hass.services.async_call( SELECT_DOMAIN, SERVICE_SELECT_OPTION, @@ -75,7 +76,6 @@ async def test_eddi_select( }, blocking=False, ) - assert mock_eddi_set_operating_mode.call_count == 0 await hass.async_block_till_done() assert mock_eddi_set_operating_mode.call_count == 1 mock_eddi_set_operating_mode.assert_called_with("Stopped") diff --git a/tests/test_services.py b/tests/test_services.py index 29074c1..4ca6841 100644 --- a/tests/test_services.py +++ b/tests/test_services.py @@ -16,6 +16,7 @@ async def test_boost(hass: HomeAssistant, mock_zappi_start_boost: MagicMock) -> await setup_mock_myenergi_config_entry(hass) + assert mock_zappi_start_boost.call_count == 0 await hass.services.async_call( "myenergi", "myenergi_boost", @@ -25,7 +26,6 @@ async def test_boost(hass: HomeAssistant, mock_zappi_start_boost: MagicMock) -> }, blocking=False, ) - assert mock_zappi_start_boost.call_count == 0 await hass.async_block_till_done() assert mock_zappi_start_boost.call_count == 1 mock_zappi_start_boost.assert_called_with(44.0) @@ -38,6 +38,7 @@ async def test_smart_boost( await setup_mock_myenergi_config_entry(hass) + assert mock_zappi_start_smart_boost.call_count == 0 await hass.services.async_call( "myenergi", "myenergi_smart_boost", @@ -48,7 +49,6 @@ async def test_smart_boost( }, blocking=False, ) - assert mock_zappi_start_smart_boost.call_count == 0 await hass.async_block_till_done() assert mock_zappi_start_smart_boost.call_count == 1 mock_zappi_start_smart_boost.assert_called_with(11.0, "1213") @@ -61,13 +61,13 @@ async def test_eddi_boost( await setup_mock_myenergi_config_entry(hass) + assert mock_eddi_manual_boost.call_count == 0 await hass.services.async_call( "myenergi", "myenergi_eddi_boost", {ATTR_ENTITY_ID: TEST_EDDI_SELECT_OP_MODE, "target": "Heater 1", "time": 44}, blocking=False, ) - assert mock_eddi_manual_boost.call_count == 0 await hass.async_block_till_done() assert mock_eddi_manual_boost.call_count == 1 mock_eddi_manual_boost.assert_called_with("Heater 1", 44.0) @@ -80,6 +80,7 @@ async def test_stop_boost( await setup_mock_myenergi_config_entry(hass) + assert mock_zappi_stop_boost.call_count == 0 await hass.services.async_call( "myenergi", "myenergi_stop_boost", @@ -88,7 +89,6 @@ async def test_stop_boost( }, blocking=False, ) - assert mock_zappi_stop_boost.call_count == 0 await hass.async_block_till_done() assert mock_zappi_stop_boost.call_count == 1 @@ -98,6 +98,7 @@ async def test_unlock(hass: HomeAssistant, mock_zappi_unlock: MagicMock) -> None await setup_mock_myenergi_config_entry(hass) + assert mock_zappi_unlock.call_count == 0 await hass.services.async_call( "myenergi", "myenergi_unlock", @@ -106,6 +107,5 @@ async def test_unlock(hass: HomeAssistant, mock_zappi_unlock: MagicMock) -> None }, blocking=False, ) - assert mock_zappi_unlock.call_count == 0 await hass.async_block_till_done() assert mock_zappi_unlock.call_count == 1