mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
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 needed
This commit is contained in:
parent
9ba66fe232
commit
30382c3dbe
@ -40,9 +40,11 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
conf = config
|
conf = config
|
||||||
coordinator = None
|
coordinator = None
|
||||||
rest = create_rest_data_from_config(hass, conf)
|
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.data is None:
|
||||||
|
if rest.last_exception:
|
||||||
|
raise PlatformNotReady from rest.last_exception
|
||||||
raise PlatformNotReady
|
raise PlatformNotReady
|
||||||
|
|
||||||
name = conf.get(CONF_NAME)
|
name = conf.get(CONF_NAME)
|
||||||
|
@ -37,13 +37,14 @@ class RestData:
|
|||||||
self._verify_ssl = verify_ssl
|
self._verify_ssl = verify_ssl
|
||||||
self._async_client = None
|
self._async_client = None
|
||||||
self.data = None
|
self.data = None
|
||||||
|
self.last_exception = None
|
||||||
self.headers = None
|
self.headers = None
|
||||||
|
|
||||||
def set_url(self, url):
|
def set_url(self, url):
|
||||||
"""Set url."""
|
"""Set url."""
|
||||||
self._resource = 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."""
|
"""Get the latest data from REST service with provided method."""
|
||||||
if not self._async_client:
|
if not self._async_client:
|
||||||
self._async_client = get_async_client(
|
self._async_client = get_async_client(
|
||||||
@ -64,6 +65,10 @@ class RestData:
|
|||||||
self.data = response.text
|
self.data = response.text
|
||||||
self.headers = response.headers
|
self.headers = response.headers
|
||||||
except httpx.RequestError as ex:
|
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.data = None
|
||||||
self.headers = None
|
self.headers = None
|
||||||
|
@ -50,9 +50,11 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
conf = config
|
conf = config
|
||||||
coordinator = None
|
coordinator = None
|
||||||
rest = create_rest_data_from_config(hass, conf)
|
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.data is None:
|
||||||
|
if rest.last_exception:
|
||||||
|
raise PlatformNotReady from rest.last_exception
|
||||||
raise PlatformNotReady
|
raise PlatformNotReady
|
||||||
|
|
||||||
name = conf.get(CONF_NAME)
|
name = conf.get(CONF_NAME)
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from os import path
|
from os import path
|
||||||
from unittest.mock import patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
import respx
|
import respx
|
||||||
@ -47,9 +47,12 @@ async def test_setup_missing_config(hass):
|
|||||||
|
|
||||||
|
|
||||||
@respx.mock
|
@respx.mock
|
||||||
async def test_setup_failed_connect(hass):
|
async def test_setup_failed_connect(hass, caplog):
|
||||||
"""Test setup when connection error occurs."""
|
"""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(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
binary_sensor.DOMAIN,
|
binary_sensor.DOMAIN,
|
||||||
@ -63,6 +66,7 @@ async def test_setup_failed_connect(hass):
|
|||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert len(hass.states.async_all()) == 0
|
assert len(hass.states.async_all()) == 0
|
||||||
|
assert "server offline" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
@respx.mock
|
@respx.mock
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
"""The tests for the REST sensor platform."""
|
"""The tests for the REST sensor platform."""
|
||||||
import asyncio
|
import asyncio
|
||||||
from os import path
|
from os import path
|
||||||
from unittest.mock import patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
import respx
|
import respx
|
||||||
@ -41,9 +41,11 @@ async def test_setup_missing_schema(hass):
|
|||||||
|
|
||||||
|
|
||||||
@respx.mock
|
@respx.mock
|
||||||
async def test_setup_failed_connect(hass):
|
async def test_setup_failed_connect(hass, caplog):
|
||||||
"""Test setup when connection error occurs."""
|
"""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(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
sensor.DOMAIN,
|
sensor.DOMAIN,
|
||||||
@ -57,6 +59,7 @@ async def test_setup_failed_connect(hass):
|
|||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert len(hass.states.async_all()) == 0
|
assert len(hass.states.async_all()) == 0
|
||||||
|
assert "server offline" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
@respx.mock
|
@respx.mock
|
||||||
|
Loading…
x
Reference in New Issue
Block a user