@@ -1742,6 +1742,67 @@ async def test_conductivity_hysteresis(
17421742 await update_plant_sensors (hass , init_integration .entry_id )
17431743 assert plant .conductivity_status == STATE_OK
17441744
1745+ async def test_threshold_unavailable_preserves_status (
1746+ self ,
1747+ hass : HomeAssistant ,
1748+ init_integration : MockConfigEntry ,
1749+ ) -> None :
1750+ """Test that unavailable threshold entities don't crash _check_threshold.
1751+
1752+ When a threshold number entity has state 'unavailable', the plant
1753+ should keep its current status rather than raising ValueError.
1754+ """
1755+ plant = hass .data [DOMAIN ][init_integration .entry_id ][ATTR_PLANT ]
1756+
1757+ # First establish a known state
1758+ await set_external_sensor_states (
1759+ hass ,
1760+ temperature = 25.0 ,
1761+ moisture = 40.0 ,
1762+ conductivity = 1000.0 ,
1763+ illuminance = 5000.0 ,
1764+ humidity = 40.0 ,
1765+ )
1766+ await update_plant_sensors (hass , init_integration .entry_id )
1767+ assert plant .state == STATE_OK
1768+ assert plant .moisture_status == STATE_OK
1769+
1770+ # Make the min_moisture threshold entity unavailable
1771+ hass .states .async_set (plant .min_moisture .entity_id , STATE_UNAVAILABLE )
1772+ await hass .async_block_till_done ()
1773+
1774+ # Update should not crash — moisture_status should be preserved
1775+ await update_plant_sensors (hass , init_integration .entry_id )
1776+ assert plant .moisture_status == STATE_OK
1777+
1778+ async def test_threshold_unknown_preserves_status (
1779+ self ,
1780+ hass : HomeAssistant ,
1781+ init_integration : MockConfigEntry ,
1782+ ) -> None :
1783+ """Test that unknown threshold entities don't crash _check_threshold."""
1784+ plant = hass .data [DOMAIN ][init_integration .entry_id ][ATTR_PLANT ]
1785+
1786+ # Establish a LOW moisture state
1787+ await set_external_sensor_states (
1788+ hass ,
1789+ temperature = 25.0 ,
1790+ moisture = 5.0 , # Below min of 20
1791+ conductivity = 1000.0 ,
1792+ illuminance = 5000.0 ,
1793+ humidity = 40.0 ,
1794+ )
1795+ await update_plant_sensors (hass , init_integration .entry_id )
1796+ assert plant .moisture_status == STATE_LOW
1797+
1798+ # Make the max_moisture threshold entity unknown
1799+ hass .states .async_set (plant .max_moisture .entity_id , STATE_UNKNOWN )
1800+ await hass .async_block_till_done ()
1801+
1802+ # Update should not crash — moisture_status should stay LOW
1803+ await update_plant_sensors (hass , init_integration .entry_id )
1804+ assert plant .moisture_status == STATE_LOW
1805+
17451806
17461807class TestYamlImport :
17471808 """Tests for YAML configuration import (async_setup)."""
0 commit comments