mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Prosegur code quality improvements (#53647)
This commit is contained in:
parent
cdce14d63d
commit
a2d66bd1c0
@ -3,10 +3,10 @@ import logging
|
||||
|
||||
from pyprosegur.auth import Auth
|
||||
|
||||
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntry
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||
from homeassistant.helpers import aiohttp_client
|
||||
|
||||
from .const import CONF_COUNTRY, DOMAIN
|
||||
@ -32,12 +32,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
except ConnectionRefusedError as error:
|
||||
_LOGGER.error("Configured credential are invalid, %s", error)
|
||||
|
||||
hass.async_create_task(
|
||||
hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_REAUTH, "entry_id": entry.data["entry_id"]},
|
||||
)
|
||||
)
|
||||
raise ConfigEntryAuthFailed from error
|
||||
|
||||
except ConnectionError as error:
|
||||
_LOGGER.error("Could not connect with Prosegur backend: %s", error)
|
||||
|
@ -25,11 +25,9 @@ STEP_USER_DATA_SCHEMA = vol.Schema(
|
||||
|
||||
async def validate_input(hass: core.HomeAssistant, data):
|
||||
"""Validate the user input allows us to connect."""
|
||||
session = aiohttp_client.async_get_clientsession(hass)
|
||||
auth = Auth(session, data[CONF_USERNAME], data[CONF_PASSWORD], data[CONF_COUNTRY])
|
||||
try:
|
||||
session = aiohttp_client.async_get_clientsession(hass)
|
||||
auth = Auth(
|
||||
session, data[CONF_USERNAME], data[CONF_PASSWORD], data[CONF_COUNTRY]
|
||||
)
|
||||
install = await Installation.retrieve(auth)
|
||||
except ConnectionRefusedError:
|
||||
raise InvalidAuth from ConnectionRefusedError
|
||||
@ -95,15 +93,14 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
errors["base"] = "cannot_connect"
|
||||
except InvalidAuth:
|
||||
errors["base"] = "invalid_auth"
|
||||
except Exception as exception: # pylint: disable=broad-except
|
||||
_LOGGER.exception(exception)
|
||||
except Exception: # pylint: disable=broad-except
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
errors["base"] = "unknown"
|
||||
else:
|
||||
data = self.entry.data.copy()
|
||||
self.hass.config_entries.async_update_entry(
|
||||
self.entry,
|
||||
data={
|
||||
**data,
|
||||
**self.entry.data,
|
||||
CONF_USERNAME: user_input[CONF_USERNAME],
|
||||
CONF_PASSWORD: user_input[CONF_PASSWORD],
|
||||
},
|
||||
|
@ -8,7 +8,7 @@ from tests.common import MockConfigEntry
|
||||
CONTRACT = "1234abcd"
|
||||
|
||||
|
||||
async def setup_platform(hass, platform):
|
||||
async def setup_platform(hass):
|
||||
"""Set up the Prosegur platform."""
|
||||
mock_entry = MockConfigEntry(
|
||||
domain=PROSEGUR_DOMAIN,
|
||||
|
@ -48,7 +48,7 @@ def mock_status(request):
|
||||
|
||||
async def test_entity_registry(hass, mock_auth, mock_status):
|
||||
"""Tests that the devices are registered in the entity registry."""
|
||||
await setup_platform(hass, ALARM_DOMAIN)
|
||||
await setup_platform(hass)
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
entry = entity_registry.async_get(PROSEGUR_ALARM_ENTITY)
|
||||
@ -74,7 +74,7 @@ async def test_connection_error(hass, mock_auth):
|
||||
|
||||
with patch("pyprosegur.installation.Installation.retrieve", return_value=install):
|
||||
|
||||
await setup_platform(hass, ALARM_DOMAIN)
|
||||
await setup_platform(hass)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@ -106,7 +106,7 @@ async def test_arm(hass, mock_auth, code, alarm_service, alarm_state):
|
||||
install.status = code
|
||||
|
||||
with patch("pyprosegur.installation.Installation.retrieve", return_value=install):
|
||||
await setup_platform(hass, ALARM_DOMAIN)
|
||||
await setup_platform(hass)
|
||||
|
||||
await hass.services.async_call(
|
||||
ALARM_DOMAIN,
|
||||
|
@ -26,7 +26,7 @@ async def test_form(hass):
|
||||
with patch(
|
||||
"homeassistant.components.prosegur.config_flow.Installation.retrieve",
|
||||
return_value=install,
|
||||
), patch(
|
||||
) as mock_retrieve, patch(
|
||||
"homeassistant.components.prosegur.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
@ -50,6 +50,8 @@ async def test_form(hass):
|
||||
}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
assert len(mock_retrieve.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_invalid_auth(hass):
|
||||
"""Test we handle invalid auth."""
|
||||
@ -120,28 +122,6 @@ async def test_form_unknown_exception(hass):
|
||||
assert result2["errors"] == {"base": "unknown"}
|
||||
|
||||
|
||||
async def test_form_validate_input(hass):
|
||||
"""Test we retrieve data from Installation."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"pyprosegur.installation.Installation.retrieve",
|
||||
return_value=MagicMock,
|
||||
) as mock_retrieve:
|
||||
await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
"username": "test-username",
|
||||
"password": "test-password",
|
||||
"country": "PT",
|
||||
},
|
||||
)
|
||||
|
||||
assert len(mock_retrieve.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_reauth_flow(hass):
|
||||
"""Test a reauthentication flow."""
|
||||
entry = MockConfigEntry(
|
||||
|
@ -18,7 +18,6 @@ from tests.common import MockConfigEntry
|
||||
async def test_setup_entry_fail_retrieve(hass, error):
|
||||
"""Test loading the Prosegur entry."""
|
||||
|
||||
hass.config.components.add(DOMAIN)
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
@ -47,7 +46,6 @@ async def test_unload_entry(hass, aioclient_mock):
|
||||
json={"data": {"token": "123456789"}},
|
||||
)
|
||||
|
||||
hass.config.components.add(DOMAIN)
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
|
Loading…
x
Reference in New Issue
Block a user