2019-08-31 12:30:59 +00:00
|
|
|
"""Tests for the withings component."""
|
2023-09-09 17:11:28 +00:00
|
|
|
from collections.abc import Iterable
|
2023-09-11 10:36:37 +00:00
|
|
|
from typing import Any
|
2023-09-09 17:11:28 +00:00
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
|
|
|
import arrow
|
|
|
|
from withings_api import DateType
|
|
|
|
from withings_api.common import (
|
|
|
|
GetSleepSummaryField,
|
|
|
|
MeasureGetMeasGroupCategory,
|
|
|
|
MeasureGetMeasResponse,
|
|
|
|
MeasureType,
|
2023-09-11 10:36:37 +00:00
|
|
|
NotifyAppli,
|
|
|
|
NotifyListResponse,
|
2023-09-09 17:11:28 +00:00
|
|
|
SleepGetSummaryResponse,
|
|
|
|
UserGetDeviceResponse,
|
|
|
|
)
|
|
|
|
|
|
|
|
from homeassistant.components.webhook import async_generate_url
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
2023-09-11 10:36:37 +00:00
|
|
|
from .common import WebhookResponse
|
|
|
|
|
|
|
|
from tests.common import load_json_object_fixture
|
2023-09-09 17:11:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def call_webhook(
|
|
|
|
hass: HomeAssistant, webhook_id: str, data: dict[str, Any], client
|
|
|
|
) -> WebhookResponse:
|
|
|
|
"""Call the webhook."""
|
|
|
|
webhook_url = async_generate_url(hass, webhook_id)
|
|
|
|
|
|
|
|
resp = await client.post(
|
|
|
|
urlparse(webhook_url).path,
|
|
|
|
data=data,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Wait for remaining tasks to complete.
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
data: dict[str, Any] = await resp.json()
|
|
|
|
resp.close()
|
|
|
|
|
|
|
|
return WebhookResponse(message=data["message"], message_code=data["code"])
|
|
|
|
|
|
|
|
|
|
|
|
class MockWithings:
|
|
|
|
"""Mock object for Withings."""
|
|
|
|
|
2023-09-11 10:36:37 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
device_fixture: str = "person0_get_device.json",
|
|
|
|
measurement_fixture: str = "person0_get_meas.json",
|
|
|
|
sleep_fixture: str = "person0_get_sleep.json",
|
|
|
|
notify_list_fixture: str = "person0_notify_list.json",
|
|
|
|
):
|
2023-09-09 17:11:28 +00:00
|
|
|
"""Initialize mock."""
|
2023-09-11 10:36:37 +00:00
|
|
|
self.device_fixture = device_fixture
|
|
|
|
self.measurement_fixture = measurement_fixture
|
|
|
|
self.sleep_fixture = sleep_fixture
|
|
|
|
self.notify_list_fixture = notify_list_fixture
|
2023-09-09 17:11:28 +00:00
|
|
|
|
|
|
|
def user_get_device(self) -> UserGetDeviceResponse:
|
|
|
|
"""Get devices."""
|
2023-09-11 10:36:37 +00:00
|
|
|
fixture = load_json_object_fixture(f"withings/{self.device_fixture}")
|
|
|
|
return UserGetDeviceResponse(**fixture)
|
2023-09-09 17:11:28 +00:00
|
|
|
|
|
|
|
def measure_get_meas(
|
|
|
|
self,
|
|
|
|
meastype: MeasureType | None = None,
|
|
|
|
category: MeasureGetMeasGroupCategory | None = None,
|
|
|
|
startdate: DateType | None = None,
|
|
|
|
enddate: DateType | None = None,
|
|
|
|
offset: int | None = None,
|
|
|
|
lastupdate: DateType | None = None,
|
|
|
|
) -> MeasureGetMeasResponse:
|
|
|
|
"""Get measurements."""
|
2023-09-11 10:36:37 +00:00
|
|
|
fixture = load_json_object_fixture(f"withings/{self.measurement_fixture}")
|
|
|
|
return MeasureGetMeasResponse(**fixture)
|
2023-09-09 17:11:28 +00:00
|
|
|
|
|
|
|
def sleep_get_summary(
|
|
|
|
self,
|
|
|
|
data_fields: Iterable[GetSleepSummaryField],
|
2023-09-11 10:36:37 +00:00
|
|
|
startdateymd: DateType | None = arrow.utcnow(),
|
|
|
|
enddateymd: DateType | None = arrow.utcnow(),
|
|
|
|
offset: int | None = None,
|
|
|
|
lastupdate: DateType | None = arrow.utcnow(),
|
2023-09-09 17:11:28 +00:00
|
|
|
) -> SleepGetSummaryResponse:
|
|
|
|
"""Get sleep."""
|
2023-09-11 10:36:37 +00:00
|
|
|
fixture = load_json_object_fixture(f"withings/{self.sleep_fixture}")
|
|
|
|
return SleepGetSummaryResponse(**fixture)
|
|
|
|
|
|
|
|
def notify_list(
|
|
|
|
self,
|
|
|
|
appli: NotifyAppli | None = None,
|
|
|
|
) -> NotifyListResponse:
|
|
|
|
"""Get sleep."""
|
|
|
|
fixture = load_json_object_fixture(f"withings/{self.notify_list_fixture}")
|
|
|
|
return NotifyListResponse(**fixture)
|