Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions homeassistant/components/withings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import re
import time

import nokia
import withings_api as withings
from oauthlib.oauth2.rfc6749.errors import MissingTokenError
from requests_oauthlib import TokenUpdated

Expand Down Expand Up @@ -68,7 +68,9 @@ class WithingsDataManager:

service_available = None

def __init__(self, hass: HomeAssistantType, profile: str, api: nokia.NokiaApi):
def __init__(
self, hass: HomeAssistantType, profile: str, api: withings.WithingsApi
):
"""Constructor."""
self._hass = hass
self._api = api
Expand Down Expand Up @@ -253,7 +255,7 @@ def create_withings_data_manager(
"""Set up the sensor config entry."""
entry_creds = entry.data.get(const.CREDENTIALS) or {}
profile = entry.data[const.PROFILE]
credentials = nokia.NokiaCredentials(
credentials = withings.WithingsCredentials(
entry_creds.get("access_token"),
entry_creds.get("token_expiry"),
entry_creds.get("token_type"),
Expand All @@ -266,7 +268,7 @@ def create_withings_data_manager(
def credentials_saver(credentials_param):
_LOGGER.debug("Saving updated credentials of type %s", type(credentials_param))

# Sanitizing the data as sometimes a NokiaCredentials object
# Sanitizing the data as sometimes a WithingsCredentials object
# is passed through from the API.
cred_data = credentials_param
if not isinstance(credentials_param, dict):
Expand All @@ -275,8 +277,8 @@ def credentials_saver(credentials_param):
entry.data[const.CREDENTIALS] = cred_data
hass.config_entries.async_update_entry(entry, data={**entry.data})

_LOGGER.debug("Creating nokia api instance")
api = nokia.NokiaApi(
_LOGGER.debug("Creating withings api instance")
api = withings.WithingsApi(
credentials, refresh_cb=(lambda token: credentials_saver(api.credentials))
)

Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/withings/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Optional

import aiohttp
import nokia
import withings_api as withings
import voluptuous as vol

from homeassistant import config_entries, data_entry_flow
Expand Down Expand Up @@ -75,7 +75,7 @@ def get_auth_client(self, profile: str):
profile,
)

return nokia.NokiaAuth(
return withings.WithingsAuth(
client_id,
client_secret,
callback_uri,
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/withings/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"config_flow": true,
"documentation": "https://www.home-assistant.io/components/withings",
"requirements": [
"nokia==1.2.0"
"withings-api==2.0.0b7"
],
"dependencies": [
"api",
Expand Down
12 changes: 6 additions & 6 deletions tests/components/withings/common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Common data for for the withings component tests."""
import time

import nokia
import withings_api as withings

import homeassistant.components.withings.const as const

Expand Down Expand Up @@ -92,7 +92,7 @@ def new_measure(type_str, value, unit):
}


def nokia_sleep_response(states):
def withings_sleep_response(states):
"""Create a sleep response based on states."""
data = []
for state in states:
Expand All @@ -104,10 +104,10 @@ def nokia_sleep_response(states):
)
)

return nokia.NokiaSleep(new_sleep_data("aa", data))
return withings.WithingsSleep(new_sleep_data("aa", data))


NOKIA_MEASURES_RESPONSE = nokia.NokiaMeasures(
WITHINGS_MEASURES_RESPONSE = withings.WithingsMeasures(
{
"updatetime": "",
"timezone": "",
Expand Down Expand Up @@ -174,7 +174,7 @@ def nokia_sleep_response(states):
)


NOKIA_SLEEP_RESPONSE = nokia_sleep_response(
WITHINGS_SLEEP_RESPONSE = withings_sleep_response(
[
const.MEASURE_TYPE_SLEEP_STATE_AWAKE,
const.MEASURE_TYPE_SLEEP_STATE_LIGHT,
Expand All @@ -183,7 +183,7 @@ def nokia_sleep_response(states):
]
)

NOKIA_SLEEP_SUMMARY_RESPONSE = nokia.NokiaSleepSummary(
WITHINGS_SLEEP_SUMMARY_RESPONSE = withings.WithingsSleepSummary(
{
"series": [
new_sleep_summary(
Expand Down
139 changes: 72 additions & 67 deletions tests/components/withings/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Awaitable, Callable, List

import asynctest
import nokia
import withings_api as withings
import pytest

import homeassistant.components.api as api
Expand All @@ -15,9 +15,9 @@
from homeassistant.setup import async_setup_component

from .common import (
NOKIA_MEASURES_RESPONSE,
NOKIA_SLEEP_RESPONSE,
NOKIA_SLEEP_SUMMARY_RESPONSE,
WITHINGS_MEASURES_RESPONSE,
WITHINGS_SLEEP_RESPONSE,
WITHINGS_SLEEP_SUMMARY_RESPONSE,
)


Expand All @@ -34,17 +34,17 @@ def __init__(
measures: List[str] = None,
unit_system: str = None,
throttle_interval: int = const.THROTTLE_INTERVAL,
nokia_request_response="DATA",
nokia_measures_response: nokia.NokiaMeasures = NOKIA_MEASURES_RESPONSE,
nokia_sleep_response: nokia.NokiaSleep = NOKIA_SLEEP_RESPONSE,
nokia_sleep_summary_response: nokia.NokiaSleepSummary = NOKIA_SLEEP_SUMMARY_RESPONSE,
withings_request_response="DATA",
withings_measures_response: withings.WithingsMeasures = WITHINGS_MEASURES_RESPONSE,
withings_sleep_response: withings.WithingsSleep = WITHINGS_SLEEP_RESPONSE,
withings_sleep_summary_response: withings.WithingsSleepSummary = WITHINGS_SLEEP_SUMMARY_RESPONSE,
) -> None:
"""Constructor."""
self._throttle_interval = throttle_interval
self._nokia_request_response = nokia_request_response
self._nokia_measures_response = nokia_measures_response
self._nokia_sleep_response = nokia_sleep_response
self._nokia_sleep_summary_response = nokia_sleep_summary_response
self._withings_request_response = withings_request_response
self._withings_measures_response = withings_measures_response
self._withings_sleep_response = withings_sleep_response
self._withings_sleep_summary_response = withings_sleep_summary_response
self._withings_config = {
const.CLIENT_ID: "my_client_id",
const.CLIENT_SECRET: "my_client_secret",
Expand Down Expand Up @@ -103,24 +103,24 @@ def throttle_interval(self):
return self._throttle_interval

@property
def nokia_request_response(self):
def withings_request_response(self):
"""Request response."""
return self._nokia_request_response
return self._withings_request_response

@property
def nokia_measures_response(self) -> nokia.NokiaMeasures:
def withings_measures_response(self) -> withings.WithingsMeasures:
"""Measures response."""
return self._nokia_measures_response
return self._withings_measures_response

@property
def nokia_sleep_response(self) -> nokia.NokiaSleep:
def withings_sleep_response(self) -> withings.WithingsSleep:
"""Sleep response."""
return self._nokia_sleep_response
return self._withings_sleep_response

@property
def nokia_sleep_summary_response(self) -> nokia.NokiaSleepSummary:
def withings_sleep_summary_response(self) -> withings.WithingsSleepSummary:
"""Sleep summary response."""
return self._nokia_sleep_summary_response
return self._withings_sleep_summary_response


class WithingsFactoryData:
Expand All @@ -130,21 +130,21 @@ def __init__(
self,
hass,
flow_id,
nokia_auth_get_credentials_mock,
nokia_api_request_mock,
nokia_api_get_measures_mock,
nokia_api_get_sleep_mock,
nokia_api_get_sleep_summary_mock,
withings_auth_get_credentials_mock,
withings_api_request_mock,
withings_api_get_measures_mock,
withings_api_get_sleep_mock,
withings_api_get_sleep_summary_mock,
data_manager_get_throttle_interval_mock,
):
"""Constructor."""
self._hass = hass
self._flow_id = flow_id
self._nokia_auth_get_credentials_mock = nokia_auth_get_credentials_mock
self._nokia_api_request_mock = nokia_api_request_mock
self._nokia_api_get_measures_mock = nokia_api_get_measures_mock
self._nokia_api_get_sleep_mock = nokia_api_get_sleep_mock
self._nokia_api_get_sleep_summary_mock = nokia_api_get_sleep_summary_mock
self._withings_auth_get_credentials_mock = withings_auth_get_credentials_mock
self._withings_api_request_mock = withings_api_request_mock
self._withings_api_get_measures_mock = withings_api_get_measures_mock
self._withings_api_get_sleep_mock = withings_api_get_sleep_mock
self._withings_api_get_sleep_summary_mock = withings_api_get_sleep_summary_mock
self._data_manager_get_throttle_interval_mock = (
data_manager_get_throttle_interval_mock
)
Expand All @@ -160,29 +160,29 @@ def flow_id(self):
return self._flow_id

@property
def nokia_auth_get_credentials_mock(self):
def withings_auth_get_credentials_mock(self):
"""Get auth credentials mock."""
return self._nokia_auth_get_credentials_mock
return self._withings_auth_get_credentials_mock

@property
def nokia_api_request_mock(self):
def withings_api_request_mock(self):
"""Get request mock."""
return self._nokia_api_request_mock
return self._withings_api_request_mock

@property
def nokia_api_get_measures_mock(self):
def withings_api_get_measures_mock(self):
"""Get measures mock."""
return self._nokia_api_get_measures_mock
return self._withings_api_get_measures_mock

@property
def nokia_api_get_sleep_mock(self):
def withings_api_get_sleep_mock(self):
"""Get sleep mock."""
return self._nokia_api_get_sleep_mock
return self._withings_api_get_sleep_mock

@property
def nokia_api_get_sleep_summary_mock(self):
def withings_api_get_sleep_summary_mock(self):
"""Get sleep summary mock."""
return self._nokia_api_get_sleep_summary_mock
return self._withings_api_get_sleep_summary_mock

@property
def data_manager_get_throttle_interval_mock(self):
Expand Down Expand Up @@ -243,9 +243,9 @@ async def factory(config: WithingsFactoryConfig) -> WithingsFactoryData:
assert await async_setup_component(hass, http.DOMAIN, config.hass_config)
assert await async_setup_component(hass, api.DOMAIN, config.hass_config)

nokia_auth_get_credentials_patch = asynctest.patch(
"nokia.NokiaAuth.get_credentials",
return_value=nokia.NokiaCredentials(
withings_auth_get_credentials_patch = asynctest.patch(
"withings_api.WithingsAuth.get_credentials",
return_value=withings.WithingsCredentials(
access_token="my_access_token",
token_expiry=time.time() + 600,
token_type="my_token_type",
Expand All @@ -255,28 +255,33 @@ async def factory(config: WithingsFactoryConfig) -> WithingsFactoryData:
consumer_secret=config.withings_config.get(const.CLIENT_SECRET),
),
)
nokia_auth_get_credentials_mock = nokia_auth_get_credentials_patch.start()
withings_auth_get_credentials_mock = withings_auth_get_credentials_patch.start()

nokia_api_request_patch = asynctest.patch(
"nokia.NokiaApi.request", return_value=config.nokia_request_response
withings_api_request_patch = asynctest.patch(
"withings_api.WithingsApi.request",
return_value=config.withings_request_response,
)
nokia_api_request_mock = nokia_api_request_patch.start()
withings_api_request_mock = withings_api_request_patch.start()

nokia_api_get_measures_patch = asynctest.patch(
"nokia.NokiaApi.get_measures", return_value=config.nokia_measures_response
withings_api_get_measures_patch = asynctest.patch(
"withings_api.WithingsApi.get_measures",
return_value=config.withings_measures_response,
)
nokia_api_get_measures_mock = nokia_api_get_measures_patch.start()
withings_api_get_measures_mock = withings_api_get_measures_patch.start()

nokia_api_get_sleep_patch = asynctest.patch(
"nokia.NokiaApi.get_sleep", return_value=config.nokia_sleep_response
withings_api_get_sleep_patch = asynctest.patch(
"withings_api.WithingsApi.get_sleep",
return_value=config.withings_sleep_response,
)
nokia_api_get_sleep_mock = nokia_api_get_sleep_patch.start()
withings_api_get_sleep_mock = withings_api_get_sleep_patch.start()

nokia_api_get_sleep_summary_patch = asynctest.patch(
"nokia.NokiaApi.get_sleep_summary",
return_value=config.nokia_sleep_summary_response,
withings_api_get_sleep_summary_patch = asynctest.patch(
"withings_api.WithingsApi.get_sleep_summary",
return_value=config.withings_sleep_summary_response,
)
withings_api_get_sleep_summary_mock = (
withings_api_get_sleep_summary_patch.start()
)
nokia_api_get_sleep_summary_mock = nokia_api_get_sleep_summary_patch.start()

data_manager_get_throttle_interval_patch = asynctest.patch(
"homeassistant.components.withings.common.WithingsDataManager"
Expand All @@ -295,11 +300,11 @@ async def factory(config: WithingsFactoryConfig) -> WithingsFactoryData:

patches.extend(
[
nokia_auth_get_credentials_patch,
nokia_api_request_patch,
nokia_api_get_measures_patch,
nokia_api_get_sleep_patch,
nokia_api_get_sleep_summary_patch,
withings_auth_get_credentials_patch,
withings_api_request_patch,
withings_api_get_measures_patch,
withings_api_get_sleep_patch,
withings_api_get_sleep_summary_patch,
data_manager_get_throttle_interval_patch,
get_measures_patch,
]
Expand Down Expand Up @@ -328,11 +333,11 @@ def create_task(*args):
return WithingsFactoryData(
hass,
flow_id,
nokia_auth_get_credentials_mock,
nokia_api_request_mock,
nokia_api_get_measures_mock,
nokia_api_get_sleep_mock,
nokia_api_get_sleep_summary_mock,
withings_auth_get_credentials_mock,
withings_api_request_mock,
withings_api_get_measures_mock,
withings_api_get_sleep_mock,
withings_api_get_sleep_summary_mock,
data_manager_get_throttle_interval_mock,
)

Expand Down
Loading