diff --git a/homeassistant/components/withings/__init__.py b/homeassistant/components/withings/__init__.py index 482c4e96e5c..92c3f2ae155 100644 --- a/homeassistant/components/withings/__init__.py +++ b/homeassistant/components/withings/__init__.py @@ -7,11 +7,11 @@ import voluptuous as vol from withings_api import WithingsAuth from homeassistant.config_entries import ConfigEntry +from homeassistant.helpers import config_entry_oauth2_flow, config_validation as cv from homeassistant.helpers.typing import ConfigType, HomeAssistantType -from homeassistant.helpers import config_validation as cv, config_entry_oauth2_flow from . import config_flow, const -from .common import _LOGGER, get_data_manager, NotAuthenticatedError +from .common import _LOGGER, NotAuthenticatedError, get_data_manager DOMAIN = const.DOMAIN diff --git a/homeassistant/components/withings/common.py b/homeassistant/components/withings/common.py index 655776ae004..5ec15eeac54 100644 --- a/homeassistant/components/withings/common.py +++ b/homeassistant/components/withings/common.py @@ -1,4 +1,5 @@ """Common code for Withings.""" +from asyncio import run_coroutine_threadsafe import datetime from functools import partial import logging @@ -6,15 +7,14 @@ import re import time from typing import Any, Dict -from asyncio import run_coroutine_threadsafe import requests from withings_api import ( AbstractWithingsApi, - SleepGetResponse, MeasureGetMeasResponse, + SleepGetResponse, SleepGetSummaryResponse, ) -from withings_api.common import UnauthorizedException, AuthFailedException +from withings_api.common import AuthFailedException, UnauthorizedException from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant diff --git a/homeassistant/components/withings/sensor.py b/homeassistant/components/withings/sensor.py index 17eae93ec0d..9b5ceab64bf 100644 --- a/homeassistant/components/withings/sensor.py +++ b/homeassistant/components/withings/sensor.py @@ -2,21 +2,21 @@ from typing import Callable, List, Union from withings_api.common import ( - MeasureType, GetSleepSummaryField, MeasureGetMeasResponse, + MeasureGroupAttribs, + MeasureType, SleepGetResponse, SleepGetSummaryResponse, - get_measure_value, - MeasureGroupAttribs, SleepState, + get_measure_value, ) from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant +from homeassistant.helpers import config_entry_oauth2_flow from homeassistant.helpers.entity import Entity from homeassistant.util import slugify -from homeassistant.helpers import config_entry_oauth2_flow from . import const from .common import _LOGGER, WithingsDataManager, get_data_manager @@ -382,7 +382,8 @@ class WithingsHealthSensor(Entity): self._state = None return - serie = data.series[len(data.series) - 1] + sorted_series = sorted(data.series, key=lambda serie: serie.startdate) + serie = sorted_series[len(sorted_series) - 1] state = None if serie.state == SleepState.AWAKE: state = const.STATE_AWAKE diff --git a/tests/components/withings/test_common.py b/tests/components/withings/test_common.py index f6075c0f734..9328526d6ef 100644 --- a/tests/components/withings/test_common.py +++ b/tests/components/withings/test_common.py @@ -1,15 +1,14 @@ """Tests for the Withings component.""" from asynctest import MagicMock - import pytest from withings_api import WithingsApi -from withings_api.common import UnauthorizedException, TimeoutException +from withings_api.common import TimeoutException, UnauthorizedException -from homeassistant.exceptions import PlatformNotReady from homeassistant.components.withings.common import ( NotAuthenticatedError, WithingsDataManager, ) +from homeassistant.exceptions import PlatformNotReady @pytest.fixture(name="withings_api") diff --git a/tests/components/withings/test_init.py b/tests/components/withings/test_init.py index bd4940d9504..286b28b61ff 100644 --- a/tests/components/withings/test_init.py +++ b/tests/components/withings/test_init.py @@ -10,26 +10,26 @@ from withings_api.common import SleepModel, SleepState import homeassistant.components.http as http from homeassistant.components.withings import ( + CONFIG_SCHEMA, async_setup, async_setup_entry, const, - CONFIG_SCHEMA, ) from homeassistant.const import STATE_UNKNOWN from homeassistant.core import HomeAssistant from .common import ( - assert_state_equals, - configure_integration, - setup_hass, WITHINGS_GET_DEVICE_RESPONSE, WITHINGS_GET_DEVICE_RESPONSE_EMPTY, + WITHINGS_MEASURES_RESPONSE, + WITHINGS_MEASURES_RESPONSE_EMPTY, WITHINGS_SLEEP_RESPONSE, WITHINGS_SLEEP_RESPONSE_EMPTY, WITHINGS_SLEEP_SUMMARY_RESPONSE, WITHINGS_SLEEP_SUMMARY_RESPONSE_EMPTY, - WITHINGS_MEASURES_RESPONSE, - WITHINGS_MEASURES_RESPONSE_EMPTY, + assert_state_equals, + configure_integration, + setup_hass, ) @@ -308,8 +308,13 @@ async def test_full_setup(hass: HomeAssistant, aiohttp_client, aioclient_mock) - { "startdate": "2019-02-01 00:00:00", "enddate": "2019-02-01 01:00:00", + "state": SleepState.REM.real, + }, + { + "startdate": "2019-02-01 01:00:00", + "enddate": "2019-02-01 02:00:00", "state": SleepState.AWAKE.real, - } + }, ], }, }, @@ -329,11 +334,16 @@ async def test_full_setup(hass: HomeAssistant, aiohttp_client, aioclient_mock) - "body": { "model": SleepModel.TRACKER.real, "series": [ + { + "startdate": "2019-02-01 01:00:00", + "enddate": "2019-02-01 02:00:00", + "state": SleepState.LIGHT.real, + }, { "startdate": "2019-02-01 00:00:00", "enddate": "2019-02-01 01:00:00", - "state": SleepState.LIGHT.real, - } + "state": SleepState.REM.real, + }, ], }, }, @@ -356,8 +366,18 @@ async def test_full_setup(hass: HomeAssistant, aiohttp_client, aioclient_mock) - { "startdate": "2019-02-01 00:00:00", "enddate": "2019-02-01 01:00:00", + "state": SleepState.LIGHT.real, + }, + { + "startdate": "2019-02-01 02:00:00", + "enddate": "2019-02-01 03:00:00", "state": SleepState.REM.real, - } + }, + { + "startdate": "2019-02-01 01:00:00", + "enddate": "2019-02-01 02:00:00", + "state": SleepState.AWAKE.real, + }, ], }, },