Convert life360 to aiohttp (#77508)

Bump life360 package to 5.1.0.
This commit is contained in:
Phil Bruckner 2022-08-31 14:54:23 -05:00 committed by GitHub
parent c98ee412c0
commit c6b3b9fa90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 20 additions and 20 deletions

View File

@ -12,10 +12,10 @@ from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from .const import ( from .const import (
COMM_MAX_RETRIES,
COMM_TIMEOUT, COMM_TIMEOUT,
CONF_AUTHORIZATION, CONF_AUTHORIZATION,
CONF_DRIVING_SPEED, CONF_DRIVING_SPEED,
@ -53,13 +53,10 @@ class Life360ConfigFlow(ConfigFlow, domain=DOMAIN):
"""Life360 integration config flow.""" """Life360 integration config flow."""
VERSION = 1 VERSION = 1
_api: Life360 | None = None
def __init__(self) -> None: _username: str | vol.UNDEFINED = vol.UNDEFINED
"""Initialize.""" _password: str | vol.UNDEFINED = vol.UNDEFINED
self._api = Life360(timeout=COMM_TIMEOUT, max_retries=COMM_MAX_RETRIES) _reauth_entry: ConfigEntry | None = None
self._username: str | vol.UNDEFINED = vol.UNDEFINED
self._password: str | vol.UNDEFINED = vol.UNDEFINED
self._reauth_entry: ConfigEntry | None = None
@staticmethod @staticmethod
@callback @callback
@ -69,10 +66,14 @@ class Life360ConfigFlow(ConfigFlow, domain=DOMAIN):
async def _async_verify(self, step_id: str) -> FlowResult: async def _async_verify(self, step_id: str) -> FlowResult:
"""Attempt to authorize the provided credentials.""" """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] = {} errors: dict[str, str] = {}
try: try:
authorization = await self.hass.async_add_executor_job( authorization = await self._api.get_authorization(
self._api.get_authorization, self._username, self._password self._username, self._password
) )
except LoginError as exc: except LoginError as exc:
LOGGER.debug("Login error: %s", exc) LOGGER.debug("Login error: %s", exc)

View File

@ -7,7 +7,6 @@ DOMAIN = "life360"
LOGGER = logging.getLogger(__package__) LOGGER = logging.getLogger(__package__)
ATTRIBUTION = "Data provided by life360.com" ATTRIBUTION = "Data provided by life360.com"
COMM_MAX_RETRIES = 2
COMM_TIMEOUT = 3.05 COMM_TIMEOUT = 3.05
SPEED_FACTOR_MPH = 2.25 SPEED_FACTOR_MPH = 2.25
SPEED_DIGITS = 1 SPEED_DIGITS = 1

View File

@ -19,12 +19,12 @@ from homeassistant.const import (
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.util.distance import convert from homeassistant.util.distance import convert
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from .const import ( from .const import (
COMM_MAX_RETRIES,
COMM_TIMEOUT, COMM_TIMEOUT,
CONF_AUTHORIZATION, CONF_AUTHORIZATION,
DOMAIN, DOMAIN,
@ -106,8 +106,8 @@ class Life360DataUpdateCoordinator(DataUpdateCoordinator[Life360Data]):
) )
self._hass = hass self._hass = hass
self._api = Life360( self._api = Life360(
session=async_get_clientsession(hass),
timeout=COMM_TIMEOUT, timeout=COMM_TIMEOUT,
max_retries=COMM_MAX_RETRIES,
authorization=entry.data[CONF_AUTHORIZATION], authorization=entry.data[CONF_AUTHORIZATION],
) )
self._missing_loc_reason = hass.data[DOMAIN].missing_loc_reason 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]]: async def _retrieve_data(self, func: str, *args: Any) -> list[dict[str, Any]]:
"""Get data from Life360.""" """Get data from Life360."""
try: try:
return await self._hass.async_add_executor_job( return await getattr(self._api, func)(*args)
getattr(self._api, func), *args
)
except LoginError as exc: except LoginError as exc:
LOGGER.debug("Login error: %s", exc) LOGGER.debug("Login error: %s", exc)
raise ConfigEntryAuthFailed from exc raise ConfigEntryAuthFailed from exc

View File

@ -4,7 +4,7 @@
"config_flow": true, "config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/life360", "documentation": "https://www.home-assistant.io/integrations/life360",
"codeowners": ["@pnbruckner"], "codeowners": ["@pnbruckner"],
"requirements": ["life360==4.1.1"], "requirements": ["life360==5.1.0"],
"iot_class": "cloud_polling", "iot_class": "cloud_polling",
"loggers": ["life360"] "loggers": ["life360"]
} }

View File

@ -980,7 +980,7 @@ librouteros==3.2.0
libsoundtouch==0.8 libsoundtouch==0.8
# homeassistant.components.life360 # homeassistant.components.life360
life360==4.1.1 life360==5.1.0
# homeassistant.components.osramlightify # homeassistant.components.osramlightify
lightify==1.0.7.3 lightify==1.0.7.3

View File

@ -715,7 +715,7 @@ librouteros==3.2.0
libsoundtouch==0.8 libsoundtouch==0.8
# homeassistant.components.life360 # homeassistant.components.life360
life360==4.1.1 life360==5.1.0
# homeassistant.components.logi_circle # homeassistant.components.logi_circle
logi_circle==0.2.3 logi_circle==0.2.3

View File

@ -76,7 +76,9 @@ def life360_fixture():
@pytest.fixture @pytest.fixture
def life360_api(): def life360_api():
"""Mock 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 yield mock.return_value