Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise ConfigEntryNotReady when unable to connect during setup of AVM Fritz!Smarthome #97985

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion homeassistant/components/fritzbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from pyfritzhome import Fritzhome, FritzhomeDevice, LoginError
from pyfritzhome.devicetypes.fritzhomeentitybase import FritzhomeEntityBase
from requests.exceptions import ConnectionError as RequestConnectionError

from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.config_entries import ConfigEntry
Expand All @@ -16,7 +17,7 @@
UnitOfTemperature,
)
from homeassistant.core import Event, HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import EntityDescription
from homeassistant.helpers.entity_registry import RegistryEntry, async_migrate_entries
Expand All @@ -36,6 +37,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

try:
await hass.async_add_executor_job(fritz.login)
except RequestConnectionError as err:
raise ConfigEntryNotReady from err
except LoginError as err:
raise ConfigEntryAuthFailed from err

Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/fritzbox/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from pyfritzhome import Fritzhome, FritzhomeDevice, LoginError
from pyfritzhome.devicetypes import FritzhomeTemplate
import requests
from requests.exceptions import ConnectionError as RequestConnectionError, HTTPError

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -51,9 +51,9 @@ def _update_fritz_devices(self) -> FritzboxCoordinatorData:
self.fritz.update_devices()
if self.has_templates:
self.fritz.update_templates()
except requests.exceptions.ConnectionError as ex:
except RequestConnectionError as ex:
raise UpdateFailed from ex
except requests.exceptions.HTTPError:
except HTTPError:
# If the device rebooted, login again
try:
self.fritz.login()
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/fritzbox/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"integration_type": "hub",
"iot_class": "local_polling",
"loggers": ["pyfritzhome"],
"requirements": ["pyfritzhome==0.6.8"],
"requirements": ["pyfritzhome==0.6.9"],
"ssdp": [
{
"st": "urn:schemas-upnp-org:device:fritzbox:1"
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1704,7 +1704,7 @@ pyforked-daapd==0.1.14
pyfreedompro==1.1.0

# homeassistant.components.fritzbox
pyfritzhome==0.6.8
pyfritzhome==0.6.9

# homeassistant.components.ifttt
pyfttt==0.3
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,7 @@ pyforked-daapd==0.1.14
pyfreedompro==1.1.0

# homeassistant.components.fritzbox
pyfritzhome==0.6.8
pyfritzhome==0.6.9

# homeassistant.components.ifttt
pyfttt==0.3
Expand Down
23 changes: 22 additions & 1 deletion tests/components/fritzbox/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ async def test_coordinator_update_when_unreachable(
unique_id="any",
)
entry.add_to_hass(hass)
fritz().get_devices.side_effect = [ConnectionError(), ""]
fritz().update_devices.side_effect = [ConnectionError(), ""]

assert not await hass.config_entries.async_setup(entry.entry_id)
assert entry.state is ConfigEntryState.SETUP_RETRY
Expand Down Expand Up @@ -258,6 +258,27 @@ async def test_raise_config_entry_not_ready_when_offline(hass: HomeAssistant) ->
unique_id="any",
)
entry.add_to_hass(hass)
with patch(
"homeassistant.components.fritzbox.Fritzhome.login",
side_effect=ConnectionError(),
) as mock_login:
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
mock_login.assert_called_once()

entries = hass.config_entries.async_entries()
config_entry = entries[0]
assert config_entry.state is ConfigEntryState.SETUP_RETRY


async def test_raise_config_entry_error_when_login_fail(hass: HomeAssistant) -> None:
"""Config entry state is SETUP_ERROR when login to fritzbox fail."""
entry = MockConfigEntry(
domain=FB_DOMAIN,
data={CONF_HOST: "any", **MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0]},
unique_id="any",
)
entry.add_to_hass(hass)
with patch(
"homeassistant.components.fritzbox.Fritzhome.login",
side_effect=LoginError("user"),
Expand Down