mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Fix test logic flaw in enphase_envoy test_select (#136570)
* Fix test logic flaw in enphase_envoy test_select * Replace test loops by test parameters * Implement review feedback to Improve use of parametrize parameters
This commit is contained in:
parent
84561b7446
commit
679b7f4032
@ -7,15 +7,12 @@ from syrupy.assertion import SnapshotAssertion
|
|||||||
|
|
||||||
from homeassistant.components.enphase_envoy.const import Platform
|
from homeassistant.components.enphase_envoy.const import Platform
|
||||||
from homeassistant.components.enphase_envoy.select import (
|
from homeassistant.components.enphase_envoy.select import (
|
||||||
ACTION_OPTIONS,
|
|
||||||
MODE_OPTIONS,
|
|
||||||
RELAY_ACTION_MAP,
|
RELAY_ACTION_MAP,
|
||||||
RELAY_MODE_MAP,
|
RELAY_MODE_MAP,
|
||||||
REVERSE_RELAY_ACTION_MAP,
|
REVERSE_RELAY_ACTION_MAP,
|
||||||
REVERSE_RELAY_MODE_MAP,
|
REVERSE_RELAY_MODE_MAP,
|
||||||
REVERSE_STORAGE_MODE_MAP,
|
REVERSE_STORAGE_MODE_MAP,
|
||||||
STORAGE_MODE_MAP,
|
STORAGE_MODE_MAP,
|
||||||
STORAGE_MODE_OPTIONS,
|
|
||||||
)
|
)
|
||||||
from homeassistant.components.select import ATTR_OPTION, DOMAIN as SELECT_DOMAIN
|
from homeassistant.components.select import ATTR_OPTION, DOMAIN as SELECT_DOMAIN
|
||||||
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_SELECT_OPTION
|
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_SELECT_OPTION
|
||||||
@ -28,9 +25,9 @@ from tests.common import MockConfigEntry, snapshot_platform
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("mock_envoy"),
|
"mock_envoy",
|
||||||
["envoy_metered_batt_relay", "envoy_eu_batt"],
|
["envoy_metered_batt_relay", "envoy_eu_batt"],
|
||||||
indirect=["mock_envoy"],
|
indirect=True,
|
||||||
)
|
)
|
||||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||||
async def test_select(
|
async def test_select(
|
||||||
@ -47,14 +44,14 @@ async def test_select(
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("mock_envoy"),
|
"mock_envoy",
|
||||||
[
|
[
|
||||||
"envoy",
|
"envoy",
|
||||||
"envoy_1p_metered",
|
"envoy_1p_metered",
|
||||||
"envoy_nobatt_metered_3p",
|
"envoy_nobatt_metered_3p",
|
||||||
"envoy_tot_cons_metered",
|
"envoy_tot_cons_metered",
|
||||||
],
|
],
|
||||||
indirect=["mock_envoy"],
|
indirect=True,
|
||||||
)
|
)
|
||||||
async def test_no_select(
|
async def test_no_select(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
@ -68,13 +65,31 @@ async def test_no_select(
|
|||||||
assert not er.async_entries_for_config_entry(entity_registry, config_entry.entry_id)
|
assert not er.async_entries_for_config_entry(entity_registry, config_entry.entry_id)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("mock_envoy", ["envoy_metered_batt_relay"], indirect=True)
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("mock_envoy"), ["envoy_metered_batt_relay"], indirect=["mock_envoy"]
|
("relay", "target", "expected_state", "call_parameter"),
|
||||||
|
[
|
||||||
|
("NC1", "generator_action", "shed", "generator_action"),
|
||||||
|
("NC1", "microgrid_action", "shed", "micro_grid_action"),
|
||||||
|
("NC1", "grid_action", "shed", "grid_action"),
|
||||||
|
("NC2", "generator_action", "shed", "generator_action"),
|
||||||
|
("NC2", "microgrid_action", "shed", "micro_grid_action"),
|
||||||
|
("NC2", "grid_action", "apply", "grid_action"),
|
||||||
|
("NC3", "generator_action", "apply", "generator_action"),
|
||||||
|
("NC3", "microgrid_action", "apply", "micro_grid_action"),
|
||||||
|
("NC3", "grid_action", "shed", "grid_action"),
|
||||||
|
],
|
||||||
)
|
)
|
||||||
|
@pytest.mark.parametrize("action", ["powered", "not_powered", "schedule", "none"])
|
||||||
async def test_select_relay_actions(
|
async def test_select_relay_actions(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mock_envoy: AsyncMock,
|
mock_envoy: AsyncMock,
|
||||||
config_entry: MockConfigEntry,
|
config_entry: MockConfigEntry,
|
||||||
|
target: str,
|
||||||
|
expected_state: str,
|
||||||
|
call_parameter: str,
|
||||||
|
relay: str,
|
||||||
|
action: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test select platform entities dry contact relay actions."""
|
"""Test select platform entities dry contact relay actions."""
|
||||||
with patch("homeassistant.components.enphase_envoy.PLATFORMS", [Platform.SELECT]):
|
with patch("homeassistant.components.enphase_envoy.PLATFORMS", [Platform.SELECT]):
|
||||||
@ -82,18 +97,14 @@ async def test_select_relay_actions(
|
|||||||
|
|
||||||
entity_base = f"{Platform.SELECT}."
|
entity_base = f"{Platform.SELECT}."
|
||||||
|
|
||||||
for contact_id, dry_contact in mock_envoy.data.dry_contact_settings.items():
|
assert (dry_contact := mock_envoy.data.dry_contact_settings[relay])
|
||||||
name = dry_contact.load_name.lower().replace(" ", "_")
|
assert (name := dry_contact.load_name.lower().replace(" ", "_"))
|
||||||
for target in (
|
|
||||||
("generator_action", dry_contact.generator_action, "generator_action"),
|
test_entity = f"{entity_base}{name}_{target}"
|
||||||
("microgrid_action", dry_contact.micro_grid_action, "micro_grid_action"),
|
|
||||||
("grid_action", dry_contact.grid_action, "grid_action"),
|
|
||||||
):
|
|
||||||
test_entity = f"{entity_base}{name}_{target[0]}"
|
|
||||||
assert (entity_state := hass.states.get(test_entity))
|
assert (entity_state := hass.states.get(test_entity))
|
||||||
assert RELAY_ACTION_MAP[target[1]] == (current_state := entity_state.state)
|
assert entity_state.state == RELAY_ACTION_MAP[expected_state]
|
||||||
# set all relay modes except current mode
|
|
||||||
for action in [action for action in ACTION_OPTIONS if not current_state]:
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
SELECT_DOMAIN,
|
SELECT_DOMAIN,
|
||||||
SERVICE_SELECT_OPTION,
|
SERVICE_SELECT_OPTION,
|
||||||
@ -104,32 +115,19 @@ async def test_select_relay_actions(
|
|||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
mock_envoy.update_dry_contact.assert_called_once_with(
|
mock_envoy.update_dry_contact.assert_called_once_with(
|
||||||
{"id": contact_id, target[2]: REVERSE_RELAY_ACTION_MAP[action]}
|
{"id": relay, call_parameter: REVERSE_RELAY_ACTION_MAP[action]}
|
||||||
)
|
)
|
||||||
mock_envoy.update_dry_contact.reset_mock()
|
|
||||||
# and finally back to original
|
|
||||||
await hass.services.async_call(
|
|
||||||
SELECT_DOMAIN,
|
|
||||||
SERVICE_SELECT_OPTION,
|
|
||||||
{
|
|
||||||
ATTR_ENTITY_ID: test_entity,
|
|
||||||
ATTR_OPTION: current_state,
|
|
||||||
},
|
|
||||||
blocking=True,
|
|
||||||
)
|
|
||||||
mock_envoy.update_dry_contact.assert_called_once_with(
|
|
||||||
{"id": contact_id, target[2]: REVERSE_RELAY_ACTION_MAP[current_state]}
|
|
||||||
)
|
|
||||||
mock_envoy.update_dry_contact.reset_mock()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize("mock_envoy", ["envoy_metered_batt_relay"], indirect=True)
|
||||||
("mock_envoy"), ["envoy_metered_batt_relay"], indirect=["mock_envoy"]
|
@pytest.mark.parametrize("relay_mode", ["battery", "standard"])
|
||||||
)
|
@pytest.mark.parametrize("relay", ["NC1", "NC2", "NC3"])
|
||||||
async def test_select_relay_modes(
|
async def test_select_relay_modes(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mock_envoy: AsyncMock,
|
mock_envoy: AsyncMock,
|
||||||
config_entry: MockConfigEntry,
|
config_entry: MockConfigEntry,
|
||||||
|
relay_mode: str,
|
||||||
|
relay: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test select platform dry contact relay mode changes."""
|
"""Test select platform dry contact relay mode changes."""
|
||||||
with patch("homeassistant.components.enphase_envoy.PLATFORMS", [Platform.SELECT]):
|
with patch("homeassistant.components.enphase_envoy.PLATFORMS", [Platform.SELECT]):
|
||||||
@ -137,40 +135,26 @@ async def test_select_relay_modes(
|
|||||||
|
|
||||||
entity_base = f"{Platform.SELECT}."
|
entity_base = f"{Platform.SELECT}."
|
||||||
|
|
||||||
for contact_id, dry_contact in mock_envoy.data.dry_contact_settings.items():
|
assert (dry_contact := mock_envoy.data.dry_contact_settings[relay])
|
||||||
name = dry_contact.load_name.lower().replace(" ", "_")
|
assert (name := dry_contact.load_name.lower().replace(" ", "_"))
|
||||||
test_entity = f"{entity_base}{name}_mode"
|
|
||||||
assert (entity_state := hass.states.get(test_entity))
|
test_entity = f"{entity_base}{name}_mode"
|
||||||
assert RELAY_MODE_MAP[dry_contact.mode] == (current_state := entity_state.state)
|
|
||||||
for mode in [mode for mode in MODE_OPTIONS if not current_state]:
|
assert (entity_state := hass.states.get(test_entity))
|
||||||
await hass.services.async_call(
|
assert entity_state.state == RELAY_MODE_MAP[dry_contact.mode]
|
||||||
SELECT_DOMAIN,
|
|
||||||
SERVICE_SELECT_OPTION,
|
|
||||||
{
|
|
||||||
ATTR_ENTITY_ID: test_entity,
|
|
||||||
ATTR_OPTION: mode,
|
|
||||||
},
|
|
||||||
blocking=True,
|
|
||||||
)
|
|
||||||
mock_envoy.update_dry_contact.assert_called_once_with(
|
|
||||||
{"id": contact_id, "mode": REVERSE_RELAY_MODE_MAP[mode]}
|
|
||||||
)
|
|
||||||
mock_envoy.update_dry_contact.reset_mock()
|
|
||||||
|
|
||||||
# and finally current mode again
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
SELECT_DOMAIN,
|
SELECT_DOMAIN,
|
||||||
SERVICE_SELECT_OPTION,
|
SERVICE_SELECT_OPTION,
|
||||||
{
|
{
|
||||||
ATTR_ENTITY_ID: test_entity,
|
ATTR_ENTITY_ID: test_entity,
|
||||||
ATTR_OPTION: current_state,
|
ATTR_OPTION: relay_mode,
|
||||||
},
|
},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
mock_envoy.update_dry_contact.assert_called_once_with(
|
mock_envoy.update_dry_contact.assert_called_once_with(
|
||||||
{"id": contact_id, "mode": REVERSE_RELAY_MODE_MAP[current_state]}
|
{"id": relay, "mode": REVERSE_RELAY_MODE_MAP[relay_mode]}
|
||||||
)
|
)
|
||||||
mock_envoy.update_dry_contact.reset_mock()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
@ -181,11 +165,13 @@ async def test_select_relay_modes(
|
|||||||
],
|
],
|
||||||
indirect=["mock_envoy"],
|
indirect=["mock_envoy"],
|
||||||
)
|
)
|
||||||
|
@pytest.mark.parametrize(("mode"), ["backup", "self_consumption", "savings"])
|
||||||
async def test_select_storage_modes(
|
async def test_select_storage_modes(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mock_envoy: AsyncMock,
|
mock_envoy: AsyncMock,
|
||||||
config_entry: MockConfigEntry,
|
config_entry: MockConfigEntry,
|
||||||
use_serial: str,
|
use_serial: str,
|
||||||
|
mode: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test select platform entities storage mode changes."""
|
"""Test select platform entities storage mode changes."""
|
||||||
with patch("homeassistant.components.enphase_envoy.PLATFORMS", [Platform.SELECT]):
|
with patch("homeassistant.components.enphase_envoy.PLATFORMS", [Platform.SELECT]):
|
||||||
@ -194,11 +180,11 @@ async def test_select_storage_modes(
|
|||||||
test_entity = f"{Platform.SELECT}.{use_serial}_storage_mode"
|
test_entity = f"{Platform.SELECT}.{use_serial}_storage_mode"
|
||||||
|
|
||||||
assert (entity_state := hass.states.get(test_entity))
|
assert (entity_state := hass.states.get(test_entity))
|
||||||
assert STORAGE_MODE_MAP[mock_envoy.data.tariff.storage_settings.mode] == (
|
assert (
|
||||||
current_state := entity_state.state
|
entity_state.state
|
||||||
|
== STORAGE_MODE_MAP[mock_envoy.data.tariff.storage_settings.mode]
|
||||||
)
|
)
|
||||||
|
|
||||||
for mode in [mode for mode in STORAGE_MODE_OPTIONS if not current_state]:
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
SELECT_DOMAIN,
|
SELECT_DOMAIN,
|
||||||
SERVICE_SELECT_OPTION,
|
SERVICE_SELECT_OPTION,
|
||||||
@ -208,24 +194,7 @@ async def test_select_storage_modes(
|
|||||||
},
|
},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
mock_envoy.set_storage_mode.assert_called_once_with(
|
mock_envoy.set_storage_mode.assert_called_once_with(REVERSE_STORAGE_MODE_MAP[mode])
|
||||||
REVERSE_STORAGE_MODE_MAP[mode]
|
|
||||||
)
|
|
||||||
mock_envoy.set_storage_mode.reset_mock()
|
|
||||||
|
|
||||||
# and finally with original mode
|
|
||||||
await hass.services.async_call(
|
|
||||||
SELECT_DOMAIN,
|
|
||||||
SERVICE_SELECT_OPTION,
|
|
||||||
{
|
|
||||||
ATTR_ENTITY_ID: test_entity,
|
|
||||||
ATTR_OPTION: current_state,
|
|
||||||
},
|
|
||||||
blocking=True,
|
|
||||||
)
|
|
||||||
mock_envoy.set_storage_mode.assert_called_once_with(
|
|
||||||
REVERSE_STORAGE_MODE_MAP[current_state]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user