From e8ff31b792f15cee90b66eadae721550ca02f6f7 Mon Sep 17 00:00:00 2001 From: Arie Catsman <120491684+catsmanac@users.noreply.github.com> Date: Thu, 20 Feb 2025 22:23:59 +0100 Subject: [PATCH] Add error handling to enphase_envoy number platform action (#136812) --- .../components/enphase_envoy/number.py | 4 +- tests/components/enphase_envoy/test_number.py | 83 ++++++++++++++++++- 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/enphase_envoy/number.py b/homeassistant/components/enphase_envoy/number.py index a88c282281b..91e93d9c59b 100644 --- a/homeassistant/components/enphase_envoy/number.py +++ b/homeassistant/components/enphase_envoy/number.py @@ -23,7 +23,7 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from .const import DOMAIN from .coordinator import EnphaseConfigEntry, EnphaseUpdateCoordinator -from .entity import EnvoyBaseEntity +from .entity import EnvoyBaseEntity, exception_handler PARALLEL_UPDATES = 1 @@ -132,6 +132,7 @@ class EnvoyRelayNumberEntity(EnvoyBaseEntity, NumberEntity): self.data.dry_contact_settings[self._relay_id] ) + @exception_handler async def async_set_native_value(self, value: float) -> None: """Update the relay.""" await self.envoy.update_dry_contact( @@ -185,6 +186,7 @@ class EnvoyStorageSettingsNumberEntity(EnvoyBaseEntity, NumberEntity): assert self.data.tariff.storage_settings is not None return self.entity_description.value_fn(self.data.tariff.storage_settings) + @exception_handler async def async_set_native_value(self, value: float) -> None: """Update the storage setting.""" await self.entity_description.update_fn(self.envoy, value) diff --git a/tests/components/enphase_envoy/test_number.py b/tests/components/enphase_envoy/test_number.py index 7f9293eef7c..07826174c7d 100644 --- a/tests/components/enphase_envoy/test_number.py +++ b/tests/components/enphase_envoy/test_number.py @@ -2,6 +2,7 @@ from unittest.mock import AsyncMock, patch +from pyenphase.exceptions import EnvoyError import pytest from syrupy.assertion import SnapshotAssertion @@ -13,6 +14,7 @@ from homeassistant.components.number import ( ) from homeassistant.const import ATTR_ENTITY_ID from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import entity_registry as er from . import setup_integration @@ -99,6 +101,43 @@ async def test_number_operation_storage( mock_envoy.set_reserve_soc.assert_awaited_once_with(test_value) +@pytest.mark.parametrize( + ("mock_envoy", "use_serial", "target", "test_value"), + [ + ("envoy_metered_batt_relay", "enpower_654321", "reserve_battery_level", 30.0), + ], + indirect=["mock_envoy"], +) +async def test_number_operation_storage_with_error( + hass: HomeAssistant, + mock_envoy: AsyncMock, + config_entry: MockConfigEntry, + use_serial: bool, + target: str, + test_value: float, +) -> None: + """Test enphase_envoy number storage entities operation.""" + with patch("homeassistant.components.enphase_envoy.PLATFORMS", [Platform.NUMBER]): + await setup_integration(hass, config_entry) + + test_entity = f"number.{use_serial}_{target}" + + mock_envoy.set_reserve_soc.side_effect = EnvoyError("Test") + with pytest.raises( + HomeAssistantError, + match=f"Failed to execute async_set_native_value for {test_entity}, host", + ): + await hass.services.async_call( + NUMBER_DOMAIN, + SERVICE_SET_VALUE, + { + ATTR_ENTITY_ID: test_entity, + ATTR_VALUE: test_value, + }, + blocking=True, + ) + + @pytest.mark.parametrize("mock_envoy", ["envoy_metered_batt_relay"], indirect=True) @pytest.mark.parametrize( ("relay", "target", "expected_value", "test_value", "test_field"), @@ -125,12 +164,10 @@ async def test_number_operation_relays( with patch("homeassistant.components.enphase_envoy.PLATFORMS", [Platform.NUMBER]): await setup_integration(hass, config_entry) - entity_base = f"{Platform.NUMBER}." - assert (dry_contact := mock_envoy.data.dry_contact_settings[relay]) assert (name := dry_contact.load_name.lower().replace(" ", "_")) - test_entity = f"{entity_base}{name}_{target}" + test_entity = f"number.{name}_{target}" assert (entity_state := hass.states.get(test_entity)) assert float(entity_state.state) == expected_value @@ -148,3 +185,43 @@ async def test_number_operation_relays( mock_envoy.update_dry_contact.assert_awaited_once_with( {"id": relay, test_field: int(test_value)} ) + + +@pytest.mark.parametrize( + ("mock_envoy", "relay", "target", "test_value"), + [ + ("envoy_metered_batt_relay", "NC1", "cutoff_battery_level", 15.0), + ], + indirect=["mock_envoy"], +) +async def test_number_operation_relays_with_error( + hass: HomeAssistant, + mock_envoy: AsyncMock, + config_entry: MockConfigEntry, + relay: str, + target: str, + test_value: float, +) -> None: + """Test enphase_envoy number relay entities operation with error returned.""" + with patch("homeassistant.components.enphase_envoy.PLATFORMS", [Platform.NUMBER]): + await setup_integration(hass, config_entry) + + assert (dry_contact := mock_envoy.data.dry_contact_settings[relay]) + assert (name := dry_contact.load_name.lower().replace(" ", "_")) + + test_entity = f"number.{name}_{target}" + + mock_envoy.update_dry_contact.side_effect = EnvoyError("Test") + with pytest.raises( + HomeAssistantError, + match=f"Failed to execute async_set_native_value for {test_entity}, host", + ): + await hass.services.async_call( + NUMBER_DOMAIN, + SERVICE_SET_VALUE, + { + ATTR_ENTITY_ID: test_entity, + ATTR_VALUE: test_value, + }, + blocking=True, + )