Arie Catsman 1e8da192b6
Add switch platform test to enphase_envoy (#122068)
* Add switch platform test to enphase_envoy

* review feedback Use HA SWITCH_DOMAIN

* Only use SWITCH_DOMAIN for service call
2024-07-17 14:51:02 +02:00

109 lines
3.2 KiB
Python

"""Test Enphase Envoy switch platform."""
from unittest.mock import AsyncMock, patch
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.enphase_envoy.const import Platform
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TOGGLE,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_ON,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import setup_integration
from tests.common import MockConfigEntry, snapshot_platform
@pytest.mark.parametrize(
("mock_envoy"), ["envoy_metered_batt_relay"], indirect=["mock_envoy"]
)
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_switch(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
mock_envoy: AsyncMock,
config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test switch platform entities against snapshot."""
with patch("homeassistant.components.enphase_envoy.PLATFORMS", [Platform.SWITCH]):
await setup_integration(hass, config_entry)
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
@pytest.mark.parametrize(
("mock_envoy"),
[
"envoy",
"envoy_1p_metered",
"envoy_nobatt_metered_3p",
"envoy_tot_cons_metered",
],
indirect=["mock_envoy"],
)
async def test_no_switch(
hass: HomeAssistant,
mock_envoy: AsyncMock,
config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test switch platform entities are not created."""
with patch("homeassistant.components.enphase_envoy.PLATFORMS", [Platform.SWITCH]):
await setup_integration(hass, config_entry)
assert not er.async_entries_for_config_entry(entity_registry, config_entry.entry_id)
@pytest.mark.parametrize(
("mock_envoy"), ["envoy_metered_batt_relay"], indirect=["mock_envoy"]
)
async def test_switch_operation(
hass: HomeAssistant,
mock_envoy: AsyncMock,
config_entry: MockConfigEntry,
) -> None:
"""Test switch platform operation."""
with patch("homeassistant.components.enphase_envoy.PLATFORMS", [Platform.SWITCH]):
await setup_integration(hass, config_entry)
sn = mock_envoy.data.enpower.serial_number
test_entity = f"{Platform.SWITCH}.enpower_{sn}_grid_enabled"
# 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.go_off_grid.assert_awaited_once_with()
mock_envoy.go_off_grid.reset_mock()
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: test_entity},
blocking=True,
)
mock_envoy.go_on_grid.assert_awaited_once_with()
mock_envoy.go_on_grid.reset_mock()
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TOGGLE,
{ATTR_ENTITY_ID: test_entity},
blocking=True,
)
mock_envoy.go_off_grid.assert_awaited_once_with()