From 1e8da192b6432e246cb8d291382542df79e73b74 Mon Sep 17 00:00:00 2001 From: Arie Catsman <120491684+catsmanac@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:51:02 +0200 Subject: [PATCH] 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 --- .../enphase_envoy/snapshots/test_switch.ambr | 231 ++++++++++++++++++ tests/components/enphase_envoy/test_switch.py | 108 ++++++++ 2 files changed, 339 insertions(+) create mode 100644 tests/components/enphase_envoy/snapshots/test_switch.ambr create mode 100644 tests/components/enphase_envoy/test_switch.py diff --git a/tests/components/enphase_envoy/snapshots/test_switch.ambr b/tests/components/enphase_envoy/snapshots/test_switch.ambr new file mode 100644 index 00000000000..a5dafd735b5 --- /dev/null +++ b/tests/components/enphase_envoy/snapshots/test_switch.ambr @@ -0,0 +1,231 @@ +# serializer version: 1 +# name: test_switch[envoy_metered_batt_relay][switch.enpower_654321_charge_from_grid-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.enpower_654321_charge_from_grid', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Charge from grid', + 'platform': 'enphase_envoy', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'charge_from_grid', + 'unique_id': '654321_charge_from_grid', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch[envoy_metered_batt_relay][switch.enpower_654321_charge_from_grid-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Enpower 654321 Charge from grid', + }), + 'context': , + 'entity_id': 'switch.enpower_654321_charge_from_grid', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_switch[envoy_metered_batt_relay][switch.enpower_654321_grid_enabled-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.enpower_654321_grid_enabled', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Grid enabled', + 'platform': 'enphase_envoy', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'grid_enabled', + 'unique_id': '654321_mains_admin_state', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch[envoy_metered_batt_relay][switch.enpower_654321_grid_enabled-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Enpower 654321 Grid enabled', + }), + 'context': , + 'entity_id': 'switch.enpower_654321_grid_enabled', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_switch[envoy_metered_batt_relay][switch.nc1_fixture-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.nc1_fixture', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': None, + 'platform': 'enphase_envoy', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '654321_relay_NC1_relay_status', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch[envoy_metered_batt_relay][switch.nc1_fixture-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'NC1 Fixture', + }), + 'context': , + 'entity_id': 'switch.nc1_fixture', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_switch[envoy_metered_batt_relay][switch.nc2_fixture-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.nc2_fixture', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': None, + 'platform': 'enphase_envoy', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '654321_relay_NC2_relay_status', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch[envoy_metered_batt_relay][switch.nc2_fixture-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'NC2 Fixture', + }), + 'context': , + 'entity_id': 'switch.nc2_fixture', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_switch[envoy_metered_batt_relay][switch.nc3_fixture-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.nc3_fixture', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': None, + 'platform': 'enphase_envoy', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '654321_relay_NC3_relay_status', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch[envoy_metered_batt_relay][switch.nc3_fixture-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'NC3 Fixture', + }), + 'context': , + 'entity_id': 'switch.nc3_fixture', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- diff --git a/tests/components/enphase_envoy/test_switch.py b/tests/components/enphase_envoy/test_switch.py new file mode 100644 index 00000000000..092e63213a9 --- /dev/null +++ b/tests/components/enphase_envoy/test_switch.py @@ -0,0 +1,108 @@ +"""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()