Skip to content

Commit

Permalink
Limit log spam from rest and include reason in platform retry
Browse files Browse the repository at this point in the history
- Each retry was logging the error again
- Now we set the cause of the PlatformNotReady to allow Home Assistant to log as needed
  • Loading branch information
bdraco committed Apr 4, 2021
1 parent 95e1daa commit de709c0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
4 changes: 3 additions & 1 deletion homeassistant/components/rest/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions homeassistant/components/rest/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ def __init__(
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(
Expand All @@ -64,6 +65,10 @@ async def async_update(self):
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
4 changes: 3 additions & 1 deletion homeassistant/components/rest/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions tests/components/rest/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions tests/components/rest/test_sensor.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit de709c0

Please sign in to comment.