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
This commit is contained in:
Arie Catsman 2024-07-17 14:51:02 +02:00 committed by GitHub
parent 57e233d30f
commit 1e8da192b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 339 additions and 0 deletions

View File

@ -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': <ANY>,
'device_class': None,
'device_id': <ANY>,
'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': <ANY>,
'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': <ANY>,
'entity_id': 'switch.enpower_654321_charge_from_grid',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'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': <ANY>,
'device_class': None,
'device_id': <ANY>,
'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': <ANY>,
'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': <ANY>,
'entity_id': 'switch.enpower_654321_grid_enabled',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
# name: test_switch[envoy_metered_batt_relay][switch.nc1_fixture-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': None,
'entity_id': 'switch.nc1_fixture',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'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': <ANY>,
'entity_id': 'switch.nc1_fixture',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_switch[envoy_metered_batt_relay][switch.nc2_fixture-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': None,
'entity_id': 'switch.nc2_fixture',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'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': <ANY>,
'entity_id': 'switch.nc2_fixture',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
# name: test_switch[envoy_metered_batt_relay][switch.nc3_fixture-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': None,
'entity_id': 'switch.nc3_fixture',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'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': <ANY>,
'entity_id': 'switch.nc3_fixture',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---

View File

@ -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()