Refactor indevolt integration to remove magic number constants (#169337)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
A. Gideonse
2026-04-28 07:51:04 +02:00
committed by GitHub
co-authored by Copilot
parent 63d4f4d03d
commit 308cb686d2
12 changed files with 310 additions and 278 deletions
+13 -9
View File
@@ -4,6 +4,7 @@ from datetime import timedelta
from unittest.mock import AsyncMock, patch
from freezegun.api import FrozenDateTimeFactory
from indevolt_api import IndevoltConfig, IndevoltEnergyMode
import pytest
from syrupy.assertion import SnapshotAssertion
@@ -18,9 +19,6 @@ from . import setup_integration
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
KEY_READ_ENERGY_MODE = "7101"
KEY_WRITE_ENERGY_MODE = "47005"
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
@pytest.mark.parametrize("generation", [2, 1], indirect=True)
@@ -42,9 +40,9 @@ async def test_select(
@pytest.mark.parametrize(
("option", "expected_value"),
[
("self_consumed_prioritized", 1),
("real_time_control", 4),
("charge_discharge_schedule", 5),
("self_consumed_prioritized", IndevoltEnergyMode.SELF_CONSUMED_PRIORITIZED),
("real_time_control", IndevoltEnergyMode.REAL_TIME_CONTROL),
("charge_discharge_schedule", IndevoltEnergyMode.CHARGE_DISCHARGE_SCHEDULE),
],
)
async def test_select_option(
@@ -62,7 +60,9 @@ async def test_select_option(
mock_indevolt.set_data.reset_mock()
# Update mock data to reflect the new value
mock_indevolt.fetch_data.return_value[KEY_READ_ENERGY_MODE] = expected_value
mock_indevolt.fetch_data.return_value[IndevoltConfig.READ_ENERGY_MODE] = (
expected_value
)
# Attempt to change option
await hass.services.async_call(
@@ -73,7 +73,9 @@ async def test_select_option(
)
# Verify set_data was called with correct parameters
mock_indevolt.set_data.assert_called_with(KEY_WRITE_ENERGY_MODE, expected_value)
mock_indevolt.set_data.assert_called_with(
IndevoltConfig.WRITE_ENERGY_MODE, expected_value
)
# Verify updated state
assert (state := hass.states.get("select.cms_sf2000_energy_mode")) is not None
@@ -120,7 +122,9 @@ async def test_select_unavailable_outdoor_portable(
"""Test that entity is unavailable when device is in outdoor/portable mode (value 0)."""
# Update mock data to fake outdoor/portable mode
mock_indevolt.fetch_data.return_value[KEY_READ_ENERGY_MODE] = 0
mock_indevolt.fetch_data.return_value[IndevoltConfig.READ_ENERGY_MODE] = (
IndevoltEnergyMode.OUTDOOR_PORTABLE
)
# Initialize platform to test availability logic
with patch("homeassistant.components.indevolt.PLATFORMS", [Platform.SELECT]):