Limit log spam from rest and include reason in platform retry (#48666)
- Each retry was logging the error again - Now we set the cause of the PlatformNotReady to allow Home Assistant to log as neededpull/44930/head
parent
9ba66fe232
commit
30382c3dbe
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue