2020-06-16 18:16:18 +00:00
|
|
|
"""Tests for the Withings component."""
|
|
|
|
from typing import Any
|
2023-09-11 19:06:20 +00:00
|
|
|
from unittest.mock import AsyncMock
|
2020-06-16 18:16:18 +00:00
|
|
|
|
2023-09-09 17:11:28 +00:00
|
|
|
import pytest
|
|
|
|
from syrupy import SnapshotAssertion
|
|
|
|
from withings_api.common import NotifyAppli
|
2020-06-16 18:16:18 +00:00
|
|
|
|
2022-12-16 09:50:20 +00:00
|
|
|
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
2020-06-16 18:16:18 +00:00
|
|
|
from homeassistant.components.withings.const import Measurement
|
2023-09-11 20:25:08 +00:00
|
|
|
from homeassistant.components.withings.entity import WithingsEntityDescription
|
2022-12-16 14:35:03 +00:00
|
|
|
from homeassistant.components.withings.sensor import SENSORS
|
2020-06-16 18:16:18 +00:00
|
|
|
from homeassistant.core import HomeAssistant, State
|
2021-03-09 13:25:03 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
2020-06-16 18:16:18 +00:00
|
|
|
from homeassistant.helpers.entity_registry import EntityRegistry
|
|
|
|
|
2023-09-11 19:06:20 +00:00
|
|
|
from . import call_webhook, setup_integration
|
2023-09-09 17:11:28 +00:00
|
|
|
from .common import async_get_entity_id
|
2023-09-11 19:06:20 +00:00
|
|
|
from .conftest import USER_ID, WEBHOOK_ID
|
2023-09-09 17:11:28 +00:00
|
|
|
|
2023-09-11 19:06:20 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2023-09-09 17:11:28 +00:00
|
|
|
from tests.typing import ClientSessionGenerator
|
2022-12-16 09:50:20 +00:00
|
|
|
|
2022-12-16 19:04:07 +00:00
|
|
|
WITHINGS_MEASUREMENTS_MAP: dict[Measurement, WithingsEntityDescription] = {
|
2022-12-16 14:35:03 +00:00
|
|
|
attr.measurement: attr for attr in SENSORS
|
2022-12-16 09:50:20 +00:00
|
|
|
}
|
2020-06-16 18:16:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
EXPECTED_DATA = (
|
2023-09-11 10:36:37 +00:00
|
|
|
(Measurement.WEIGHT_KG, 70.0),
|
|
|
|
(Measurement.FAT_MASS_KG, 5.0),
|
|
|
|
(Measurement.FAT_FREE_MASS_KG, 60.0),
|
|
|
|
(Measurement.MUSCLE_MASS_KG, 50.0),
|
|
|
|
(Measurement.BONE_MASS_KG, 10.0),
|
|
|
|
(Measurement.HEIGHT_M, 2.0),
|
|
|
|
(Measurement.FAT_RATIO_PCT, 0.07),
|
|
|
|
(Measurement.DIASTOLIC_MMHG, 70.0),
|
|
|
|
(Measurement.SYSTOLIC_MMGH, 100.0),
|
|
|
|
(Measurement.HEART_PULSE_BPM, 60.0),
|
|
|
|
(Measurement.SPO2_PCT, 0.95),
|
|
|
|
(Measurement.HYDRATION, 0.95),
|
|
|
|
(Measurement.PWV, 100.0),
|
|
|
|
(Measurement.SLEEP_BREATHING_DISTURBANCES_INTENSITY, 160.0),
|
|
|
|
(Measurement.SLEEP_DEEP_DURATION_SECONDS, 322),
|
|
|
|
(Measurement.SLEEP_HEART_RATE_AVERAGE, 164.0),
|
|
|
|
(Measurement.SLEEP_HEART_RATE_MAX, 165.0),
|
|
|
|
(Measurement.SLEEP_HEART_RATE_MIN, 166.0),
|
|
|
|
(Measurement.SLEEP_LIGHT_DURATION_SECONDS, 334),
|
|
|
|
(Measurement.SLEEP_REM_DURATION_SECONDS, 336),
|
|
|
|
(Measurement.SLEEP_RESPIRATORY_RATE_AVERAGE, 169.0),
|
|
|
|
(Measurement.SLEEP_RESPIRATORY_RATE_MAX, 170.0),
|
|
|
|
(Measurement.SLEEP_RESPIRATORY_RATE_MIN, 171.0),
|
|
|
|
(Measurement.SLEEP_SCORE, 222),
|
|
|
|
(Measurement.SLEEP_SNORING, 173.0),
|
|
|
|
(Measurement.SLEEP_SNORING_EPISODE_COUNT, 348),
|
|
|
|
(Measurement.SLEEP_TOSLEEP_DURATION_SECONDS, 162.0),
|
|
|
|
(Measurement.SLEEP_TOWAKEUP_DURATION_SECONDS, 163.0),
|
|
|
|
(Measurement.SLEEP_WAKEUP_COUNT, 350),
|
|
|
|
(Measurement.SLEEP_WAKEUP_DURATION_SECONDS, 176.0),
|
2020-06-16 18:16:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def async_assert_state_equals(
|
2022-12-16 14:35:03 +00:00
|
|
|
entity_id: str,
|
|
|
|
state_obj: State,
|
|
|
|
expected: Any,
|
2022-12-16 19:04:07 +00:00
|
|
|
description: WithingsEntityDescription,
|
2020-06-16 18:16:18 +00:00
|
|
|
) -> None:
|
|
|
|
"""Assert at given state matches what is expected."""
|
|
|
|
assert state_obj, f"Expected entity {entity_id} to exist but it did not"
|
|
|
|
|
|
|
|
assert state_obj.state == str(expected), (
|
|
|
|
f"Expected {expected} but was {state_obj.state} "
|
2022-12-16 19:04:07 +00:00
|
|
|
f"for measure {description.measurement}, {entity_id}"
|
2020-06-16 18:16:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-09-09 17:11:28 +00:00
|
|
|
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
2020-06-16 18:16:18 +00:00
|
|
|
async def test_sensor_default_enabled_entities(
|
2023-02-17 15:52:28 +00:00
|
|
|
hass: HomeAssistant,
|
2023-09-11 19:06:20 +00:00
|
|
|
withings: AsyncMock,
|
|
|
|
config_entry: MockConfigEntry,
|
|
|
|
disable_webhook_delay,
|
2023-09-09 17:11:28 +00:00
|
|
|
hass_client_no_auth: ClientSessionGenerator,
|
2020-06-16 18:16:18 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test entities enabled by default."""
|
2023-09-11 19:06:20 +00:00
|
|
|
await setup_integration(hass, config_entry)
|
2021-03-09 13:25:03 +00:00
|
|
|
entity_registry: EntityRegistry = er.async_get(hass)
|
2020-06-16 18:16:18 +00:00
|
|
|
|
2023-09-11 19:06:20 +00:00
|
|
|
client = await hass_client_no_auth()
|
|
|
|
# Assert entities should exist.
|
|
|
|
for attribute in SENSORS:
|
|
|
|
entity_id = await async_get_entity_id(hass, attribute, USER_ID, SENSOR_DOMAIN)
|
|
|
|
assert entity_id
|
|
|
|
assert entity_registry.async_is_registered(entity_id)
|
|
|
|
resp = await call_webhook(
|
|
|
|
hass,
|
|
|
|
WEBHOOK_ID,
|
|
|
|
{"userid": USER_ID, "appli": NotifyAppli.SLEEP},
|
|
|
|
client,
|
|
|
|
)
|
|
|
|
assert resp.message_code == 0
|
|
|
|
resp = await call_webhook(
|
|
|
|
hass,
|
|
|
|
WEBHOOK_ID,
|
|
|
|
{"userid": USER_ID, "appli": NotifyAppli.WEIGHT},
|
|
|
|
client,
|
|
|
|
)
|
|
|
|
assert resp.message_code == 0
|
|
|
|
|
|
|
|
for measurement, expected in EXPECTED_DATA:
|
|
|
|
attribute = WITHINGS_MEASUREMENTS_MAP[measurement]
|
|
|
|
entity_id = await async_get_entity_id(hass, attribute, USER_ID, SENSOR_DOMAIN)
|
|
|
|
state_obj = hass.states.get(entity_id)
|
|
|
|
|
|
|
|
async_assert_state_equals(entity_id, state_obj, expected, attribute)
|
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,
|
|
|
|
disable_webhook_delay,
|
|
|
|
config_entry: MockConfigEntry,
|
2023-09-09 17:11:28 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test all entities."""
|
2023-09-11 19:06:20 +00:00
|
|
|
await setup_integration(hass, config_entry)
|
|
|
|
|
|
|
|
for sensor in SENSORS:
|
|
|
|
entity_id = await async_get_entity_id(hass, sensor, USER_ID, SENSOR_DOMAIN)
|
|
|
|
assert hass.states.get(entity_id) == snapshot
|