mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Improve switch platform test COV for enphase_envoy (#122227)
This commit is contained in:
parent
153b69c971
commit
221480add1
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user