diff --git a/tests/components/withings/__init__.py b/tests/components/withings/__init__.py index 9693d21f162..2425a5bd600 100644 --- a/tests/components/withings/__init__.py +++ b/tests/components/withings/__init__.py @@ -5,13 +5,19 @@ from typing import Any from urllib.parse import urlparse from aiohttp.test_utils import TestClient +from aiowithings import Goals, MeasurementGroup from freezegun.api import FrozenDateTimeFactory from homeassistant.components.webhook import async_generate_url from homeassistant.config import async_process_ha_core_config from homeassistant.core import HomeAssistant -from tests.common import MockConfigEntry, async_fire_time_changed +from tests.common import ( + MockConfigEntry, + async_fire_time_changed, + load_json_array_fixture, + load_json_object_fixture, +) @dataclass @@ -64,3 +70,17 @@ async def prepare_webhook_setup( freezer.tick(timedelta(seconds=1)) async_fire_time_changed(hass) await hass.async_block_till_done() + + +def load_goals_fixture(fixture: str = "withings/goals.json") -> Goals: + """Return goals from fixture.""" + goals_json = load_json_object_fixture(fixture) + return Goals.from_api(goals_json) + + +def load_measurements_fixture( + fixture: str = "withings/measurements.json", +) -> list[MeasurementGroup]: + """Return measurement from fixture.""" + meas_json = load_json_array_fixture(fixture) + return [MeasurementGroup.from_api(measurement) for measurement in meas_json] diff --git a/tests/components/withings/conftest.py b/tests/components/withings/conftest.py index 0131feba943..8a824d84917 100644 --- a/tests/components/withings/conftest.py +++ b/tests/components/withings/conftest.py @@ -3,7 +3,7 @@ from datetime import timedelta import time from unittest.mock import AsyncMock, patch -from aiowithings import Device, Goals, MeasurementGroup, SleepSummary, WithingsClient +from aiowithings import Device, SleepSummary, WithingsClient from aiowithings.models import NotificationConfiguration import pytest @@ -15,7 +15,8 @@ from homeassistant.components.withings.const import DOMAIN from homeassistant.core import HomeAssistant from homeassistant.setup import async_setup_component -from tests.common import MockConfigEntry, load_json_object_fixture +from tests.common import MockConfigEntry, load_json_array_fixture +from tests.components.withings import load_goals_fixture, load_measurements_fixture CLIENT_ID = "1234" CLIENT_SECRET = "5678" @@ -128,32 +129,24 @@ def polling_config_entry(expires_at: int, scopes: list[str]) -> MockConfigEntry: def mock_withings(): """Mock withings.""" - devices_json = load_json_object_fixture("withings/get_device.json") - devices = [Device.from_api(device) for device in devices_json["devices"]] + devices_json = load_json_array_fixture("withings/devices.json") + devices = [Device.from_api(device) for device in devices_json] - meas_json = load_json_object_fixture("withings/get_meas.json") - measurement_groups = [ - MeasurementGroup.from_api(measurement) - for measurement in meas_json["measuregrps"] - ] + measurement_groups = load_measurements_fixture("withings/measurements.json") - sleep_json = load_json_object_fixture("withings/get_sleep.json") + sleep_json = load_json_array_fixture("withings/sleep_summaries.json") sleep_summaries = [ - SleepSummary.from_api(sleep_summary) for sleep_summary in sleep_json["series"] + SleepSummary.from_api(sleep_summary) for sleep_summary in sleep_json ] - notification_json = load_json_object_fixture("withings/notify_list.json") + notification_json = load_json_array_fixture("withings/notifications.json") notifications = [ - NotificationConfiguration.from_api(not_conf) - for not_conf in notification_json["profiles"] + NotificationConfiguration.from_api(not_conf) for not_conf in notification_json ] - goals_json = load_json_object_fixture("withings/goals.json") - goals = Goals.from_api(goals_json) - mock = AsyncMock(spec=WithingsClient) mock.get_devices.return_value = devices - mock.get_goals.return_value = goals + mock.get_goals.return_value = load_goals_fixture("withings/goals.json") mock.get_measurement_in_period.return_value = measurement_groups mock.get_measurement_since.return_value = measurement_groups mock.get_sleep_summary_since.return_value = sleep_summaries diff --git a/tests/components/withings/fixtures/devices.json b/tests/components/withings/fixtures/devices.json new file mode 100644 index 00000000000..9a2b7b81cf4 --- /dev/null +++ b/tests/components/withings/fixtures/devices.json @@ -0,0 +1,13 @@ +[ + { + "type": "Scale", + "battery": "high", + "model": "Body+", + "model_id": 5, + "timezone": "Europe/Amsterdam", + "first_session_date": null, + "last_session_date": 1693867179, + "deviceid": "f998be4b9ccc9e136fd8cd8e8e344c31ec3b271d", + "hash_deviceid": "f998be4b9ccc9e136fd8cd8e8e344c31ec3b271d" + } +] diff --git a/tests/components/withings/fixtures/get_device.json b/tests/components/withings/fixtures/get_device.json deleted file mode 100644 index 64bac3d4a19..00000000000 --- a/tests/components/withings/fixtures/get_device.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "devices": [ - { - "type": "Scale", - "battery": "high", - "model": "Body+", - "model_id": 5, - "timezone": "Europe/Amsterdam", - "first_session_date": null, - "last_session_date": 1693867179, - "deviceid": "f998be4b9ccc9e136fd8cd8e8e344c31ec3b271d", - "hash_deviceid": "f998be4b9ccc9e136fd8cd8e8e344c31ec3b271d" - } - ] -} diff --git a/tests/components/withings/fixtures/get_meas.json b/tests/components/withings/fixtures/get_meas.json deleted file mode 100644 index d473b61c274..00000000000 --- a/tests/components/withings/fixtures/get_meas.json +++ /dev/null @@ -1,313 +0,0 @@ -{ - "more": false, - "timezone": "UTC", - "updatetime": 1564617600, - "offset": 0, - "measuregrps": [ - { - "grpid": 1, - "attrib": 0, - "date": 1564660800, - "created": 1564660800, - "modified": 1564660800, - "category": 1, - "deviceid": "91a7e556c2022ef54dca6e07a853c3193734d148", - "hash_deviceid": "91a7e556c2022ef54dca6e07a853c3193734d148", - "measures": [ - { - "type": 1, - "unit": 0, - "value": 70 - }, - { - "type": 8, - "unit": 0, - "value": 5 - }, - { - "type": 5, - "unit": 0, - "value": 60 - }, - { - "type": 76, - "unit": 0, - "value": 50 - }, - { - "type": 88, - "unit": 0, - "value": 10 - }, - { - "type": 4, - "unit": 0, - "value": 2 - }, - { - "type": 12, - "unit": 0, - "value": 40 - }, - { - "type": 71, - "unit": 0, - "value": 40 - }, - { - "type": 73, - "unit": 0, - "value": 20 - }, - { - "type": 6, - "unit": -3, - "value": 70 - }, - { - "type": 9, - "unit": 0, - "value": 70 - }, - { - "type": 10, - "unit": 0, - "value": 100 - }, - { - "type": 11, - "unit": 0, - "value": 60 - }, - { - "type": 54, - "unit": -2, - "value": 95 - }, - { - "type": 77, - "unit": -2, - "value": 95 - }, - { - "type": 91, - "unit": 0, - "value": 100 - }, - { - "type": 123, - "unit": 0, - "value": 100 - }, - { - "type": 155, - "unit": 0, - "value": 100 - }, - { - "type": 168, - "unit": 0, - "value": 100 - }, - { - "type": 169, - "unit": 0, - "value": 100 - } - ], - "modelid": 45, - "model": "BPM Connect", - "comment": null - }, - { - "grpid": 1, - "attrib": 0, - "date": 1564657200, - "created": 1564657200, - "modified": 1564657200, - "category": 1, - "deviceid": "91a7e556c2022ef54dca6e07a853c3193734d148", - "hash_deviceid": "91a7e556c2022ef54dca6e07a853c3193734d148", - "measures": [ - { - "type": 1, - "unit": 0, - "value": 71 - }, - { - "type": 8, - "unit": 0, - "value": 51 - }, - { - "type": 5, - "unit": 0, - "value": 61 - }, - { - "type": 76, - "unit": 0, - "value": 51 - }, - { - "type": 88, - "unit": 0, - "value": 11 - }, - { - "type": 4, - "unit": 0, - "value": 21 - }, - { - "type": 12, - "unit": 0, - "value": 41 - }, - { - "type": 71, - "unit": 0, - "value": 41 - }, - { - "type": 73, - "unit": 0, - "value": 21 - }, - { - "type": 6, - "unit": -3, - "value": 71 - }, - { - "type": 9, - "unit": 0, - "value": 71 - }, - { - "type": 10, - "unit": 0, - "value": 101 - }, - { - "type": 11, - "unit": 0, - "value": 61 - }, - { - "type": 54, - "unit": -2, - "value": 96 - }, - { - "type": 77, - "unit": -2, - "value": 96 - }, - { - "type": 91, - "unit": 0, - "value": 101 - } - ], - "modelid": 45, - "model": "BPM Connect", - "comment": null - }, - { - "grpid": 1, - "attrib": 1, - "date": 1564664400, - "created": 1564664400, - "modified": 1564664400, - "category": 1, - "deviceid": "91a7e556c2022ef54dca6e07a853c3193734d148", - "hash_deviceid": "91a7e556c2022ef54dca6e07a853c3193734d148", - "measures": [ - { - "type": 1, - "unit": 0, - "value": 71 - }, - { - "type": 8, - "unit": 0, - "value": 4 - }, - { - "type": 5, - "unit": 0, - "value": 40 - }, - { - "type": 76, - "unit": 0, - "value": 51 - }, - { - "type": 88, - "unit": 0, - "value": 11 - }, - { - "type": 4, - "unit": 0, - "value": 201 - }, - { - "type": 12, - "unit": 0, - "value": 41 - }, - { - "type": 71, - "unit": 0, - "value": 34 - }, - { - "type": 73, - "unit": 0, - "value": 21 - }, - { - "type": 6, - "unit": -3, - "value": 71 - }, - { - "type": 9, - "unit": 0, - "value": 71 - }, - { - "type": 10, - "unit": 0, - "value": 101 - }, - { - "type": 11, - "unit": 0, - "value": 61 - }, - { - "type": 54, - "unit": -2, - "value": 98 - }, - { - "type": 77, - "unit": -2, - "value": 96 - }, - { - "type": 91, - "unit": 0, - "value": 102 - } - ], - "modelid": 45, - "model": "BPM Connect", - "comment": null - } - ] -} diff --git a/tests/components/withings/fixtures/get_sleep.json b/tests/components/withings/fixtures/get_sleep.json deleted file mode 100644 index 29ed3df3fd3..00000000000 --- a/tests/components/withings/fixtures/get_sleep.json +++ /dev/null @@ -1,201 +0,0 @@ -{ - "more": false, - "offset": 0, - "series": [ - { - "id": 2081804182, - "timezone": "Europe/Paris", - "model": 32, - "model_id": 63, - "hash_deviceid": "201d0b9a0556d6b755166b2cf8d22d3bdf0487ee", - "startdate": 1618691453, - "enddate": 1618713173, - "date": "2021-04-18", - "data": { - "wakeupduration": 3060, - "wakeupcount": 1, - "durationtosleep": 540, - "remsleepduration": 2400, - "durationtowakeup": 1140, - "total_sleep_time": 18660, - "sleep_efficiency": 0.86, - "sleep_latency": 540, - "wakeup_latency": 1140, - "waso": 1380, - "nb_rem_episodes": 1, - "out_of_bed_count": 0, - "lightsleepduration": 10440, - "deepsleepduration": 5820, - "hr_average": 103, - "hr_min": 70, - "hr_max": 120, - "rr_average": 14, - "rr_min": 10, - "rr_max": 20, - "breathing_disturbances_intensity": 9, - "snoring": 1080, - "snoringepisodecount": 18, - "sleep_score": 37, - "apnea_hypopnea_index": 9 - }, - "created": 1620237476, - "modified": 1620237476 - }, - { - "id": 2081804265, - "timezone": "Europe/Paris", - "model": 32, - "model_id": 63, - "hash_deviceid": "201d0b9a0556d6b755166b2cf8d22d3bdf0487ee", - "startdate": 1618605055, - "enddate": 1618636975, - "date": "2021-04-17", - "data": { - "wakeupduration": 2520, - "wakeupcount": 3, - "durationtosleep": 900, - "remsleepduration": 6840, - "durationtowakeup": 420, - "total_sleep_time": 26880, - "sleep_efficiency": 0.91, - "sleep_latency": 900, - "wakeup_latency": 420, - "waso": 1200, - "nb_rem_episodes": 2, - "out_of_bed_count": 0, - "lightsleepduration": 12840, - "deepsleepduration": 7200, - "hr_average": 85, - "hr_min": 50, - "hr_max": 120, - "rr_average": 16, - "rr_min": 10, - "rr_max": 20, - "breathing_disturbances_intensity": 14, - "snoring": 1140, - "snoringepisodecount": 19, - "sleep_score": 90, - "apnea_hypopnea_index": 14 - }, - "created": 1620237480, - "modified": 1620237479 - }, - { - "id": 2081804358, - "timezone": "Europe/Paris", - "model": 32, - "model_id": 63, - "hash_deviceid": "201d0b9a0556d6b755166b2cf8d22d3bdf0487ee", - "startdate": 1618518658, - "enddate": 1618548058, - "date": "2021-04-16", - "data": { - "wakeupduration": 4080, - "wakeupcount": 1, - "durationtosleep": 840, - "remsleepduration": 2040, - "durationtowakeup": 1560, - "total_sleep_time": 16860, - "sleep_efficiency": 0.81, - "sleep_latency": 840, - "wakeup_latency": 1560, - "waso": 1680, - "nb_rem_episodes": 2, - "out_of_bed_count": 0, - "lightsleepduration": 11100, - "deepsleepduration": 3720, - "hr_average": 65, - "hr_min": 50, - "hr_max": 91, - "rr_average": 14, - "rr_min": 10, - "rr_max": 20, - "breathing_disturbances_intensity": -1, - "snoring": 1020, - "snoringepisodecount": 17, - "sleep_score": 20, - "apnea_hypopnea_index": -1 - }, - "created": 1620237484, - "modified": 1620237484 - }, - { - "id": 2081804405, - "timezone": "Europe/Paris", - "model": 32, - "model_id": 63, - "hash_deviceid": "201d0b9a0556d6b755166b2cf8d22d3bdf0487ee", - "startdate": 1618432203, - "enddate": 1618453143, - "date": "2021-04-15", - "data": { - "wakeupduration": 4080, - "wakeupcount": 1, - "durationtosleep": 840, - "remsleepduration": 2040, - "durationtowakeup": 1560, - "total_sleep_time": 16860, - "sleep_efficiency": 0.81, - "sleep_latency": 840, - "wakeup_latency": 1560, - "waso": 1680, - "nb_rem_episodes": 2, - "out_of_bed_count": 0, - "lightsleepduration": 11100, - "deepsleepduration": 3720, - "hr_average": 65, - "hr_min": 50, - "hr_max": 91, - "rr_average": 14, - "rr_min": 10, - "rr_max": 20, - "breathing_disturbances_intensity": -1, - "snoring": 1020, - "snoringepisodecount": 17, - "sleep_score": 20, - "apnea_hypopnea_index": -1 - }, - "created": 1620237486, - "modified": 1620237486 - }, - { - "id": 2081804490, - "timezone": "Europe/Paris", - "model": 32, - "model_id": 63, - "hash_deviceid": "201d0b9a0556d6b755166b2cf8d22d3bdf0487ee", - "startdate": 1618345805, - "enddate": 1618373504, - "date": "2021-04-14", - "data": { - "wakeupduration": 3600, - "wakeupcount": 2, - "durationtosleep": 780, - "remsleepduration": 3960, - "durationtowakeup": 300, - "total_sleep_time": 22680, - "sleep_efficiency": 0.86, - "sleep_latency": 780, - "wakeup_latency": 300, - "waso": 3939, - "nb_rem_episodes": 4, - "out_of_bed_count": 3, - "lightsleepduration": 12960, - "deepsleepduration": 5760, - "hr_average": 98, - "hr_min": 70, - "hr_max": 120, - "rr_average": 13, - "rr_min": 10, - "rr_max": 20, - "breathing_disturbances_intensity": 29, - "snoring": 960, - "snoringepisodecount": 16, - "sleep_score": 62, - "apnea_hypopnea_index": 29 - }, - "created": 1620237490, - "modified": 1620237489 - } - ] -} diff --git a/tests/components/withings/fixtures/measurements.json b/tests/components/withings/fixtures/measurements.json new file mode 100644 index 00000000000..3ed59a7c3f4 --- /dev/null +++ b/tests/components/withings/fixtures/measurements.json @@ -0,0 +1,307 @@ +[ + { + "grpid": 1, + "attrib": 0, + "date": 1564660800, + "created": 1564660800, + "modified": 1564660800, + "category": 1, + "deviceid": "91a7e556c2022ef54dca6e07a853c3193734d148", + "hash_deviceid": "91a7e556c2022ef54dca6e07a853c3193734d148", + "measures": [ + { + "type": 1, + "unit": 0, + "value": 70 + }, + { + "type": 8, + "unit": 0, + "value": 5 + }, + { + "type": 5, + "unit": 0, + "value": 60 + }, + { + "type": 76, + "unit": 0, + "value": 50 + }, + { + "type": 88, + "unit": 0, + "value": 10 + }, + { + "type": 4, + "unit": 0, + "value": 2 + }, + { + "type": 12, + "unit": 0, + "value": 40 + }, + { + "type": 71, + "unit": 0, + "value": 40 + }, + { + "type": 73, + "unit": 0, + "value": 20 + }, + { + "type": 6, + "unit": -3, + "value": 70 + }, + { + "type": 9, + "unit": 0, + "value": 70 + }, + { + "type": 10, + "unit": 0, + "value": 100 + }, + { + "type": 11, + "unit": 0, + "value": 60 + }, + { + "type": 54, + "unit": -2, + "value": 95 + }, + { + "type": 77, + "unit": -2, + "value": 95 + }, + { + "type": 91, + "unit": 0, + "value": 100 + }, + { + "type": 123, + "unit": 0, + "value": 100 + }, + { + "type": 155, + "unit": 0, + "value": 100 + }, + { + "type": 168, + "unit": 0, + "value": 100 + }, + { + "type": 169, + "unit": 0, + "value": 100 + } + ], + "modelid": 45, + "model": "BPM Connect", + "comment": null + }, + { + "grpid": 1, + "attrib": 0, + "date": 1564657200, + "created": 1564657200, + "modified": 1564657200, + "category": 1, + "deviceid": "91a7e556c2022ef54dca6e07a853c3193734d148", + "hash_deviceid": "91a7e556c2022ef54dca6e07a853c3193734d148", + "measures": [ + { + "type": 1, + "unit": 0, + "value": 71 + }, + { + "type": 8, + "unit": 0, + "value": 51 + }, + { + "type": 5, + "unit": 0, + "value": 61 + }, + { + "type": 76, + "unit": 0, + "value": 51 + }, + { + "type": 88, + "unit": 0, + "value": 11 + }, + { + "type": 4, + "unit": 0, + "value": 21 + }, + { + "type": 12, + "unit": 0, + "value": 41 + }, + { + "type": 71, + "unit": 0, + "value": 41 + }, + { + "type": 73, + "unit": 0, + "value": 21 + }, + { + "type": 6, + "unit": -3, + "value": 71 + }, + { + "type": 9, + "unit": 0, + "value": 71 + }, + { + "type": 10, + "unit": 0, + "value": 101 + }, + { + "type": 11, + "unit": 0, + "value": 61 + }, + { + "type": 54, + "unit": -2, + "value": 96 + }, + { + "type": 77, + "unit": -2, + "value": 96 + }, + { + "type": 91, + "unit": 0, + "value": 101 + } + ], + "modelid": 45, + "model": "BPM Connect", + "comment": null + }, + { + "grpid": 1, + "attrib": 1, + "date": 1564664400, + "created": 1564664400, + "modified": 1564664400, + "category": 1, + "deviceid": "91a7e556c2022ef54dca6e07a853c3193734d148", + "hash_deviceid": "91a7e556c2022ef54dca6e07a853c3193734d148", + "measures": [ + { + "type": 1, + "unit": 0, + "value": 71 + }, + { + "type": 8, + "unit": 0, + "value": 4 + }, + { + "type": 5, + "unit": 0, + "value": 40 + }, + { + "type": 76, + "unit": 0, + "value": 51 + }, + { + "type": 88, + "unit": 0, + "value": 11 + }, + { + "type": 4, + "unit": 0, + "value": 201 + }, + { + "type": 12, + "unit": 0, + "value": 41 + }, + { + "type": 71, + "unit": 0, + "value": 34 + }, + { + "type": 73, + "unit": 0, + "value": 21 + }, + { + "type": 6, + "unit": -3, + "value": 71 + }, + { + "type": 9, + "unit": 0, + "value": 71 + }, + { + "type": 10, + "unit": 0, + "value": 101 + }, + { + "type": 11, + "unit": 0, + "value": 61 + }, + { + "type": 54, + "unit": -2, + "value": 98 + }, + { + "type": 77, + "unit": -2, + "value": 96 + }, + { + "type": 91, + "unit": 0, + "value": 102 + } + ], + "modelid": 45, + "model": "BPM Connect", + "comment": null + } +] diff --git a/tests/components/withings/fixtures/get_meas_1.json b/tests/components/withings/fixtures/measurements_1.json similarity index 100% rename from tests/components/withings/fixtures/get_meas_1.json rename to tests/components/withings/fixtures/measurements_1.json diff --git a/tests/components/withings/fixtures/notifications.json b/tests/components/withings/fixtures/notifications.json new file mode 100644 index 00000000000..8f4d49fde49 --- /dev/null +++ b/tests/components/withings/fixtures/notifications.json @@ -0,0 +1,20 @@ +[ + { + "appli": 50, + "callbackurl": "https://not.my.callback/url", + "expires": 2147483647, + "comment": null + }, + { + "appli": 50, + "callbackurl": "https://example.com/api/webhook/55a7335ea8dee830eed4ef8f84cda8f6d80b83af0847dc74032e86120bffed5e", + "expires": 2147483647, + "comment": null + }, + { + "appli": 51, + "callbackurl": "https://example.com/api/webhook/55a7335ea8dee830eed4ef8f84cda8f6d80b83af0847dc74032e86120bffed5e", + "expires": 2147483647, + "comment": null + } +] diff --git a/tests/components/withings/fixtures/notify_list.json b/tests/components/withings/fixtures/notify_list.json deleted file mode 100644 index ef7a99857e4..00000000000 --- a/tests/components/withings/fixtures/notify_list.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "profiles": [ - { - "appli": 50, - "callbackurl": "https://not.my.callback/url", - "expires": 2147483647, - "comment": null - }, - { - "appli": 50, - "callbackurl": "https://example.com/api/webhook/55a7335ea8dee830eed4ef8f84cda8f6d80b83af0847dc74032e86120bffed5e", - "expires": 2147483647, - "comment": null - }, - { - "appli": 51, - "callbackurl": "https://example.com/api/webhook/55a7335ea8dee830eed4ef8f84cda8f6d80b83af0847dc74032e86120bffed5e", - "expires": 2147483647, - "comment": null - } - ] -} diff --git a/tests/components/withings/fixtures/sleep_summaries.json b/tests/components/withings/fixtures/sleep_summaries.json new file mode 100644 index 00000000000..1bcfcfcc1d2 --- /dev/null +++ b/tests/components/withings/fixtures/sleep_summaries.json @@ -0,0 +1,197 @@ +[ + { + "id": 2081804182, + "timezone": "Europe/Paris", + "model": 32, + "model_id": 63, + "hash_deviceid": "201d0b9a0556d6b755166b2cf8d22d3bdf0487ee", + "startdate": 1618691453, + "enddate": 1618713173, + "date": "2021-04-18", + "data": { + "wakeupduration": 3060, + "wakeupcount": 1, + "durationtosleep": 540, + "remsleepduration": 2400, + "durationtowakeup": 1140, + "total_sleep_time": 18660, + "sleep_efficiency": 0.86, + "sleep_latency": 540, + "wakeup_latency": 1140, + "waso": 1380, + "nb_rem_episodes": 1, + "out_of_bed_count": 0, + "lightsleepduration": 10440, + "deepsleepduration": 5820, + "hr_average": 103, + "hr_min": 70, + "hr_max": 120, + "rr_average": 14, + "rr_min": 10, + "rr_max": 20, + "breathing_disturbances_intensity": 9, + "snoring": 1080, + "snoringepisodecount": 18, + "sleep_score": 37, + "apnea_hypopnea_index": 9 + }, + "created": 1620237476, + "modified": 1620237476 + }, + { + "id": 2081804265, + "timezone": "Europe/Paris", + "model": 32, + "model_id": 63, + "hash_deviceid": "201d0b9a0556d6b755166b2cf8d22d3bdf0487ee", + "startdate": 1618605055, + "enddate": 1618636975, + "date": "2021-04-17", + "data": { + "wakeupduration": 2520, + "wakeupcount": 3, + "durationtosleep": 900, + "remsleepduration": 6840, + "durationtowakeup": 420, + "total_sleep_time": 26880, + "sleep_efficiency": 0.91, + "sleep_latency": 900, + "wakeup_latency": 420, + "waso": 1200, + "nb_rem_episodes": 2, + "out_of_bed_count": 0, + "lightsleepduration": 12840, + "deepsleepduration": 7200, + "hr_average": 85, + "hr_min": 50, + "hr_max": 120, + "rr_average": 16, + "rr_min": 10, + "rr_max": 20, + "breathing_disturbances_intensity": 14, + "snoring": 1140, + "snoringepisodecount": 19, + "sleep_score": 90, + "apnea_hypopnea_index": 14 + }, + "created": 1620237480, + "modified": 1620237479 + }, + { + "id": 2081804358, + "timezone": "Europe/Paris", + "model": 32, + "model_id": 63, + "hash_deviceid": "201d0b9a0556d6b755166b2cf8d22d3bdf0487ee", + "startdate": 1618518658, + "enddate": 1618548058, + "date": "2021-04-16", + "data": { + "wakeupduration": 4080, + "wakeupcount": 1, + "durationtosleep": 840, + "remsleepduration": 2040, + "durationtowakeup": 1560, + "total_sleep_time": 16860, + "sleep_efficiency": 0.81, + "sleep_latency": 840, + "wakeup_latency": 1560, + "waso": 1680, + "nb_rem_episodes": 2, + "out_of_bed_count": 0, + "lightsleepduration": 11100, + "deepsleepduration": 3720, + "hr_average": 65, + "hr_min": 50, + "hr_max": 91, + "rr_average": 14, + "rr_min": 10, + "rr_max": 20, + "breathing_disturbances_intensity": -1, + "snoring": 1020, + "snoringepisodecount": 17, + "sleep_score": 20, + "apnea_hypopnea_index": -1 + }, + "created": 1620237484, + "modified": 1620237484 + }, + { + "id": 2081804405, + "timezone": "Europe/Paris", + "model": 32, + "model_id": 63, + "hash_deviceid": "201d0b9a0556d6b755166b2cf8d22d3bdf0487ee", + "startdate": 1618432203, + "enddate": 1618453143, + "date": "2021-04-15", + "data": { + "wakeupduration": 4080, + "wakeupcount": 1, + "durationtosleep": 840, + "remsleepduration": 2040, + "durationtowakeup": 1560, + "total_sleep_time": 16860, + "sleep_efficiency": 0.81, + "sleep_latency": 840, + "wakeup_latency": 1560, + "waso": 1680, + "nb_rem_episodes": 2, + "out_of_bed_count": 0, + "lightsleepduration": 11100, + "deepsleepduration": 3720, + "hr_average": 65, + "hr_min": 50, + "hr_max": 91, + "rr_average": 14, + "rr_min": 10, + "rr_max": 20, + "breathing_disturbances_intensity": -1, + "snoring": 1020, + "snoringepisodecount": 17, + "sleep_score": 20, + "apnea_hypopnea_index": -1 + }, + "created": 1620237486, + "modified": 1620237486 + }, + { + "id": 2081804490, + "timezone": "Europe/Paris", + "model": 32, + "model_id": 63, + "hash_deviceid": "201d0b9a0556d6b755166b2cf8d22d3bdf0487ee", + "startdate": 1618345805, + "enddate": 1618373504, + "date": "2021-04-14", + "data": { + "wakeupduration": 3600, + "wakeupcount": 2, + "durationtosleep": 780, + "remsleepduration": 3960, + "durationtowakeup": 300, + "total_sleep_time": 22680, + "sleep_efficiency": 0.86, + "sleep_latency": 780, + "wakeup_latency": 300, + "waso": 3939, + "nb_rem_episodes": 4, + "out_of_bed_count": 3, + "lightsleepduration": 12960, + "deepsleepduration": 5760, + "hr_average": 98, + "hr_min": 70, + "hr_max": 120, + "rr_average": 13, + "rr_min": 10, + "rr_max": 20, + "breathing_disturbances_intensity": 29, + "snoring": 960, + "snoringepisodecount": 16, + "sleep_score": 62, + "apnea_hypopnea_index": 29 + }, + "created": 1620237490, + "modified": 1620237489 + } +] diff --git a/tests/components/withings/test_init.py b/tests/components/withings/test_init.py index baec7e92ea0..3f20791ac4d 100644 --- a/tests/components/withings/test_init.py +++ b/tests/components/withings/test_init.py @@ -120,11 +120,9 @@ async def test_data_manager_webhook_subscription( hass: HomeAssistant, withings: AsyncMock, webhook_config_entry: MockConfigEntry, - hass_client_no_auth: ClientSessionGenerator, ) -> None: """Test data manager webhook subscriptions.""" await setup_integration(hass, webhook_config_entry) - await hass_client_no_auth() await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=1)) await hass.async_block_till_done() @@ -158,12 +156,10 @@ async def test_webhook_subscription_polling_config( hass: HomeAssistant, withings: AsyncMock, polling_config_entry: MockConfigEntry, - hass_client_no_auth: ClientSessionGenerator, freezer: FrozenDateTimeFactory, ) -> None: """Test webhook subscriptions not run when polling.""" await setup_integration(hass, polling_config_entry, False) - await hass_client_no_auth() await hass.async_block_till_done() freezer.tick(timedelta(seconds=1)) async_fire_time_changed(hass) diff --git a/tests/components/withings/test_sensor.py b/tests/components/withings/test_sensor.py index 6738d9a3eb4..6cf33c45c9d 100644 --- a/tests/components/withings/test_sensor.py +++ b/tests/components/withings/test_sensor.py @@ -2,7 +2,6 @@ from datetime import timedelta from unittest.mock import AsyncMock, patch -from aiowithings import Goals, MeasurementGroup from freezegun.api import FrozenDateTimeFactory import pytest from syrupy import SnapshotAssertion @@ -11,14 +10,9 @@ from homeassistant.const import STATE_UNAVAILABLE, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er -from . import setup_integration +from . import load_goals_fixture, load_measurements_fixture, setup_integration -from tests.common import ( - MockConfigEntry, - async_fire_time_changed, - load_json_array_fixture, - load_json_object_fixture, -) +from tests.common import MockConfigEntry, async_fire_time_changed @pytest.mark.usefixtures("entity_registry_enabled_by_default") @@ -78,11 +72,6 @@ async def test_update_updates_incrementally( 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 ( @@ -90,7 +79,10 @@ async def test_update_updates_incrementally( == "2019-08-01 12:00:00+00:00" ) - withings.get_measurement_since.return_value = measurement_groups + withings.get_measurement_since.return_value = load_measurements_fixture( + "withings/measurements_1.json" + ) + await _skip_10_minutes() assert ( str(withings.get_measurement_since.call_args_list[1].args[0]) @@ -116,21 +108,16 @@ async def test_update_new_measurement_creates_new_sensor( 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 + withings.get_measurement_in_period.return_value = load_measurements_fixture( + "withings/measurements_1.json" + ) 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 + withings.get_measurement_in_period.return_value = load_measurements_fixture( + "withings/measurements.json" + ) freezer.tick(timedelta(minutes=10)) async_fire_time_changed(hass) @@ -146,17 +133,15 @@ async def test_update_new_goals_creates_new_sensor( freezer: FrozenDateTimeFactory, ) -> None: """Test fetching new goals will add a new sensor.""" - goals_json = load_json_object_fixture("withings/goals_1.json") - goals = Goals.from_api(goals_json) - withings.get_goals.return_value = goals + + withings.get_goals.return_value = load_goals_fixture("withings/goals_1.json") + await setup_integration(hass, polling_config_entry, False) assert hass.states.get("sensor.henk_step_goal") is None assert hass.states.get("sensor.henk_weight_goal") is not None - goals_json = load_json_object_fixture("withings/goals.json") - goals = Goals.from_api(goals_json) - withings.get_goals.return_value = goals + withings.get_goals.return_value = load_goals_fixture("withings/goals.json") freezer.tick(timedelta(hours=1)) async_fire_time_changed(hass)