Skip to content

Commit

Permalink
Make Withings sleep sensor only show last night (#101993)
Browse files Browse the repository at this point in the history
  • Loading branch information
joostlek committed Oct 23, 2023
1 parent 30ba78c commit a6ade59
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
5 changes: 3 additions & 2 deletions homeassistant/components/withings/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
WithingsUnauthorizedError,
aggregate_measurements,
)
from aiowithings.helpers import aggregate_sleep_summary

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -145,7 +144,9 @@ async def _internal_update_data(self) -> SleepSummary | None:
SleepSummaryDataFields.TOTAL_TIME_AWAKE,
],
)
return aggregate_sleep_summary(response)
if not response:
return None
return response[0]


class WithingsBedPresenceDataUpdateCoordinator(WithingsDataUpdateCoordinator[None]):
Expand Down
28 changes: 14 additions & 14 deletions tests/components/withings/snapshots/test_sensor.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
'entity_id': 'sensor.henk_average_heart_rate',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '83',
'state': '103',
})
# ---
# name: test_all_entities[sensor.henk_average_respiratory_rate]
Expand Down Expand Up @@ -100,7 +100,7 @@
'entity_id': 'sensor.henk_breathing_disturbances_intensity',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '10',
'state': '9',
})
# ---
# name: test_all_entities[sensor.henk_deep_sleep]
Expand All @@ -116,7 +116,7 @@
'entity_id': 'sensor.henk_deep_sleep',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '26220',
'state': '5820',
})
# ---
# name: test_all_entities[sensor.henk_diastolic_blood_pressure]
Expand Down Expand Up @@ -315,7 +315,7 @@
'entity_id': 'sensor.henk_light_sleep',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '58440',
'state': '10440',
})
# ---
# name: test_all_entities[sensor.henk_maximum_heart_rate]
Expand All @@ -330,7 +330,7 @@
'entity_id': 'sensor.henk_maximum_heart_rate',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '108',
'state': '120',
})
# ---
# name: test_all_entities[sensor.henk_maximum_respiratory_rate]
Expand Down Expand Up @@ -359,7 +359,7 @@
'entity_id': 'sensor.henk_minimum_heart_rate',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '58',
'state': '70',
})
# ---
# name: test_all_entities[sensor.henk_minimum_respiratory_rate]
Expand Down Expand Up @@ -435,7 +435,7 @@
'entity_id': 'sensor.henk_rem_sleep',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '17280',
'state': '2400',
})
# ---
# name: test_all_entities[sensor.henk_skin_temperature]
Expand Down Expand Up @@ -481,7 +481,7 @@
'entity_id': 'sensor.henk_sleep_score',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '90',
'state': '37',
})
# ---
# name: test_all_entities[sensor.henk_snoring]
Expand All @@ -494,7 +494,7 @@
'entity_id': 'sensor.henk_snoring',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '1044',
'state': '1080',
})
# ---
# name: test_all_entities[sensor.henk_snoring_episode_count]
Expand All @@ -507,7 +507,7 @@
'entity_id': 'sensor.henk_snoring_episode_count',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '87',
'state': '18',
})
# ---
# name: test_all_entities[sensor.henk_soft_activity_today]
Expand Down Expand Up @@ -613,7 +613,7 @@
'entity_id': 'sensor.henk_time_to_sleep',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '780',
'state': '540',
})
# ---
# name: test_all_entities[sensor.henk_time_to_wakeup]
Expand All @@ -629,7 +629,7 @@
'entity_id': 'sensor.henk_time_to_wakeup',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '996',
'state': '1140',
})
# ---
# name: test_all_entities[sensor.henk_total_calories_burnt_today]
Expand Down Expand Up @@ -685,7 +685,7 @@
'entity_id': 'sensor.henk_wakeup_count',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '8',
'state': '1',
})
# ---
# name: test_all_entities[sensor.henk_wakeup_time]
Expand All @@ -701,7 +701,7 @@
'entity_id': 'sensor.henk_wakeup_time',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '3468',
'state': '3060',
})
# ---
# name: test_all_entities[sensor.henk_weight]
Expand Down
21 changes: 21 additions & 0 deletions tests/components/withings/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,24 @@ async def test_activity_sensors_created_when_receive_activity_data(
await hass.async_block_till_done()

assert hass.states.get("sensor.henk_steps_today") is not None


@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_no_sleep(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
withings: AsyncMock,
polling_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test no sleep found."""
await setup_integration(hass, polling_config_entry, False)

withings.get_sleep_summary_since.return_value = []
freezer.tick(timedelta(minutes=10))
async_fire_time_changed(hass)
await hass.async_block_till_done()

state = hass.states.get("sensor.henk_average_respiratory_rate")
assert state is not None
assert state.state == STATE_UNAVAILABLE

0 comments on commit a6ade59

Please sign in to comment.