Ejemplo n.º 1
0
    async def factory(config: WithingsFactoryConfig) -> WithingsFactoryData:
        CONFIG_SCHEMA(config.hass_config.get(DOMAIN))

        await async_process_ha_core_config(
            hass, config.hass_config.get("homeassistant"))
        assert await async_setup_component(hass, http.DOMAIN,
                                           config.hass_config)
        assert await async_setup_component(hass, api.DOMAIN,
                                           config.hass_config)

        nokia_auth_get_credentials_patch = asynctest.patch(
            "nokia.NokiaAuth.get_credentials",
            return_value=nokia.NokiaCredentials(
                access_token="my_access_token",
                token_expiry=time.time() + 600,
                token_type="my_token_type",
                refresh_token="my_refresh_token",
                user_id="my_user_id",
                client_id=config.withings_config.get(const.CLIENT_ID),
                consumer_secret=config.withings_config.get(
                    const.CLIENT_SECRET),
            ),
        )
        nokia_auth_get_credentials_mock = nokia_auth_get_credentials_patch.start(
        )

        nokia_api_request_patch = asynctest.patch(
            "nokia.NokiaApi.request",
            return_value=config.nokia_request_response)
        nokia_api_request_mock = nokia_api_request_patch.start()

        nokia_api_get_measures_patch = asynctest.patch(
            "nokia.NokiaApi.get_measures",
            return_value=config.nokia_measures_response)
        nokia_api_get_measures_mock = nokia_api_get_measures_patch.start()

        nokia_api_get_sleep_patch = asynctest.patch(
            "nokia.NokiaApi.get_sleep",
            return_value=config.nokia_sleep_response)
        nokia_api_get_sleep_mock = nokia_api_get_sleep_patch.start()

        nokia_api_get_sleep_summary_patch = asynctest.patch(
            "nokia.NokiaApi.get_sleep_summary",
            return_value=config.nokia_sleep_summary_response,
        )
        nokia_api_get_sleep_summary_mock = nokia_api_get_sleep_summary_patch.start(
        )

        data_manager_get_throttle_interval_patch = asynctest.patch(
            "homeassistant.components.withings.common.WithingsDataManager"
            ".get_throttle_interval",
            return_value=config.throttle_interval,
        )
        data_manager_get_throttle_interval_mock = (
            data_manager_get_throttle_interval_patch.start())

        get_measures_patch = asynctest.patch(
            "homeassistant.components.withings.sensor.get_measures",
            return_value=config.measures,
        )
        get_measures_patch.start()

        patches.extend([
            nokia_auth_get_credentials_patch,
            nokia_api_request_patch,
            nokia_api_get_measures_patch,
            nokia_api_get_sleep_patch,
            nokia_api_get_sleep_summary_patch,
            data_manager_get_throttle_interval_patch,
            get_measures_patch,
        ])

        # Collect the flow id.
        tasks = []

        orig_async_create_task = hass.async_create_task

        def create_task(*args):
            task = orig_async_create_task(*args)
            tasks.append(task)
            return task

        async_create_task_patch = asynctest.patch.object(
            hass, "async_create_task", side_effect=create_task)

        with async_create_task_patch:
            assert await async_setup_component(hass, DOMAIN,
                                               config.hass_config)
            await hass.async_block_till_done()

            flow_id = tasks[2].result()["flow_id"]

        return WithingsFactoryData(
            hass,
            flow_id,
            nokia_auth_get_credentials_mock,
            nokia_api_request_mock,
            nokia_api_get_measures_mock,
            nokia_api_get_sleep_mock,
            nokia_api_get_sleep_summary_mock,
            data_manager_get_throttle_interval_mock,
        )
Ejemplo n.º 2
0
def config_schema_validate(withings_config) -> None:
    """Assert a schema config succeeds."""
    hass_config = {http.DOMAIN: {}, const.DOMAIN: withings_config}

    return CONFIG_SCHEMA(hass_config)
Ejemplo n.º 3
0
def config_schema_validate(withings_config):
    """Assert a schema config succeeds."""
    hass_config = BASE_HASS_CONFIG.copy()
    hass_config[const.DOMAIN] = withings_config

    return CONFIG_SCHEMA(hass_config)