예제 #1
0
def new_profile_config(
    profile: str,
    user_id: int,
    api_response_user_get_device: UserGetDeviceResponse | Exception
    | None = None,
    api_response_measure_get_meas: MeasureGetMeasResponse | Exception
    | None = None,
    api_response_sleep_get_summary: SleepGetSummaryResponse | Exception
    | None = None,
    api_response_notify_list: NotifyListResponse | Exception | None = None,
    api_response_notify_revoke: Exception | None = None,
) -> ProfileConfig:
    """Create a new profile config immutable object."""
    return ProfileConfig(
        profile=profile,
        user_id=user_id,
        api_response_user_get_device=api_response_user_get_device
        or UserGetDeviceResponse(devices=[]),
        api_response_measure_get_meas=api_response_measure_get_meas
        or MeasureGetMeasResponse(
            measuregrps=[],
            more=False,
            offset=0,
            timezone=dt_util.UTC,
            updatetime=arrow.get(12345),
        ),
        api_response_sleep_get_summary=api_response_sleep_get_summary
        or SleepGetSummaryResponse(more=False, offset=0, series=[]),
        api_response_notify_list=api_response_notify_list
        or NotifyListResponse(profiles=[]),
        api_response_notify_revoke=api_response_notify_revoke,
    )
예제 #2
0
def test_notify_list(withings_api: WithingsApi) -> None:
    """Test function."""
    responses_add_notify_list()

    assert withings_api.notify_list() == NotifyListResponse(profiles=(
        NotifyListProfile(
            appli=NotifyAppli.WEIGHT,
            callbackurl="http://localhost/callback",
            comment="fake_comment1",
            expires=None,
        ),
        NotifyListProfile(
            appli=NotifyAppli.CIRCULATORY,
            callbackurl="http://localhost/callback2",
            comment="fake_comment2",
            expires=arrow.get("2019-09-02"),
        ),
        NotifyListProfile(
            appli=NotifyAppli.UNKNOWN,
            callbackurl="http://localhost/callback2",
            comment="fake_comment2",
            expires=arrow.get("2019-09-02"),
        ),
    ))

    assert_url_path(responses.calls[0].request.url, "/notify")
    assert_url_query_equals(responses.calls[0].request.url, {"action": "list"})
예제 #3
0
async def test_data_manager_webhook_subscription(
    hass: HomeAssistant,
    component_factory: ComponentFactory,
    aioclient_mock: AiohttpClientMocker,
) -> None:
    """Test data manager webhook subscriptions."""
    person0 = new_profile_config("person0", 0)
    await component_factory.configure_component(profile_configs=(person0, ))

    api: ConfigEntryWithingsApi = MagicMock(spec=ConfigEntryWithingsApi)
    data_manager = DataManager(
        hass,
        "person0",
        api,
        0,
        WebhookConfig(id="1234",
                      url="http://localhost/api/webhook/1234",
                      enabled=True),
    )

    # pylint: disable=protected-access
    data_manager._notify_subscribe_delay = datetime.timedelta(seconds=0)
    data_manager._notify_unsubscribe_delay = datetime.timedelta(seconds=0)

    api.notify_list.return_value = NotifyListResponse(profiles=(
        NotifyListProfile(
            appli=NotifyAppli.BED_IN,
            callbackurl="https://not.my.callback/url",
            expires=None,
            comment=None,
        ),
        NotifyListProfile(
            appli=NotifyAppli.BED_IN,
            callbackurl=data_manager.webhook_config.url,
            expires=None,
            comment=None,
        ),
        NotifyListProfile(
            appli=NotifyAppli.BED_OUT,
            callbackurl=data_manager.webhook_config.url,
            expires=None,
            comment=None,
        ),
    ))

    aioclient_mock.clear_requests()
    aioclient_mock.request(
        "HEAD",
        data_manager.webhook_config.url,
        status=200,
    )

    # Test subscribing
    await data_manager.async_subscribe_webhook()
    api.notify_subscribe.assert_any_call(data_manager.webhook_config.url,
                                         NotifyAppli.WEIGHT)
    api.notify_subscribe.assert_any_call(data_manager.webhook_config.url,
                                         NotifyAppli.CIRCULATORY)
    api.notify_subscribe.assert_any_call(data_manager.webhook_config.url,
                                         NotifyAppli.ACTIVITY)
    api.notify_subscribe.assert_any_call(data_manager.webhook_config.url,
                                         NotifyAppli.SLEEP)
    try:
        api.notify_subscribe.assert_any_call(data_manager.webhook_config.url,
                                             NotifyAppli.USER)
        assert False
    except AssertionError:
        pass
    try:
        api.notify_subscribe.assert_any_call(data_manager.webhook_config.url,
                                             NotifyAppli.BED_IN)
        assert False
    except AssertionError:
        pass
    try:
        api.notify_subscribe.assert_any_call(data_manager.webhook_config.url,
                                             NotifyAppli.BED_OUT)
        assert False
    except AssertionError:
        pass

    # Test unsubscribing.
    await data_manager.async_unsubscribe_webhook()
    api.notify_revoke.assert_any_call(data_manager.webhook_config.url,
                                      NotifyAppli.BED_IN)
    api.notify_revoke.assert_any_call(data_manager.webhook_config.url,
                                      NotifyAppli.BED_OUT)