From 221480add1d37aca66bb902f812802ec99b4d451 Mon Sep 17 00:00:00 2001 From: Arie Catsman <120491684+catsmanac@users.noreply.github.com> Date: Sat, 20 Jul 2024 11:20:46 +0200 Subject: [PATCH] Improve switch platform test COV for enphase_envoy (#122227) --- tests/components/enphase_envoy/test_switch.py | 109 +++++++++++++++++- 1 file changed, 107 insertions(+), 2 deletions(-) diff --git a/tests/components/enphase_envoy/test_switch.py b/tests/components/enphase_envoy/test_switch.py index 092e63213a9..5a549257685 100644 --- a/tests/components/enphase_envoy/test_switch.py +++ b/tests/components/enphase_envoy/test_switch.py @@ -12,6 +12,8 @@ from homeassistant.const import ( SERVICE_TOGGLE, SERVICE_TURN_OFF, SERVICE_TURN_ON, + STATE_CLOSED, + STATE_OFF, STATE_ON, ) from homeassistant.core import HomeAssistant @@ -64,12 +66,12 @@ async def test_no_switch( @pytest.mark.parametrize( ("mock_envoy"), ["envoy_metered_batt_relay"], indirect=["mock_envoy"] ) -async def test_switch_operation( +async def test_switch_grid_operation( hass: HomeAssistant, mock_envoy: AsyncMock, config_entry: MockConfigEntry, ) -> None: - """Test switch platform operation.""" + """Test switch platform operation for grid switches.""" with patch("homeassistant.components.enphase_envoy.PLATFORMS", [Platform.SWITCH]): await setup_integration(hass, config_entry) @@ -106,3 +108,106 @@ async def test_switch_operation( blocking=True, ) mock_envoy.go_off_grid.assert_awaited_once_with() + mock_envoy.go_off_grid.reset_mock() + + test_entity = f"{Platform.SWITCH}.enpower_{sn}_charge_from_grid" + + # validate envoy value is reflected in entity + assert (entity_state := hass.states.get(test_entity)) + assert entity_state.state == STATE_ON + + # test grid status switch operation + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: test_entity}, + blocking=True, + ) + mock_envoy.disable_charge_from_grid.assert_awaited_once_with() + mock_envoy.disable_charge_from_grid.reset_mock() + + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: test_entity}, + blocking=True, + ) + mock_envoy.enable_charge_from_grid.assert_awaited_once_with() + mock_envoy.enable_charge_from_grid.reset_mock() + + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TOGGLE, + {ATTR_ENTITY_ID: test_entity}, + blocking=True, + ) + mock_envoy.disable_charge_from_grid.assert_awaited_once_with() + mock_envoy.disable_charge_from_grid.reset_mock() + + +@pytest.mark.parametrize( + ("mock_envoy"), ["envoy_metered_batt_relay"], indirect=["mock_envoy"] +) +async def test_switch_relay_operation( + hass: HomeAssistant, + mock_envoy: AsyncMock, + config_entry: MockConfigEntry, +) -> None: + """Test enphase_envoy switch relay entities operation.""" + with patch("homeassistant.components.enphase_envoy.PLATFORMS", [Platform.SWITCH]): + await setup_integration(hass, config_entry) + + entity_base = f"{Platform.SWITCH}." + + for contact_id, dry_contact in mock_envoy.data.dry_contact_settings.items(): + name = dry_contact.load_name.lower().replace(" ", "_") + test_entity = f"{entity_base}{name}" + target_value = mock_envoy.data.dry_contact_status[contact_id].status + assert (entity_state := hass.states.get(test_entity)) + assert ( + entity_state.state == STATE_ON + if target_value == STATE_CLOSED + else STATE_OFF + ) + + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: test_entity}, + blocking=True, + ) + + mock_envoy.open_dry_contact.assert_awaited_once_with(contact_id) + mock_envoy.close_dry_contact.assert_not_awaited() + mock_envoy.open_dry_contact.reset_mock() + + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: test_entity}, + blocking=True, + ) + + mock_envoy.close_dry_contact.assert_awaited_once_with(contact_id) + mock_envoy.open_dry_contact.assert_not_awaited() + mock_envoy.close_dry_contact.reset_mock() + + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TOGGLE, + {ATTR_ENTITY_ID: test_entity}, + blocking=True, + ) + + assert ( + mock_envoy.open_dry_contact.await_count + if target_value == STATE_CLOSED + else mock_envoy.close_dry_contact.await_count + ) == 1 + assert ( + mock_envoy.close_dry_contact.await_count + if target_value == STATE_CLOSED + else mock_envoy.open_dry_contact.await_count + ) == 0 + mock_envoy.open_dry_contact.reset_mock() + mock_envoy.close_dry_contact.reset_mock()