From e32694c146bec200c6583f48959d058899581db5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Eric=20Sv=C3=A4rd?= <eric.svard@gmail.com>
Date: Wed, 29 Jun 2022 12:53:55 +0200
Subject: [PATCH] Make SolarEdge energy value validation a bit less aggressive
 (#69998)

* Make energy value validation a bit less aggressive

Attempt to solve issue 69600 introduced by previous fix for issue
59285.

- Introduce a tolerance factor for energy value validation.
- Only skip update the specific invalid energy entity. An energy entity
  with invalid values will now show "State unknown".

* Remove the tolerance factor. Let's just ignore the specific invalid energy entity.
---
 .../components/solaredge/coordinator.py          |  6 ++++--
 tests/components/solaredge/test_coordinator.py   | 16 ++++++++++++----
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/homeassistant/components/solaredge/coordinator.py b/homeassistant/components/solaredge/coordinator.py
index 4e93571f8a4..fe8f2f86a8e 100644
--- a/homeassistant/components/solaredge/coordinator.py
+++ b/homeassistant/components/solaredge/coordinator.py
@@ -94,8 +94,10 @@ class SolarEdgeOverviewDataService(SolarEdgeDataService):
             for index, key in enumerate(energy_keys, start=1):
                 # All coming values in list should be larger than the current value.
                 if any(self.data[k] > self.data[key] for k in energy_keys[index:]):
-                    self.data = {}
-                    raise UpdateFailed("Invalid energy values, skipping update")
+                    LOGGER.info(
+                        "Ignoring invalid energy value %s for %s", self.data[key], key
+                    )
+                    self.data.pop(key)
 
         LOGGER.debug("Updated SolarEdge overview: %s", self.data)
 
diff --git a/tests/components/solaredge/test_coordinator.py b/tests/components/solaredge/test_coordinator.py
index b3c9227648e..eb5d033f112 100644
--- a/tests/components/solaredge/test_coordinator.py
+++ b/tests/components/solaredge/test_coordinator.py
@@ -6,8 +6,9 @@ from homeassistant.components.solaredge.const import (
     DEFAULT_NAME,
     DOMAIN,
     OVERVIEW_UPDATE_DELAY,
+    SENSOR_TYPES,
 )
-from homeassistant.const import CONF_API_KEY, CONF_NAME, STATE_UNAVAILABLE
+from homeassistant.const import CONF_API_KEY, CONF_NAME, STATE_UNKNOWN
 from homeassistant.core import HomeAssistant
 import homeassistant.util.dt as dt_util
 
@@ -29,6 +30,9 @@ async def test_solaredgeoverviewdataservice_energy_values_validity(
     )
     mock_solaredge().get_details.return_value = {"details": {"status": "active"}}
     mock_config_entry.add_to_hass(hass)
+    for description in SENSOR_TYPES:
+        description.entity_registry_enabled_default = True
+
     await hass.config_entries.async_setup(mock_config_entry.entry_id)
 
     # Valid energy values update
@@ -56,7 +60,7 @@ async def test_solaredgeoverviewdataservice_energy_values_validity(
 
     state = hass.states.get("sensor.solaredge_lifetime_energy")
     assert state
-    assert state.state == STATE_UNAVAILABLE
+    assert state.state == STATE_UNKNOWN
 
     # New valid energy values update
     mock_overview_data["overview"]["lifeTimeData"]["energy"] = 100001
@@ -74,9 +78,13 @@ async def test_solaredgeoverviewdataservice_energy_values_validity(
     async_fire_time_changed(hass, dt_util.utcnow() + OVERVIEW_UPDATE_DELAY)
     await hass.async_block_till_done()
 
-    state = hass.states.get("sensor.solaredge_lifetime_energy")
+    state = hass.states.get("sensor.solaredge_energy_this_year")
+    assert state
+    assert state.state == STATE_UNKNOWN
+    # Check that the valid lastMonthData is still available
+    state = hass.states.get("sensor.solaredge_energy_this_month")
     assert state
-    assert state.state == STATE_UNAVAILABLE
+    assert state.state == str(mock_overview_data["overview"]["lastMonthData"]["energy"])
 
     # All zero energy values should also be valid.
     mock_overview_data["overview"]["lifeTimeData"]["energy"] = 0.0
-- 
GitLab