2020-06-16 18:16:18 +00:00
|
|
|
"""Tests for the Withings component."""
|
2023-09-26 07:17:11 +00:00
|
|
|
from datetime import timedelta
|
2023-09-11 19:06:20 +00:00
|
|
|
from unittest.mock import AsyncMock
|
2020-06-16 18:16:18 +00:00
|
|
|
|
2023-10-14 14:19:04 +00:00
|
|
|
from aiowithings import MeasurementGroup
|
2023-09-26 07:17:11 +00:00
|
|
|
from freezegun.api import FrozenDateTimeFactory
|
2023-09-09 17:11:28 +00:00
|
|
|
import pytest
|
|
|
|
from syrupy import SnapshotAssertion
|
2020-06-16 18:16:18 +00:00
|
|
|
|
2023-10-15 10:14:50 +00:00
|
|
|
from homeassistant.const import STATE_UNAVAILABLE, Platform
|
2023-10-12 11:42:00 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-03-09 13:25:03 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
2020-06-16 18:16:18 +00:00
|
|
|
|
2023-10-12 11:42:00 +00:00
|
|
|
from . import setup_integration
|
2023-09-09 17:11:28 +00:00
|
|
|
|
2023-10-14 14:19:04 +00:00
|
|
|
from tests.common import (
|
|
|
|
MockConfigEntry,
|
|
|
|
async_fire_time_changed,
|
|
|
|
load_json_array_fixture,
|
2023-10-15 16:00:52 +00:00
|
|
|
load_json_object_fixture,
|
2023-10-14 14:19:04 +00:00
|
|
|
)
|
2020-06-16 18:16:18 +00:00
|
|
|
|
|
|
|
|
2023-09-09 17:11:28 +00:00
|
|
|
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
|
|
|
async def test_all_entities(
|
2023-09-11 19:06:20 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
snapshot: SnapshotAssertion,
|
|
|
|
withings: AsyncMock,
|
2023-09-26 07:17:11 +00:00
|
|
|
polling_config_entry: MockConfigEntry,
|
2023-09-09 17:11:28 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test all entities."""
|
2023-09-26 07:17:11 +00:00
|
|
|
await setup_integration(hass, polling_config_entry)
|
2023-10-15 10:14:50 +00:00
|
|
|
entity_registry = er.async_get(hass)
|
|
|
|
entities = er.async_entries_for_config_entry(
|
|
|
|
entity_registry, polling_config_entry.entry_id
|
|
|
|
)
|
2023-09-11 19:06:20 +00:00
|
|
|
|
2023-10-15 10:14:50 +00:00
|
|
|
for entity in entities:
|
2023-10-15 19:38:30 +00:00
|
|
|
if entity.domain == Platform.SENSOR:
|
2023-10-15 10:14:50 +00:00
|
|
|
assert hass.states.get(entity.entity_id) == snapshot
|
2023-10-15 19:38:30 +00:00
|
|
|
assert entities
|
2023-09-26 07:17:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_update_failed(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
snapshot: SnapshotAssertion,
|
|
|
|
withings: AsyncMock,
|
|
|
|
polling_config_entry: MockConfigEntry,
|
|
|
|
freezer: FrozenDateTimeFactory,
|
|
|
|
) -> None:
|
|
|
|
"""Test all entities."""
|
2023-09-26 19:52:18 +00:00
|
|
|
await setup_integration(hass, polling_config_entry, False)
|
2023-09-26 07:17:11 +00:00
|
|
|
|
2023-10-14 14:19:04 +00:00
|
|
|
withings.get_measurement_since.side_effect = Exception
|
2023-09-26 07:17:11 +00:00
|
|
|
freezer.tick(timedelta(minutes=10))
|
|
|
|
async_fire_time_changed(hass)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.henk_weight")
|
|
|
|
assert state is not None
|
|
|
|
assert state.state == STATE_UNAVAILABLE
|
2023-10-14 14:19:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2023-10-15 16:00:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_update_new_measurement_creates_new_sensor(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
withings: AsyncMock,
|
|
|
|
polling_config_entry: MockConfigEntry,
|
|
|
|
freezer: FrozenDateTimeFactory,
|
|
|
|
) -> None:
|
|
|
|
"""Test fetching a new measurement will add a new sensor."""
|
|
|
|
meas_json = load_json_array_fixture("withings/get_meas_1.json")
|
|
|
|
measurement_groups = [
|
|
|
|
MeasurementGroup.from_api(measurement) for measurement in meas_json
|
|
|
|
]
|
|
|
|
withings.get_measurement_in_period.return_value = measurement_groups
|
|
|
|
await setup_integration(hass, polling_config_entry, False)
|
|
|
|
|
|
|
|
assert hass.states.get("sensor.henk_fat_mass") is None
|
|
|
|
|
|
|
|
meas_json = load_json_object_fixture("withings/get_meas.json")
|
|
|
|
measurement_groups = [
|
|
|
|
MeasurementGroup.from_api(measurement)
|
|
|
|
for measurement in meas_json["measuregrps"]
|
|
|
|
]
|
|
|
|
withings.get_measurement_in_period.return_value = measurement_groups
|
|
|
|
|
|
|
|
freezer.tick(timedelta(minutes=10))
|
|
|
|
async_fire_time_changed(hass)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get("sensor.henk_fat_mass") is not None
|