mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Fix pyload async_update SensorEntity raising exceptions (#119655)
* Fix Sensorentity raising exceptions * Increase test coverage
This commit is contained in:
parent
05cbda0e50
commit
c077c2a972
@ -33,7 +33,6 @@ from homeassistant.helpers.aiohttp_client import async_create_clientsession
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
from homeassistant.helpers.update_coordinator import UpdateFailed
|
||||
|
||||
from .const import DEFAULT_HOST, DEFAULT_NAME, DEFAULT_PORT
|
||||
|
||||
@ -132,20 +131,24 @@ class PyLoadSensor(SensorEntity):
|
||||
_LOGGER.info("Authentication failed, trying to reauthenticate")
|
||||
try:
|
||||
await self.api.login()
|
||||
except InvalidAuth as e:
|
||||
raise PlatformNotReady(
|
||||
f"Authentication failed for {self.api.username}, check your login credentials"
|
||||
) from e
|
||||
else:
|
||||
raise UpdateFailed(
|
||||
"Unable to retrieve data due to cookie expiration but re-authentication was successful."
|
||||
except InvalidAuth:
|
||||
_LOGGER.error(
|
||||
"Authentication failed for %s, check your login credentials",
|
||||
self.api.username,
|
||||
)
|
||||
except CannotConnect as e:
|
||||
raise UpdateFailed(
|
||||
"Unable to connect and retrieve data from pyLoad API"
|
||||
) from e
|
||||
except ParserError as e:
|
||||
raise UpdateFailed("Unable to parse data from pyLoad API") from e
|
||||
return
|
||||
else:
|
||||
_LOGGER.info(
|
||||
"Unable to retrieve data due to cookie expiration "
|
||||
"but re-authentication was successful"
|
||||
)
|
||||
return
|
||||
except CannotConnect:
|
||||
_LOGGER.debug("Unable to connect and retrieve data from pyLoad API")
|
||||
return
|
||||
except ParserError:
|
||||
_LOGGER.error("Unable to parse data from pyLoad API")
|
||||
return
|
||||
|
||||
value = getattr(self.data, self.type)
|
||||
|
||||
|
@ -2,15 +2,19 @@
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
from pyloadapi.exceptions import CannotConnect, InvalidAuth, ParserError
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.pyload.sensor import SCAN_INTERVAL
|
||||
from homeassistant.components.sensor import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_pyloadapi")
|
||||
async def test_setup(
|
||||
@ -60,9 +64,9 @@ async def test_setup_exceptions(
|
||||
@pytest.mark.parametrize(
|
||||
("exception", "expected_exception"),
|
||||
[
|
||||
(CannotConnect, "UpdateFailed"),
|
||||
(ParserError, "UpdateFailed"),
|
||||
(InvalidAuth, "UpdateFailed"),
|
||||
(CannotConnect, "Unable to connect and retrieve data from pyLoad API"),
|
||||
(ParserError, "Unable to parse data from pyLoad API"),
|
||||
(InvalidAuth, "Authentication failed, trying to reauthenticate"),
|
||||
],
|
||||
)
|
||||
async def test_sensor_update_exceptions(
|
||||
@ -80,5 +84,31 @@ async def test_sensor_update_exceptions(
|
||||
assert await async_setup_component(hass, DOMAIN, pyload_config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_all(DOMAIN)) == 0
|
||||
assert len(hass.states.async_all(DOMAIN)) == 1
|
||||
assert expected_exception in caplog.text
|
||||
|
||||
|
||||
async def test_sensor_invalid_auth(
|
||||
hass: HomeAssistant,
|
||||
pyload_config: ConfigType,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test invalid auth during sensor update."""
|
||||
|
||||
assert await async_setup_component(hass, DOMAIN, pyload_config)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_all(DOMAIN)) == 1
|
||||
|
||||
mock_pyloadapi.get_status.side_effect = InvalidAuth
|
||||
mock_pyloadapi.login.side_effect = InvalidAuth
|
||||
|
||||
freezer.tick(SCAN_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert (
|
||||
"Authentication failed for username, check your login credentials"
|
||||
in caplog.text
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user