Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix withings wrong sleep state entry #29651

Merged
merged 4 commits into from Dec 10, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion homeassistant/components/withings/sensor.py
Expand Up @@ -10,6 +10,7 @@
get_measure_value,
MeasureGroupAttribs,
SleepState,
SleepGetSerie
)

from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -382,7 +383,11 @@ async def async_update_sleep_state(self, data: SleepGetResponse) -> None:
self._state = None
return

serie = data.series[len(data.series) - 1]
def sort_serie(serie: SleepGetSerie) -> str:
return serie.startdate

sorted_series = sorted(data.series, key=sort_serie)
vangorra marked this conversation as resolved.
Show resolved Hide resolved
serie = sorted_series[len(data.series) - 1]
state = None
if serie.state == SleepState.AWAKE:
state = const.STATE_AWAKE
Expand Down
22 changes: 21 additions & 1 deletion tests/components/withings/test_init.py
Expand Up @@ -308,6 +308,11 @@ 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,
}
],
Expand All @@ -329,10 +334,15 @@ 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,
}
],
},
Expand All @@ -356,7 +366,17 @@ 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,
}
],
},
Expand Down