diff --git a/homeassistant/components/rest/binary_sensor.py b/homeassistant/components/rest/binary_sensor.py index 9692f5b9339..a90c5bd7c77 100644 --- a/homeassistant/components/rest/binary_sensor.py +++ b/homeassistant/components/rest/binary_sensor.py @@ -40,9 +40,11 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= conf = config coordinator = None rest = create_rest_data_from_config(hass, conf) - await rest.async_update() + await rest.async_update(log_errors=False) if rest.data is None: + if rest.last_exception: + raise PlatformNotReady from rest.last_exception raise PlatformNotReady name = conf.get(CONF_NAME) diff --git a/homeassistant/components/rest/data.py b/homeassistant/components/rest/data.py index dd2e29616c7..8b03bcfb128 100644 --- a/homeassistant/components/rest/data.py +++ b/homeassistant/components/rest/data.py @@ -37,13 +37,14 @@ class RestData: self._verify_ssl = verify_ssl self._async_client = None self.data = None + self.last_exception = None self.headers = None def set_url(self, url): """Set url.""" self._resource = url - async def async_update(self): + async def async_update(self, log_errors=True): """Get the latest data from REST service with provided method.""" if not self._async_client: self._async_client = get_async_client( @@ -64,6 +65,10 @@ class RestData: self.data = response.text self.headers = response.headers except httpx.RequestError as ex: - _LOGGER.error("Error fetching data: %s failed with %s", self._resource, ex) + if log_errors: + _LOGGER.error( + "Error fetching data: %s failed with %s", self._resource, ex + ) + self.last_exception = ex self.data = None self.headers = None diff --git a/homeassistant/components/rest/sensor.py b/homeassistant/components/rest/sensor.py index d303f7a57b3..7727b5f09ab 100644 --- a/homeassistant/components/rest/sensor.py +++ b/homeassistant/components/rest/sensor.py @@ -50,9 +50,11 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= conf = config coordinator = None rest = create_rest_data_from_config(hass, conf) - await rest.async_update() + await rest.async_update(log_errors=False) if rest.data is None: + if rest.last_exception: + raise PlatformNotReady from rest.last_exception raise PlatformNotReady name = conf.get(CONF_NAME) diff --git a/tests/components/rest/test_binary_sensor.py b/tests/components/rest/test_binary_sensor.py index 9adb04ea40c..f6445c25022 100644 --- a/tests/components/rest/test_binary_sensor.py +++ b/tests/components/rest/test_binary_sensor.py @@ -2,7 +2,7 @@ import asyncio from os import path -from unittest.mock import patch +from unittest.mock import MagicMock, patch import httpx import respx @@ -47,9 +47,12 @@ async def test_setup_missing_config(hass): @respx.mock -async def test_setup_failed_connect(hass): +async def test_setup_failed_connect(hass, caplog): """Test setup when connection error occurs.""" - respx.get("http://localhost").mock(side_effect=httpx.RequestError) + + respx.get("http://localhost").mock( + side_effect=httpx.RequestError("server offline", request=MagicMock()) + ) assert await async_setup_component( hass, binary_sensor.DOMAIN, @@ -63,6 +66,7 @@ async def test_setup_failed_connect(hass): ) await hass.async_block_till_done() assert len(hass.states.async_all()) == 0 + assert "server offline" in caplog.text @respx.mock diff --git a/tests/components/rest/test_sensor.py b/tests/components/rest/test_sensor.py index 2e308f69384..50b959be36b 100644 --- a/tests/components/rest/test_sensor.py +++ b/tests/components/rest/test_sensor.py @@ -1,7 +1,7 @@ """The tests for the REST sensor platform.""" import asyncio from os import path -from unittest.mock import patch +from unittest.mock import MagicMock, patch import httpx import respx @@ -41,9 +41,11 @@ async def test_setup_missing_schema(hass): @respx.mock -async def test_setup_failed_connect(hass): +async def test_setup_failed_connect(hass, caplog): """Test setup when connection error occurs.""" - respx.get("http://localhost").mock(side_effect=httpx.RequestError) + respx.get("http://localhost").mock( + side_effect=httpx.RequestError("server offline", request=MagicMock()) + ) assert await async_setup_component( hass, sensor.DOMAIN, @@ -57,6 +59,7 @@ async def test_setup_failed_connect(hass): ) await hass.async_block_till_done() assert len(hass.states.async_all()) == 0 + assert "server offline" in caplog.text @respx.mock