From c6b3b9fa9011dbc410326ef8ad2e5fb06d5d812d Mon Sep 17 00:00:00 2001 From: Phil Bruckner Date: Wed, 31 Aug 2022 14:54:23 -0500 Subject: [PATCH] Convert life360 to aiohttp (#77508) Bump life360 package to 5.1.0. --- .../components/life360/config_flow.py | 21 ++++++++++--------- homeassistant/components/life360/const.py | 1 - .../components/life360/coordinator.py | 8 +++---- .../components/life360/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/life360/test_config_flow.py | 4 +++- 7 files changed, 20 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/life360/config_flow.py b/homeassistant/components/life360/config_flow.py index 331882aa991..5153e389d8b 100644 --- a/homeassistant/components/life360/config_flow.py +++ b/homeassistant/components/life360/config_flow.py @@ -12,10 +12,10 @@ from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import callback from homeassistant.data_entry_flow import FlowResult +from homeassistant.helpers.aiohttp_client import async_get_clientsession import homeassistant.helpers.config_validation as cv from .const import ( - COMM_MAX_RETRIES, COMM_TIMEOUT, CONF_AUTHORIZATION, CONF_DRIVING_SPEED, @@ -53,13 +53,10 @@ class Life360ConfigFlow(ConfigFlow, domain=DOMAIN): """Life360 integration config flow.""" VERSION = 1 - - def __init__(self) -> None: - """Initialize.""" - self._api = Life360(timeout=COMM_TIMEOUT, max_retries=COMM_MAX_RETRIES) - self._username: str | vol.UNDEFINED = vol.UNDEFINED - self._password: str | vol.UNDEFINED = vol.UNDEFINED - self._reauth_entry: ConfigEntry | None = None + _api: Life360 | None = None + _username: str | vol.UNDEFINED = vol.UNDEFINED + _password: str | vol.UNDEFINED = vol.UNDEFINED + _reauth_entry: ConfigEntry | None = None @staticmethod @callback @@ -69,10 +66,14 @@ class Life360ConfigFlow(ConfigFlow, domain=DOMAIN): async def _async_verify(self, step_id: str) -> FlowResult: """Attempt to authorize the provided credentials.""" + if not self._api: + self._api = Life360( + session=async_get_clientsession(self.hass), timeout=COMM_TIMEOUT + ) errors: dict[str, str] = {} try: - authorization = await self.hass.async_add_executor_job( - self._api.get_authorization, self._username, self._password + authorization = await self._api.get_authorization( + self._username, self._password ) except LoginError as exc: LOGGER.debug("Login error: %s", exc) diff --git a/homeassistant/components/life360/const.py b/homeassistant/components/life360/const.py index ccaf69877d6..21bf9a89c5e 100644 --- a/homeassistant/components/life360/const.py +++ b/homeassistant/components/life360/const.py @@ -7,7 +7,6 @@ DOMAIN = "life360" LOGGER = logging.getLogger(__package__) ATTRIBUTION = "Data provided by life360.com" -COMM_MAX_RETRIES = 2 COMM_TIMEOUT = 3.05 SPEED_FACTOR_MPH = 2.25 SPEED_DIGITS = 1 diff --git a/homeassistant/components/life360/coordinator.py b/homeassistant/components/life360/coordinator.py index ed774bba8ca..a098f1f6735 100644 --- a/homeassistant/components/life360/coordinator.py +++ b/homeassistant/components/life360/coordinator.py @@ -19,12 +19,12 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed +from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from homeassistant.util.distance import convert import homeassistant.util.dt as dt_util from .const import ( - COMM_MAX_RETRIES, COMM_TIMEOUT, CONF_AUTHORIZATION, DOMAIN, @@ -106,8 +106,8 @@ class Life360DataUpdateCoordinator(DataUpdateCoordinator[Life360Data]): ) self._hass = hass self._api = Life360( + session=async_get_clientsession(hass), timeout=COMM_TIMEOUT, - max_retries=COMM_MAX_RETRIES, authorization=entry.data[CONF_AUTHORIZATION], ) self._missing_loc_reason = hass.data[DOMAIN].missing_loc_reason @@ -115,9 +115,7 @@ class Life360DataUpdateCoordinator(DataUpdateCoordinator[Life360Data]): async def _retrieve_data(self, func: str, *args: Any) -> list[dict[str, Any]]: """Get data from Life360.""" try: - return await self._hass.async_add_executor_job( - getattr(self._api, func), *args - ) + return await getattr(self._api, func)(*args) except LoginError as exc: LOGGER.debug("Login error: %s", exc) raise ConfigEntryAuthFailed from exc diff --git a/homeassistant/components/life360/manifest.json b/homeassistant/components/life360/manifest.json index 23fdad892d2..c1a69b245ff 100644 --- a/homeassistant/components/life360/manifest.json +++ b/homeassistant/components/life360/manifest.json @@ -4,7 +4,7 @@ "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/life360", "codeowners": ["@pnbruckner"], - "requirements": ["life360==4.1.1"], + "requirements": ["life360==5.1.0"], "iot_class": "cloud_polling", "loggers": ["life360"] } diff --git a/requirements_all.txt b/requirements_all.txt index ffbff8840a0..175f5a88e82 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -980,7 +980,7 @@ librouteros==3.2.0 libsoundtouch==0.8 # homeassistant.components.life360 -life360==4.1.1 +life360==5.1.0 # homeassistant.components.osramlightify lightify==1.0.7.3 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index bc79492c7a2..b8472bbafbb 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -715,7 +715,7 @@ librouteros==3.2.0 libsoundtouch==0.8 # homeassistant.components.life360 -life360==4.1.1 +life360==5.1.0 # homeassistant.components.logi_circle logi_circle==0.2.3 diff --git a/tests/components/life360/test_config_flow.py b/tests/components/life360/test_config_flow.py index 02d5539e117..b614549fc04 100644 --- a/tests/components/life360/test_config_flow.py +++ b/tests/components/life360/test_config_flow.py @@ -76,7 +76,9 @@ def life360_fixture(): @pytest.fixture def life360_api(): """Mock Life360 api.""" - with patch("homeassistant.components.life360.config_flow.Life360") as mock: + with patch( + "homeassistant.components.life360.config_flow.Life360", autospec=True + ) as mock: yield mock.return_value