From 2d74dafd3fb7be37423d252e531429188db7d7fa Mon Sep 17 00:00:00 2001 From: Denis Shulyaka Date: Fri, 16 Feb 2024 17:29:14 +0300 Subject: [PATCH] Generic Hygrostat: Do not log warning if the hygrostat is already not active (#102662) * Generic Hygrostat: Do not log warning if the hygrostat is already not active * add test --- .../generic_hygrostat/humidifier.py | 7 +++-- .../generic_hygrostat/test_humidifier.py | 29 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/generic_hygrostat/humidifier.py b/homeassistant/components/generic_hygrostat/humidifier.py index 095b46245cf..d69a8a968c7 100644 --- a/homeassistant/components/generic_hygrostat/humidifier.py +++ b/homeassistant/components/generic_hygrostat/humidifier.py @@ -396,9 +396,12 @@ class GenericHygrostat(HumidifierEntity, RestoreEntity): try: self._cur_humidity = float(humidity) except ValueError as ex: - _LOGGER.warning("Unable to update from sensor: %s", ex) + if self._active: + _LOGGER.warning("Unable to update from sensor: %s", ex) + self._active = False + else: + _LOGGER.debug("Unable to update from sensor: %s", ex) self._cur_humidity = None - self._active = False if self._is_device_active: await self._async_device_turn_off() diff --git a/tests/components/generic_hygrostat/test_humidifier.py b/tests/components/generic_hygrostat/test_humidifier.py index d68e5ca78e0..e1a33a19834 100644 --- a/tests/components/generic_hygrostat/test_humidifier.py +++ b/tests/components/generic_hygrostat/test_humidifier.py @@ -468,6 +468,35 @@ async def test_sensor_bad_value(hass: HomeAssistant, setup_comp_2) -> None: assert hass.states.get(ENTITY).state == STATE_UNAVAILABLE +async def test_sensor_bad_value_twice( + hass: HomeAssistant, setup_comp_2, caplog +) -> None: + """Test sensor that the second bad value is not logged as warning.""" + assert hass.states.get(ENTITY).state == STATE_ON + + _setup_sensor(hass, "forty") + await hass.async_block_till_done() + + assert hass.states.get(ENTITY).state == STATE_UNAVAILABLE + assert [ + rec.levelname + for rec in caplog.records + if "Unable to update from sensor" in rec.message + ] == ["WARNING"] + + caplog.clear() + + _setup_sensor(hass, "fifty") + await hass.async_block_till_done() + + assert hass.states.get(ENTITY).state == STATE_UNAVAILABLE + assert [ + rec.levelname + for rec in caplog.records + if "Unable to update from sensor" in rec.message + ] == ["DEBUG"] + + async def test_set_target_humidity_humidifier_on( hass: HomeAssistant, setup_comp_2 ) -> None: