Update Withings measurements incrementally after the first update (#102002)

* Update incrementally after the first update

* Update incrementally after the first update
This commit is contained in:
Joost Lekkerkerker 2023-10-14 16:19:04 +02:00 committed by GitHub
parent 302b444269
commit 76083a0b4c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 171 additions and 10 deletions

View file

@ -2,6 +2,7 @@
from datetime import timedelta
from unittest.mock import AsyncMock
from aiowithings import MeasurementGroup
from freezegun.api import FrozenDateTimeFactory
import pytest
from syrupy import SnapshotAssertion
@ -16,7 +17,11 @@ from homeassistant.helpers import entity_registry as er
from . import setup_integration
from .conftest import USER_ID
from tests.common import MockConfigEntry, async_fire_time_changed
from tests.common import (
MockConfigEntry,
async_fire_time_changed,
load_json_array_fixture,
)
async def async_get_entity_id(
@ -57,7 +62,7 @@ async def test_update_failed(
"""Test all entities."""
await setup_integration(hass, polling_config_entry, False)
withings.get_measurement_in_period.side_effect = Exception
withings.get_measurement_since.side_effect = Exception
freezer.tick(timedelta(minutes=10))
async_fire_time_changed(hass)
await hass.async_block_till_done()
@ -65,3 +70,49 @@ async def test_update_failed(
state = hass.states.get("sensor.henk_weight")
assert state is not None
assert state.state == STATE_UNAVAILABLE
async def test_update_updates_incrementally(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
withings: AsyncMock,
polling_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test fetching new data updates since the last valid update."""
await setup_integration(hass, polling_config_entry, False)
async def _skip_10_minutes() -> None:
freezer.tick(timedelta(minutes=10))
async_fire_time_changed(hass)
await hass.async_block_till_done()
meas_json = load_json_array_fixture("withings/get_meas_1.json")
measurement_groups = [
MeasurementGroup.from_api(measurement) for measurement in meas_json
]
assert withings.get_measurement_since.call_args_list == []
await _skip_10_minutes()
assert (
str(withings.get_measurement_since.call_args_list[0].args[0])
== "2019-08-01 12:00:00+00:00"
)
withings.get_measurement_since.return_value = measurement_groups
await _skip_10_minutes()
assert (
str(withings.get_measurement_since.call_args_list[1].args[0])
== "2019-08-01 12:00:00+00:00"
)
await _skip_10_minutes()
assert (
str(withings.get_measurement_since.call_args_list[2].args[0])
== "2021-04-16 20:30:55+00:00"
)
state = hass.states.get("sensor.henk_weight")
assert state is not None
assert state.state == "71"
assert len(withings.get_measurement_in_period.call_args_list) == 1