Parameterize enphase_envoy number tests. (#136631)

This commit is contained in:
Arie Catsman 2025-01-28 16:56:14 +01:00 committed by GitHub
parent 52dc124cfe
commit ae157e8592
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -21,9 +21,9 @@ from tests.common import MockConfigEntry, snapshot_platform
@pytest.mark.parametrize(
("mock_envoy"),
"mock_envoy",
["envoy_metered_batt_relay", "envoy_eu_batt"],
indirect=["mock_envoy"],
indirect=True,
)
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_number(
@ -40,14 +40,14 @@ async def test_number(
@pytest.mark.parametrize(
("mock_envoy"),
"mock_envoy",
[
"envoy",
"envoy_1p_metered",
"envoy_nobatt_metered_3p",
"envoy_tot_cons_metered",
],
indirect=["mock_envoy"],
indirect=True,
)
async def test_no_number(
hass: HomeAssistant,
@ -62,10 +62,10 @@ async def test_no_number(
@pytest.mark.parametrize(
("mock_envoy", "use_serial"),
("mock_envoy", "use_serial", "expected_value", "test_value"),
[
("envoy_metered_batt_relay", "enpower_654321"),
("envoy_eu_batt", "envoy_1234"),
("envoy_metered_batt_relay", "enpower_654321", 15.0, 30.0),
("envoy_eu_batt", "envoy_1234", 0.0, 80.0),
],
indirect=["mock_envoy"],
)
@ -74,6 +74,8 @@ async def test_number_operation_storage(
mock_envoy: AsyncMock,
config_entry: MockConfigEntry,
use_serial: bool,
expected_value: float,
test_value: float,
) -> None:
"""Test enphase_envoy number storage entities operation."""
with patch("homeassistant.components.enphase_envoy.PLATFORMS", [Platform.NUMBER]):
@ -82,10 +84,8 @@ async def test_number_operation_storage(
test_entity = f"{Platform.NUMBER}.{use_serial}_reserve_battery_level"
assert (entity_state := hass.states.get(test_entity))
assert mock_envoy.data.tariff.storage_settings.reserved_soc == float(
entity_state.state
)
test_value = 30.0
assert float(entity_state.state) == expected_value
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
@ -99,13 +99,27 @@ async def test_number_operation_storage(
mock_envoy.set_reserve_soc.assert_awaited_once_with(test_value)
@pytest.mark.parametrize("mock_envoy", ["envoy_metered_batt_relay"], indirect=True)
@pytest.mark.parametrize(
("mock_envoy"), ["envoy_metered_batt_relay"], indirect=["mock_envoy"]
("relay", "target", "expected_value", "test_value", "test_field"),
[
("NC1", "cutoff_battery_level", 25.0, 15.0, "soc_low"),
("NC1", "restore_battery_level", 70.0, 75.0, "soc_high"),
("NC2", "cutoff_battery_level", 30.0, 25.0, "soc_low"),
("NC2", "restore_battery_level", 70.0, 80.0, "soc_high"),
("NC3", "cutoff_battery_level", 30.0, 45.0, "soc_low"),
("NC3", "restore_battery_level", 70.0, 90.0, "soc_high"),
],
)
async def test_number_operation_relays(
hass: HomeAssistant,
mock_envoy: AsyncMock,
config_entry: MockConfigEntry,
relay: str,
target: str,
expected_value: float,
test_value: float,
test_field: str,
) -> None:
"""Test enphase_envoy number relay entities operation."""
with patch("homeassistant.components.enphase_envoy.PLATFORMS", [Platform.NUMBER]):
@ -113,48 +127,24 @@ async def test_number_operation_relays(
entity_base = f"{Platform.NUMBER}."
for counter, (contact_id, dry_contact) in enumerate(
mock_envoy.data.dry_contact_settings.items()
):
name = dry_contact.load_name.lower().replace(" ", "_")
test_entity = f"{entity_base}{name}_cutoff_battery_level"
assert (entity_state := hass.states.get(test_entity))
assert mock_envoy.data.dry_contact_settings[contact_id].soc_low == float(
entity_state.state
)
test_value = 10.0 + counter
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: test_entity,
ATTR_VALUE: test_value,
},
blocking=True,
)
assert (dry_contact := mock_envoy.data.dry_contact_settings[relay])
assert (name := dry_contact.load_name.lower().replace(" ", "_"))
mock_envoy.update_dry_contact.assert_awaited_once_with(
{"id": contact_id, "soc_low": test_value}
)
mock_envoy.update_dry_contact.reset_mock()
test_entity = f"{entity_base}{name}_{target}"
test_entity = f"{entity_base}{name}_restore_battery_level"
assert (entity_state := hass.states.get(test_entity))
assert mock_envoy.data.dry_contact_settings[contact_id].soc_high == float(
entity_state.state
)
test_value = 80.0 - counter
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: test_entity,
ATTR_VALUE: test_value,
},
blocking=True,
)
assert (entity_state := hass.states.get(test_entity))
assert float(entity_state.state) == expected_value
mock_envoy.update_dry_contact.assert_awaited_once_with(
{"id": contact_id, "soc_high": test_value}
)
mock_envoy.update_dry_contact.reset_mock()
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: test_entity,
ATTR_VALUE: test_value,
},
blocking=True,
)
mock_envoy.update_dry_contact.assert_awaited_once_with(
{"id": relay, test_field: int(test_value)}
)